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
Suppose L is a LIST and p, q, and r are positions. As a function of n, the length of list L, determine how many times the functi
xz_007 [3.2K]

Answer:

Explanation:

  p := FIRST(L);

  while p <> END(L) do begin

      q := p;

      while q <> END(L) do begin

          q := NEXT(q, L);

          r := FIRST(L);

          while r <> q do

              r := NEXT(r, L)

      end;

      p := NEXT(p, L)

  end;

5 0
3 years ago
Merge arrays A and B into array C using merge sort algorithm. Give final sorted numbers of C using colored numbers without showi
sweet-ann [11.9K]

Answer:

Explanation:

Given code:-

#include<iostream>

using namespace std; //namespace

#define RED "\033[31m" /* Red */

#define BLUE "\033[34m" /* Blue */ //color definition

#define WHITE "\033[37m" /* White */

void mergeArrays(int arr1[], int arr2[], int n1, int n2, int arr3[])

//merger sort

{

int i = 0, j = 0, k = 0;

while (i<n1 && j <n2) //iterate over both array

{

if (arr1[i] < arr2[j]) //iff array 1 element is larger

{

arr3[k] = arr1[i]; //assign to a3

k++;

i++;

}

else

{

arr3[k] = arr2[j]; //else assign array 2 to a3

j++;

k++;

}

}

//say the array turns out to br of different sizes

//let us copy the remaining elemnt to array 3

while (i < n1)

{

arr3[k] = arr1[i];

i++;

k++;

}

while (j < n2)

{

arr3[k] = arr2[j];

k++;

j++;

}

}

int main(){

int A[4] = {3,5,7,9}; //array 1 assignment

int a_size = 4;

int b_size = 6;

int B[6] = {1,3,7,9,13,25}; //array 2 assignment

int C[10]; //array 3 declearation

int a_count = 0;

int b_count = 0; //counter for both array index

mergeArrays(A,B,a_size,b_size,C); //merger function applied;

cout<<WHITE<<"A: ";

for(int i = 0; i<4; i++){

cout<<RED<<A[i]<<" ";

}

cout<<endl<<WHITE<<"B: ";

for(int i = 0; i<6; i++){

cout<<BLUE<<B[i]<<" ";

}

cout<<endl<<WHITE<<"C: ";

for(int i = 0; i<10; i++){ //iterate over C

if(C[i] == A[a_count] && a_count < a_size){

cout<<RED<<C[i]<<" ";

// if element is common in array 1 print in red

a_count++;

}

else if (C[i] == B[b_count] && b_count < b_size){

//say element was similar in array 2,  print in blue

cout<<BLUE<<C[i]<<" ";

b_count++;

}

}

cout<<endl;

}

7 0
2 years ago
When we look for errors inside of our code on our own or with a partner , what is that called?
lutik1710 [3]
It’s called that he is cheating
6 0
2 years ago
Read 2 more answers
A(n) ______is like an intranet except it shares its resources with users from a distant location. Select your answer, then click
nasty-shy [4]

An <u>extranet</u> is like an intranet except that, it shares its resources with users from a distant location.

<h3>What is an intranet?</h3>

An intranet simply refers to an internal organizational network which is exclusively designed and developed to be used privately. Also, it is used for providing the employees of an organization with easy access to data and information.

In Computer networking, the end users of an extranet have the ability to access a company's entire intranet from a distant location.

Read more on intranet here: brainly.com/question/2580626

#SPJ1

8 0
1 year ago
Write 3 things that can't be done without technology.
bulgar [2K]

Answer:

Hacking Online Orders Math

Explanation:

4 0
2 years ago
Read 2 more answers
Other questions:
  • Which IPv6 transition technology can be used with NAT routers and has the address prefix 2001::32?
    10·1 answer
  • Assume you have a project with seven activities labeled A-G (following). Derive the earliest completion time (or early finish-EF
    15·1 answer
  • How long did it take Linkedln to reach 1 million users?
    5·2 answers
  • Multiple Choice
    6·1 answer
  • How do you reduce computer screen flicker
    11·1 answer
  • Write the definition of a function reverse, whose first parameter is an array of integers and whose second parameter is the numb
    15·1 answer
  • In Java:
    15·1 answer
  • The transmission control protocol (TCP) layer helps computers to communicate in which of the following ways?
    10·1 answer
  • ¿Cuál es la función que cumplía los sofistas y Porque eran tan importantes?​
    13·1 answer
  • . Which responsibility belongs to the marketing function?
    11·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!