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
This type of server spools documents and puts them in a queue.
Paraphin [41]

Print is the type of server that spools documents and puts them in a queue. Whenever you are printing multiple documents, it is the standard operation of the printer to prioritize those documents that came first and add to the queue the other documents waiting for printing.

4 0
3 years ago
Can some one help please
quester [9]

Answer:

I think its screenplay

Explanation:

Firstly, the question starts with an a so it can't be b. And format can't be repeated. Which leaves us with screenplay.

Hope this helps.

8 0
1 year ago
State one advantage of organising data into columns and rows
Bezzdna [24]
It's neat organized and ready to find when needed
6 0
3 years ago
write a program that keeps names and email addresses in a dictionary as key-value pairs. the program should display a menu that
erma4kov [3.2K]

To write a program that keeps names and email addresses in a dictionary as key-value pairs check the code given below.

<h3>What is key-value pairs?</h3>

In a key-value pair, two related data elements are combined: a value, which is a variable that belongs to the set (for example, male/female, green, 100), and a key, which is a constant that defines the data set (for example, gender, color, price).

A key-value pair could look something like this when fully formed:

gender = male

color = green

price > 100

↓↓↓//<u>Python code</u>//↓↓↓

import pickle

import sys

try:

   f=open('email.dat','rb')

   d=pickle.load(f)  

   f.close()

     

except:    

   d={}

while True:

   print('\n1. Find a email address')

   print('2. Add name and email address')

   print('3. Change an email address')

   print('4. Delete an email address')

   print('5. Exit\n')

   choice=input('\nEnter a choice: ')

   if choice:

       choice=int(choice)

   else:

       print('\nEnter a number')

       continue    

   if choice == 1:

       while True:

           name=input('\nEnter the name ')

           if name:

               if name in d:

                   print('\n%s is the email id of %s \n' % (d[name],name))

                   break

               else:

                   print('\n Email not found \n')

                   break

           else:

               print('\nName cannot be empty\n')

               continue

           

   elif choice==2:

       while True:            

       

           name=input('\nEnter the name ')

           if name:

               break;

           else:

               print('\nName cannot be empty \n')

               continue

       while True:            

       

           email=input('\nEnter the email address ')

           if email:

               d[name]=email

               break

           else:

               print('\nEmail cannot be empty\n')

               continue

           

   elif choice==3:

       while True:            

       

           name=input('\nEnter the name to change the email address ')

           if name:

               if name in d:

                   email=input('\nEnter the new email address ')

                   d[name]=email

                   print('\nEmail address changed \n')

                   break;

               else:

                   print('\nName not found \n')

                   break

           else:

               print('\nName cannot be empty \n')

               continue

           

   elif choice == 4:

       while True:            

       

           name=input('\nEnter the name to remove ')

           if name:

               if name in d:

                   del d[name]

                   print('\nName and Email address removed \n ')

                   break;

               else:

                   print('\nName not found \n')

                   break

           else:

               print('\nName cannot be empty\n')

               continue

   elif choice == 5:

       

       f=open('email.dat','wb')

       pickle.dump(d,f)

       f.close()

       sys.exit()

   else:

       print('\nEnter a valid choice ')    

Learn more about key-value pair

brainly.com/question/29414672

#SPJ1

3 0
1 year ago
The code size of 2-address instruction is________________.? 5 bytes? 7 bytes? 3 bytes? 2 bytes
Finger [1]

Answer:

7 bytes

Explanation:

<u>2 Address Instruction</u>

The 2 address instruction consist 3 components in the format.

One is opcode,other two are addresses of destination and source.

<u>Example-</u>

load b,c | Opcode   destination address,source address

add a,d  | Opcode   destination address,source address

sub c,f    | Opcode   destination address,source address

Opcode consists of 1 bytes whereas destination address and source address consist of 3 bytes each.

(1+3+3) bytes=7 bytes

5 0
3 years ago
Other questions:
  • Write a program that estimates the approximate number of times the user’s heart has beat in his/her lifetime using an average he
    12·1 answer
  • In your own words, describe the structure and function of both the stack and queue data structure and discuss how they are diffe
    6·1 answer
  • ________ is a set of rules for exchanging files such as text, graphic images, sound, video, and other multimedia files on the we
    14·1 answer
  • Current versions of windows support file names up to ________ characters long
    5·1 answer
  • Var faceS = 100; var mouthS = 30; ellipse(200, 200, faceS, faceS); ellipse(200, 220, mouthS, mouthS); How wide is the second ell
    12·1 answer
  • _____ _____ deals with the definitions properties of mathematical models of computation.
    8·1 answer
  • What does a virus do to a computer? How can it be fixed?
    9·1 answer
  • Write a method named isNumericPalindrome that accepts an integer parameter named num. If num is a palindrome the method must ret
    6·1 answer
  • How many bytes/second is a 16Mbps cable modem connection?
    9·1 answer
  • Which operating system is used by most the supercomputer system in the world
    9·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!