Question: 1. #include #include int main() { int* x = malloc(5*sizeof(int)); int y[5] = {5,5,5,5,5}; *x = y; int i; for (i = 0; i <

1. #include

#include

int main() { int* x = malloc(5*sizeof(int));

int y[5] = {5,5,5,5,5};

*x = y;

int i;

for (i = 0; i < 5; ++i) {

printf("%d ", *(x+i)); }

free(x);

return 0; }

What will be printed?

a. Error due to malloc incorrectly being called

b. Error because free was incorrectly used

c. Error because malloc returns a void*, but we are assigning it to int*

d. Error/warning due to how y was stored into x

2. You are given a function that returns a pointer (int* pointer) to a dynamically allocated array that was created using malloc. How can you get the size of the array?

a. sizeof(free(pointer));

b. sizeof(pointer);

c. sizeof(pointer*sizeof(int));

3. #include

#include int* foo() { int *x = malloc(3*sizeof(int)); //assume x populated correctly here return x; } int main() { int* y = foo(); int i; for(i = 0; i < 3; ++i) { printf("%d ", *(y+i)); } free(y); return 0; }

Memory leak? yes or no?

4.#include #include int* foo() { int *x = malloc(1*sizeof(int)); *x = 5; free(x); return x; } int main() { int* y = foo(); printf("%d", *y); return 0; }

memory leak? yes or no?

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!