Question
Asked Nov 30, 2019
77 views

analyze each program and figure out what is wrong with the memory management in the code and how to fix the code.

#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>

const int SIZE = 10;

int *
myfunction() {
int array[SIZE];
for (int i = 0; i< SIZE; i++) {
array[i] = 0;
}
return array;
}

int
main(int argc, char *argv[])
{
int *p = myfunction();
for (int i=0; i<SIZE; i++) {
printf("%d: %d\n", i, p[i]);
}

return 0;
}

 

check_circle

Expert Answer

Step 1

The memory using local array in function. This array value stored in stack until function return, that is why system does not free the stack space. This generates the problem of array overridden.

To overcome from this problem:

Use malloc to allocate the memory in array.

Assert is used because if evaluated value would be 0 then it causes an assertion failure and exiting from program.

Step 2

Program:

#include <unistd.h>

#include <stdio.h>

#include <stdlib.h>

#include <assert.h>

 

const int SIZE = 10;

 

int *

myfunction() {

//allacting memory of local variable using malloc()

int *array=(int*) malloc(SIZE*sizeof(int));

//apply assert to avoid assertion failure

assert(array!=NULL);

for (int i = 0; i< SIZE; i++) ...

Want to see the full answer?

See Solution

Check out a sample Q&A here.

Want to see this answer and more?

Solutions are written by subject experts who are available 24/7. Questions are typically answered within 1 hour.*

See Solution
*Response times may vary by subject and question.
Tagged in
EngineeringComputer Science

Programing Language

Related Computer Science Q&A

Find answers to questions asked by student like you
Show more Q&A
add
question_answer

Q: In my C++ program, I am trying to write code that will check command line arguments for "-amount" an...

A: Note:The expression “floor(x)-x!=0” can be used to check whether the value of “x” is a double value....

question_answer

Q: Transient PopulationPopulations are affected by the birth and death rate, as well as the number of p...

A: Program:#include &lt;iostream&gt;using namespace std;double population(double P, double B, double D,...

question_answer

Q: What are formatted input and output statements?

A: Formatting –The C or C++ language allows a programmer to format an input to process the value for fu...

question_answer

Q: Using Java Eclipse create the following program

A: Below is the approach and java code to create an ArrayList which adds three objects Loan, Date, Circ...

question_answer

Q: Python 3.7.4: I'm wasting a second request for the same question because the person who answered the...

A: This question asks about to check year is Leap year or not. So, let's know about Leap Year : Leap Ye...

question_answer

Q: Express the complement of the following function in sum of minterms: a) F (A,B,C,D) = Σ (0,2,6,11,13...

A: a.Complement of  F (A,B,C,D) = Σ (0,2,6,11,13,14)The complement of a function is obtained by replaci...

question_answer

Q: Online Responder used to issue certificates to network devices, such as routers and switches. True...

A: Online responder (OR):A position service which allows customers to test the revocation status of a c...

question_answer

Q: Write a c++ program for You are burning some music CDs for a party. You've arranged a list of songs ...

A: The C++ program is given below:Approach:Declaring essential headers and namespaces to perform input ...

question_answer

Q: Simplify the following Boolean function and expression, using four-variable maps  a) cF(w,x,y,z) = Σ...

A: Since you have posted multiple parts and have not specified which part needs to be answered. So, we ...