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]
3 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:
Maslowich3 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
What is closeable interface in java.
alisha [4.7K]

Answer:

A Closeable is a source or destination of data that can be closed. The close method is invoked to release resources that the object is holding (such as open files).

Explanation:

hope it help?

8 0
3 years ago
Anybody wana play 2k21
Vaselesa [24]

Answer:

no

Explanation:

no

4 0
3 years ago
Read 2 more answers
What is characteristic of the Computer?
oksian1 [2.3K]

Answer:

storage to store the data and files

5 0
3 years ago
Read 2 more answers
Write a question that the database will understand. Which records are not equal to 5? &lt; 5 &gt; 5 &gt; =5 &lt; &gt; 5
Novay_Z [31]

Answer:

In the given question the option "<5, >5, and < >5" is not equals to 5.

Explanation:

In this question the option "<5, >5, and < >5" is not equal to 5. That can be described as:

  • In the following options, the value that is 5 is less than, greater than and less than greater than that means all option is not equal to 5.
  • In these options, only the third option is equal to the value.

That's why the answer to this question is "<5, >5, and < >5".

4 0
3 years ago
, , ,g d,t ,dt m,dt ymtd
maks197457 [2]

Answer:ummm me confused

Explanation:

4 0
3 years ago
Other questions:
  • When reading data across the network (i.e. from a URL) in Python 3, what string method must be used to convert it to the interna
    9·1 answer
  • The web server software used has a significant impact on how a web site's web pages look on a user's computer.
    7·1 answer
  • Which operating system is used by most desktop and laptop computers?
    5·1 answer
  • The PRNG variable ___________ is defined in NIST SP 800-90 as a number associated with the amount of work required to break a cr
    7·1 answer
  • Problem: you need to write a code that read 2 array of size 10x10 of zero and ones that represent black
    8·1 answer
  • Which of the following is the shortcut key combination for pasting copied text?
    11·2 answers
  • A peripheral can be used to tell a computer to complete a specific task.<br> A) True <br> B)False
    13·1 answer
  • Write a program using integers userNum and divNum as input, and output userNum divided by divNum three times.
    7·1 answer
  • Explain how work can impact<br> family life.
    8·1 answer
  • What are the advantages of using ICT jn our society?​
    8·2 answers
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!