1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
velikii [3]
2 years ago
10

You are provided with the following program poem.cpp. All is good, and the memory is allocated dynamically, but it crashes your

computer, because it runs out of memory really quickly:
#include
using namespace std;

string * createAPoemDynamically() {
string *p = new string;
*p = "Roses are red, violets are blue";
return p;
}

int main() {
while(true) {
string *p;
p = createAPoemDynamically();

// assume that the poem p is not needed at this point

}
}
Fix this program poem.cpp. It should still keep creating poems, but all dynamically allocated memory should get deleted when it is not needed.
Computers and Technology
1 answer:
Maslowich2 years ago
8 0

Answer:

See explaination

Explanation:

#include <iostream>

using namespace std;

string * createAPoemDynamically()

{

string *p = new string;

*p = "Roses are red, violets are blue";

return p;

}

int main() {

while(true) {

string *p;

p = createAPoemDynamically();

if (!p)

{

cout << "Memory allocation failed\n";

}

cout<<*p<<"\n";

// assume that the poem p is not needed at this point

//delete allocated memory

delete p;

}

}

You might be interested in
There is an interface I that has abstract class AC as its subclass. Class C inherits AC. We know that C delegates to an object o
Aloiza [94]

Answer:

are u in HS or college work

I am trying to understand

6 0
2 years ago
Darnell is preparing to read A Rocket to the Stars for class. What might he predict based on the title?
liq [111]

Answer:

It is science fiction

Explanation:

Rockets and stars relate to science

8 0
2 years ago
Read 2 more answers
What's the main idea??
tatyana61 [14]
My computer wont let me download this pdf

7 0
2 years ago
You must keep track of some data. Your options are: (1) A linked-list maintained in sorted order. (2) A linked-list of unsorted
ladessa [460]

Answer:

Question was incomplete and continued the question

For each of the following scenarios, which of these choices would be best? Explain your answer.

BST

Sorted Array

Un-sorted Array

a) The records are guaranteed to arrive already sorted from lowest to highest (i.e., whenever a record is inserted, its key value will always be greater than that of the last record inserted). A total of 1000 inserts will be interspersed with 1000 searches.

b) The records arrive with values having a uniform random distribution (so the BST is likely to be well balanced). 1,000,000 insertions are performed, followed by 10 searches.

Explanation:

Answer for a: Un-sorted array or Un-sorted linked list : as mentioned in the question itself that the records are arriving in the sorted order and search will not be O(log n) and insert will be not be O(n).

Answer for b : Un-sorted array or Un-sorted linkedlist : Number of the items to be inserted is already known which is 1,000,000 but it is very high and at the same time search is low. Unsorted array or Unsorted linked list will be best option here.

7 0
3 years ago
Based on the information in the table, which of the following tasks is likely to take the longest amount of time when scaled up
kakasveta [241]

Answer:

Task A

Explanation:

8 0
3 years ago
Other questions:
  • Which of the following would likely be covered under homeowners insurance but NOT by renter's insurance?
    9·2 answers
  • The Internet of Things (IoT) is a concept with emphasis on machine-to-machine communications to describe a more complex system t
    10·1 answer
  • Write a program that prompts the user to enter an equation in the form of 10 5, or 10-5, or 1*5, or 13/4, or 13%4. The program s
    6·1 answer
  • Many companies use software to scan resumes and search for _____________.
    10·1 answer
  • Cliff just started working with a client who has a very disorganized AdWords account. What’s an effective way for him to begin r
    15·1 answer
  • Which of the following is most likely to be a result of hacking? Group of answer choices slowing of network speed certain Web si
    13·1 answer
  • assume an int array, candy, stores the number of candy bars sold by a group of children wherecandy[j] is the number of candy bar
    7·2 answers
  • Hi wanna play fortnite tomorrow add me im batjoker09 no caps or spaces
    13·1 answer
  • Xavier wants to print the slides of his PowerPoint presentation to practice and make notes but doesn't want to use too much ink
    5·2 answers
  • In PowerPoint, a picture might be a photograph, a shape you draw, a piece of clip art, or an illustration created using a graphi
    7·2 answers
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!