189k views
3 votes
Is "p = malloc ( 4 * sizeof(int) );" a statically allocated array?

A) yes
B) no
C) maybe
D) not sure

1 Answer

4 votes

Final answer:

The code "p = malloc ( 4 * sizeof(int) );" dynamically allocates memory for an array of 4 integers at runtime, so it is not a statically allocated array.

Step-by-step explanation:

The statement "p = malloc ( 4 * sizeof(int) );" does not create a statically allocated array. Instead, it dynamically allocates memory for an array of 4 integers. When using malloc(), memory is allocated at runtime from the heap, which is a pool of memory the program can use for dynamic allocation.

Static allocation, on the other hand, occurs at compile time and the array size must be known beforehand. An example of static allocation would be declaring an array like int arr[4]; within a function or globally, where arr would have its space allocated on the stack or in the program's data segment, respectively.

User Urvish Modi
by
7.6k points