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
How many voltage values can be represented with a 10-bit binary code?
viktelen [127]
Answer is D. 1024
8 bits is enough to represent 256 different numbers. 9 bits is enough to represent 512 numbers. 10 bits can represent 1024 numbers.
8 0
2 years ago
What might be one reason why a stock becomes more valuable over time
Nikolay [14]
1. The General Market is Down This is for the most part the most evident reason that a stock is underestimated and happens when the large scale perspective of the economy is poor. It is helpful for financial specialists to have some fundamental apparatuses to esteem the general market so they can get ready as the market progresses toward becoming underestimated.
7 0
3 years ago
How to get out of compatibility mode in word?
fomenos
Compatibility mode is so older or different versions of word all look the same regardless of its current version.  So a lot of features you see in compatibility mode will be unavailable unless you upgrade.  If you upgrade though be sure to uninstall the older version first.  I hope this helped!!! Good Luck! :)
5 0
3 years ago
What type of device does a computer turn to first when attempting to make contact with a host with a known IP address on another
scoray [572]

Answer:

default gateway

Explanation:

A default gateway in a computer network environment means the access point through which a networked device uses to send or receive information from another network. Default gateways make it possible to exchange information from a local network to an outside network. So simply they serve as the interface through which communication is made with external networks or the internet.

6 0
3 years ago
Create a function named first_a that uses a list comprehension. The function will take a single integer parameter n. Find every
aivan3 [116]

Answer:

def first_a(n):

   lst1 =[x for x in range(1,n+1)if x%6==0 or x%11 ==0]

   print(lst1)

Explanation:

Above is a function in python programming language. We have used list comprehension to check numbers that are multiples of 6 or 11 in a range.

When this function is called it will receive an argument (n) of type integer, a range will then be generated from 1 to n+1 since n is inclusive. The modulo operator is used to determine is a value is a multiple of 6 or 11, since their multiples will evaluate to 0

3 0
3 years ago
Other questions:
  • PLEASE HELP ME ON THIS..............PLEASEEEEEEEEEEEE PLEASEEEEEE<br><br> ITTS EASYYYYYYYY
    12·1 answer
  • A tool you might use to manipulate an image, such as a photograph, into a seamless repeating texture would be:
    7·2 answers
  • How do you get the computer to stop opening randomly
    15·2 answers
  • Robert's employer has agreed to pay half the tuition for Robert to complete his college degree. This benefit is known as what?
    7·2 answers
  • How can I make a website login system with only using php?​
    6·1 answer
  • This matches the domain name with the correct IP address:
    12·1 answer
  • In a gear system, when you gain torque you lose what?
    11·1 answer
  • What is the name of the advanced warrior race of robots in Doctor Who?
    10·2 answers
  • Which of the following keywords allows a subclass to access a superclass method even when the subclass has overridden the superc
    9·1 answer
  • Which are ways that technology keeps you hooked?
    6·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!