Consider the following 3 code fragments: 1) *ptr = malloc (sizeof(int)*3); ptr [
ID: 3722252 • Letter: C
Question
Consider the following 3 code fragments: 1) *ptr = malloc (sizeof(int)*3); ptr [0] 0: ptr[1] 0; ptr[2] 0; = = 2) int *ptr if (ptr calloc (3, NULL) { sizeof (int)); = printf("Unable to allocate menory. " exit (1) 3) int *ptr if (ptr calloc (1, NULL) { sizeof (int)); = printf("Unable to allocate menory. " exit (1) ptr = realloc (ptr, if ptr.. NULL){ 3*sizeof (int)); printt("Unable to allocate memory. ") exit (1) Dynamic allocation of an integer array of size 3 with elements initialized to O is safely done in code fragment(s) 2 only 1,2 and 3 O 1 and 2 O 1 only 2 and3Explanation / Answer
2). 1,2 and 3 is the right answer as in the first malloc first allocated then provide the 0 value to the pointer. In second they used calloc, so there is no need to give the value as the pointer will have the 0 value. In third, they first used calloc and then they just changed to the same size using realloc.
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.