ProgrammingVery hard
In C, which of these are true about malloc's return value (assume the request succeeds)?
- A. It is always zeroed
- B. It is always an int pointer
- C. It is suitably aligned for any object type and may be assigned to a pointer of any object type after a cast or implicit conversion from void *
- D. It must be cast to char * before use
Correct answer: C. It is suitably aligned for any object type and may be assigned to a pointer of any object type after a cast or implicit conversion from void *
malloc returns a void * aligned for any standard type. Its contents are indeterminate, and in C it needs no cast.
More C Programming questions
- A program allocates an int array of size n with malloc(n sizeof(int)). For n = 2^31, on a platform with 32-bit sizet, what can go wrong?
- A malloc'd block of 16 bytes is written past its end by 4 bytes. What is the correct description?
- Which function call allocates storage for an array of 100 doubles, all set to 0.0, in one step?
- A function gets a pointer to a linked list and must keep the list unchanged but needs to walk it twice. Which kind of pointer parameter is…
- A program keeps 10^5 small objects in a free list of pointers. Which operation is O(1) per object?
- int arr = malloc(5 sizeof arr); for (int i = 0; i < 5; i++) arr[i] = i i; int q = arr + 2; printf("%d", q[1]); What is printed?