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

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 and3

Explanation / 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.

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