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
memory leak? yes or no?
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
