Here are the questions and answers that i received from myteacher, but i find th
ID: 3611115 • Letter: H
Question
Here are the questions and answers that i received from myteacher, but i find them questionable. I thought the program would print out a random number like-855433 or something like that since p is never assigned toanything and the data is randomized unless it is initialized. does anyone agree or am i missing something here. Also what is thesignificance of the *10 in the first questoin. thank youint main(void){ int* p = malloc(sizeof(int)*10); printf(“%d”, p[10]); free(p); } What does this print -10 answer
int main(void){ int* p = malloc(10); printf(“%d”,p[3]); } What does this print -3answer Here are the questions and answers that i received from myteacher, but i find them questionable. I thought the program would print out a random number like-855433 or something like that since p is never assigned toanything and the data is randomized unless it is initialized. does anyone agree or am i missing something here. Also what is thesignificance of the *10 in the first questoin. thank you
int main(void){ int* p = malloc(sizeof(int)*10); printf(“%d”, p[10]); free(p); } What does this print -10 answer
int main(void){ int* p = malloc(10); printf(“%d”,p[3]); } What does this print -3answer What does this print -3answer
Explanation / Answer
//Hope this will help you.//the significance of *10 is the you are allocating the memory forthe 10 integers.
//like if sizeof(int) = 4 , then 4*10 =40 , which will able to keep10 integers.
int main(void){ int* p = malloc(sizeof(int)*10);
//we have allocated the memory for 10integers, p[0] to p[9] , so p[10] is out of bound memory,
//hence it must be garbage, or may give segmentation fault.
printf(“%d”,p[10]);
free(p); }
int main(void){ int* p = malloc(10);
//similarly here, we are allocating but notinit the memory, hence will give any garbagevalue,
printf(“%d”,p[3]); }
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.