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
The piston engine uses the ________ to convert the reciprocating motion of the piston into rotary motion.
PSYCHO15rus [73]

The piston engine uses the crankshaft to convert the reciprocating motion of the piston into rotary motion.

<span>The crankshaft is used to convert reciprocating motion of the piston into rotary motion, while the conversion process is called torque, which is a twisting force. Aside from crankshaft, there are a total of four parts of the engine that work together in order to convert the reciprocating motion into rotary motion namely cylinder, or also called the chamber of the piston, the piston itself, and the connecting rod.</span>

5 0
3 years ago
Kaylen has been working on his computer and notices that the screen seems to be flickering. The monitor is not turning on and of
Komok [63]

Answer:

Screen flickering is usually caused by an incompatible app or display driver. It could also be caused by a bad HDMI/VGA connection to the monitor.

Hope it helps ! :)

6 0
3 years ago
3 advantages of using desktop computer rather than a laptop computer
Vilka [71]
Desktop can be customized and it usually has more processing power than laptops, as for laptops are better for portable work like word document
6 0
3 years ago
Read 2 more answers
If the task is to write firewall specifications for the preparation of a(n) ____, the planner would note that the deliverable is
Tpy6a [65]

Answer:

RFP

Explanation:

It is the type of document that is used to ask vendors to propose the solutions of problem that are faced by customer. It is the abbreviation of "Request for Proposal". This proposal is processed though the bidding, to offer the services or submit business proposals by vendors.

4 0
3 years ago
What does it mean to empty the cache?
Lina20 [59]
Cache are the temporary files that are downloaded onto your computer while going on a website, to clear this, you are getting rid of any of the cache. Not what you downloaded yourself, what the internet page downloaded for you.
6 0
3 years ago
Other questions:
  • Which of the following statements is false?
    12·1 answer
  • If you delete an imessage does the other person see it
    12·1 answer
  • What are the conditions for using still photos in a video program
    6·1 answer
  • Convert to octal. Convert to hexadecimal. Then convert both of your answers todecimal, and verify that they are the same.(a) 111
    12·1 answer
  • By using password-stealing software, ________ capture what is typed on a keyboard and easily obtain enough personal information.
    5·1 answer
  • What do developers do to support software products? explain to users which development process model was used to make the produc
    8·1 answer
  • Online Book Merchants offers premium customers 1 free book with every purchase of 5 or more books and offers 2 free books with e
    11·1 answer
  • If you've finished working with a data file but intend to work on it again during your work session, which button would you use
    10·2 answers
  • ________(fill in the blank)in online education is intrinsically related to equity.
    13·1 answer
  • Acquiring new knowledge and skills at every stage of life:
    6·2 answers
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!