Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

How to answer these pointer and allocation questions. Please explain number 1 to

ID: 3728832 • Letter: H

Question

How to answer these pointer and allocation questions. Please explain number 1 to me in detail so I know how to answer the other questions related to it. I deleted all other problems because they were mostly repeat, and if I could just figure out how to do one, then I'd be fine.

1. How many bytes are allocated? Is there anything wrong with this allocation? If so, how would you fix it?
int* x = (int*) malloc(12);

4. Try to guess what will happen, then try it out in a C program. Write your answer beside each question.
a)
int* x;
printf("%d ", x);
It will print out the address of x
b)
int* x;
printf("%d ", *x);
It would return an error
c)
int* x = NULL;
printf("%d ", *x);
return an error
d)
int* x = (int*) malloc(sizeof(int) * 10);
x[10] = 4;

e)
int* x = (int*) malloc(10);
x[6] = 4;
f)
int x[10];
x[10] = 4;
g)
char* x = "flip";
x[2] = 'o';

Explanation / Answer

1. How many bytes are allocated? Is there anything wrong with this allocation? If so, how would you fix it?
int* x = (int*) malloc(12);

Yes there is an error.

Correct:  int* x = (int*) malloc(12*sizeof(int)); // you need to specify the size of the data type

So, lets assume 4 byte is the size of one integer

then 12*4 = 48 bytes for 12 integer

and 2 bytes for pointer (all lind of pointer takes 2 byte space)

so total = 48 +2 = 50 byte

Please answer others ypurself, if you have doubt then comment

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote