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
Phoenix [80]
2 years ago
8

5. Write few lines of code that creates two arrays with malloc. Then write a statement that can create a memory leak. Discuss wh

y you think your code has a memory leak by drawing the status of the memory after you use malloc and the line of the code you claim that creates a memory leak.
Computers and Technology
1 answer:
Darina [25.2K]2 years ago
3 0

Answer:

 // function with memory leak  

void func_to_show_mem_leak()  {  

int *pointer;

pointer = malloc(10 * sizeof(int));

*(pointer+3) = 99;}  

 

// driver code  

int main()  

{  

    // Call the function  

   // to get the memory leak  

   func_to_show_mem_leak();  

     return 0;  }

Explanation:

Memory leakage occurs when programmers allocates memory by using new keyword and forgets to deallocate the memory by using delete() function or delete[] operator. One of the most memory leakage occurs by using wrong delete operator. The delete operator should be used to free a single allocated memory space, whereas the delete [] operator should be used to free an array of data values.

You might be interested in
) Perform error checking for the data point entries. If any of the following errors occurs, output the appropriate error message
emmainna [20.7K]

Answer:

In Python:

entry = input("Sentence: ")

while True:

   if entry.count(",") == 0:

       print("Error: No comma in string")

       entry = input("Sentence: ")

   elif entry.count(",") > 1:

       print("Error: Too many comma in input")

       entry = input("Sentence: ")

   else:

       ind = entry.index(',')+1

       if entry[ind].isnumeric() == False:

           print("Comma not followed by an integer")

           entry = input("Sentence: ")

       else:

           break

print("Valid Input")

Explanation:

This prompts the user for a sentence

entry = input("Sentence: ")

The following loop is repeated until the user enters a valid entry

while True:

This is executed if the number of commas is 0

<em>    if entry.count(",") == 0:</em>

<em>        print("Error: No comma in string")</em>

<em>        entry = input("Sentence: ")</em>

This is executed if the number of commas is more than 1

<em>    elif entry.count(",") > 1:</em>

<em>        print("Error: Too many comma in input")</em>

<em>        entry = input("Sentence: ")</em>

This is executed if the number of commas is 1

   else:

This calculates the next index after the comma

       ind = entry.index(',')+1

This checks if the character after the comma is a number

       if entry[ind].isnumeric() == False:

If it is not a number, the print statement is executed

<em>            print("Comma not followed by an integer")</em>

<em>            entry = input("Sentence: ")</em>

If otherwise, the loop is exited

<em>        else:</em>

<em>            break</em>

This prints valid input, when the user enters a valid string

print("Valid Input")

Note that: entry = input("Sentence: ") <em>is used to get input</em>

4 0
3 years ago
Extend to also calculate and output the number of 1 gallon cans needed to paint the wal. Hint: Use a math function to round up t
Crank

Answer:

Here is the Python program:

import math #import math to use mathematical functions

height = float(input("Enter wall height (feet): ")) #prompts user to enter wall height and store it in float type variable height

width = float(input("Enter wall width (feet): ")) #prompts user to enter wall width and store it in float type variable width

area = height *width #computes wall area

print('Wall area: {} square feet'.format(round(area))) #displays wall area using round method that returns a floating-point number rounded

sqftPerGallon = 350 #sets sqftPerGallon to 350

paintNeeded = area/ sqftPerGallon #computes needed paint

print("Paint needed: {:.2f} gallons".format(paintNeeded)) #displays computed paint needed up to 2 decimal places

cansNeeded = int(math.ceil(paintNeeded)) #computes needed cans rounding the paintNeeded up to nearest integer using math.ceil

print("Cans needed: {} can(s)".format(cansNeeded)) #displays computed cans needed

colorCostDict = {'red': 35, 'blue': 25, 'green': 23} #creates a dictionary of colors with colors as key and cost as values

color = input("Choose a color to paint the wall: ") #prompts user to enter a color

if color in colorCostDict: #if the chosen color is present in the dictionary

    colorCost = colorCostDict.get(color) #then get the color cost from dictionary and stores it into colorCost using get method that returns the value(cost) of the item with the specified key(color)

    cost = cansNeeded * colorCost #computes the cost of purchasing paint of specified color per cansNeeded

  print("Cost of purchasing {} paint: ${}".format(color,colorCostDict[color])) #displays the real cost of the chosen color paint

print("Cost of purchasing {} paint per {} gallon can(s): ${}".format(color,cansNeeded, cost)) #displays the cost of chosen color paint per cans needed.

Explanation:

The program first prompts the user to enter height and width. Lets say user enter 20 as height and 50 as width so the program becomes:

Wall area computed as:

area = height *width

area = 20 * 50

area = 1000

Hence the output of this part is:

Wall area: 1000 square feet                                                                                                                     Next program computes paint needed as:

paintNeeded = area/ sqftPerGallon

Since sqftPerGallon = 350 and area= 1000

paintNeeded = 1000 / 350

paintNeeded = 2.86

Hence the output of this part is:

Paint needed: 2.86 gallons

Next program computes cans needed as:      

cansNeeded = int(math.ceil(paintNeeded))

This rounds the computed value of paintNeeded i.e. 2.86 up to nearest integer using math.ceil so,

cansNeeded = 3                                                                  

Hence the output of this part is:

Cans needed: 3 can(s)                                                                                                                            Next program prompts user to choose a color to paint the wall

Lets say user chooses 'blue'

So the program get the cost corresponding to blue color and multiplies this cost to cans needed to compute the cost of purchasing blue paint per gallon cans. So

cost = cansNeeded * colorCost

cost = 3 * 25

cost = 75

So the output of this part is:

Cost of purchasing blue paint per 3 gallon can(s): $75                                                                                        The screenshot of the program along with its output is attached.

5 0
2 years ago
I am not a living being, I am a cylindrical shape that has three to eight sides. I never die. I can build anything again. What i
Setler79 [48]

Answer:

A shape

Explanation:

4 0
3 years ago
our client, Rhonda, has come to you for advice. Rhonda has recently graduated from college and has a good job with a large compa
Radda [10]

Answer:

See explaination for how to manage her personal risk

Explanation:

Personal risks can be described as anything that exposes you to lose of money. It is often connection to financial investments and insurance.

The basic things She can do to manage her personal risks are:

1. Saving:

Savings in much ways drastically reduces the percentage of risks and help you build confidence. Savings can help Rhonda manage her personal risks as savings helps one become financially secure and provide safety in case of emergency.

2. Investing:

After savings comes the major process, which is investment. It is rightly said, savings without invested proper is vain. Investment not only gives you returns or generates more profits but also ensures present and future long term financial security.

3. Reduce expenses:

A common man's expenses can never finish except it is controlled. Reduction in daily expenses can give a hike in savings and increase return on investment. Prompt planning can help cut in expenses.

5 0
3 years ago
Which answer choice correctly distinguishes among the three pieces of data?
wolverine [178]

Answer:

a. 1 is a packet, 2 is data, 3 is a frame.

Explanation:

And what is not  mentioned is segment which used TCP/UDP and is part of Transport layer. The packet carries the destination and sender IP address, and is part of the Network Layer. The frame has the Mac address of destination device and senders device and is part of data link layer.

Hence segment has no IP address, hence b. is not correct. Also, data cannot have the IP Address, and Frame has the MAC address, Hence, the above answer. And this arrangement is part of Data Encapsulation.

Also keep in mind data can be anything like a series of bits, or any and it can or not have a header.

7 0
2 years ago
Other questions:
  • You just read a restaurant review on the internet of the newest restaurant in town. The writer of the article said the food was
    15·1 answer
  • Carol typed a memo to send to everyone in her department. To create this memo, she used a _____. spreadsheet word processor fax
    10·2 answers
  • The lightbulb transfers electricity energy into light what is one type of energy that is also generated that is not a desired af
    14·1 answer
  • You are configuring a switch that has three hosts attached to FastEthernet 0/2 through 0/4. All three hosts are part of a public
    10·1 answer
  • The three tasks within data harmonization, namely: data consolidation, data cleansing, and data formatting use techniques called
    8·1 answer
  • What disese is sue suffering from
    15·2 answers
  • To display the Color gallery, with the graphic selected, click the Color button in the ____ group on the PICTURE TOOLS FORMAT ta
    14·1 answer
  • Find the simple interest Jay owes on a five-year student loan of $48,000 with an annual interest rate of 5%.
    6·1 answer
  • ANSWER ASAP!!!!!
    10·2 answers
  • The _____ method randomly rearranges the items in the list provided in the parentheses.
    5·2 answers
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!