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
Exampels of semantic tags ?​
spayn [35]
A semantic element clearly describes its meaning to both the browser and the developer.Examples of non-semantic elements: and <span> - Tells nothing about its content. Examples of semantic elements: ,, and - Clearly defines its content.</span>
4 0
3 years ago
And, or, not are examples of boolean logic
forsale [732]

true a example  boolean logic is (and, or, and  not)

-scav

8 0
2 years ago
Can someone please help me I need to turn this in my 3:00pm Eastern time
galben [10]

Answer:

1000000000

Explanation:

8 0
2 years ago
Suppose that outfile is an ofstream variable and output is to be stored in the file outputData.out. Which of the following state
katrin [286]

Answer:

outFile.open("outputData.out");

4 0
3 years ago
Using the C language, write a function that accepts two parameters: a string of characters and a single character. The function
Sav [38]

Answer:

#include <stdio.h>

void interchangeCase(char phrase[],char c){

  for(int i=0;phrase[i]!='\0';i++){

      if(phrase[i]==c){

          if(phrase[i]>='A' && phrase[i]<='Z')

              phrase[i]+=32;

          else

              phrase[i]-=32;      

      }

  }

}

int main(){

  char c1[]="Eevee";

  interchangeCase(c1,'e');

  printf("%s\n",c1);

  char c2[]="Eevee";

  interchangeCase(c2,'E');

  printf("%s\n",c2);    

}

Explanation:

  • Create a function called interchangeCase that takes the phrase and c as parameters.
  • Run a for loop that runs until the end of phrase and check whether the selected character is found or not using an if statement.
  • If the character is upper-case alphabet, change it to lower-case alphabet and otherwise do the vice versa.
  • Inside the main function, test the program and display the results.
8 0
3 years ago
Other questions:
  • The cost of large ground based telescopes has remained the same since the 1800's costing the equivalent of about a billion dolla
    9·1 answer
  • Which word in brackets is most opposite to the word in capitals? PROSCRIBE (allow, stifle, promote, verify)​
    14·2 answers
  • Information from the system that is used to make modifications in the input, processing actions, or outputs is referred to as: G
    9·2 answers
  • Discuss the pros and cons of tombstoning versus multitasking. Why do you think Microsoft chose tombstoning?
    11·1 answer
  • 1. The programmer intends for this pseudocode to display three random numbers in the range of 1 through 7. According to the way
    12·1 answer
  • In the Programming Process which of the following is not involved in defining what the program is to do:_____________ Group of a
    10·1 answer
  • An online journal or diary you create to share your thoughts, opinions and personal news is referred to
    13·1 answer
  • Can anybody answer this
    11·1 answer
  • Advantages of Linux include_____.
    6·1 answer
  • Write a program that takes a list of integers as input. The input begins with an integer indicating the number of integers that
    8·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!