1. #include #include int main() { int* x = malloc(5*sizeof(int)); int y[5] = {5,
ID: 3861912 • Letter: 1
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 < 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);
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 <stdio.h>
#include <stdlib.h>
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?
c. sizeof(pointer*sizeof(int));Explanation / Answer
1)a. Error due to malloc incorrectly being called
2)c. sizeof(pointer*sizeof(int));
3)No
4)No
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.