The _______ memory allocation function modifies the previous allocated space.

2012

The _______ memory allocation function modifies the previous allocated space.

Answer: D. realloc( )ConceptIn C, dynamic memory is handled by a small family of library functions declared in <stdlib.h>, and each of them is defined by the job it performs on…

  1. A.

    calloc( )

  2. B.

    free( )

  3. C.

    malloc( )

  4. D.

    realloc( )

Attempted by 186 students.

Show answer & explanation

Correct answer: D

Concept

In C, dynamic memory is handled by a small family of library functions declared in <stdlib.h>, and each of them is defined by the job it performs on the heap: obtaining a fresh block, obtaining a fresh block whose bytes are cleared to zero, changing the size of a block that already exists, or handing a block back to the allocator. A function can act on an allocation made by an earlier call only if its parameter list carries a pointer to that allocation, so the signature rather than the name tells you which job a function does.

Applying it here

The blank asks for the function that modifies the previously allocated space, so it must receive a pointer to a block that already exists together with a new size, and hand back a block of that new size still holding the same data. Comparing the four signatures makes the division of work explicit:

Function

Signature

Job it performs on the heap

calloc( )

void *calloc(size_t n, size_t size)

Obtains a fresh block large enough for n items of the given size and clears every byte of it to 0.

free( )

void free(void *ptr)

Hands the block that ptr refers to back to the allocator and ends its lifetime.

malloc( )

void *malloc(size_t size)

Obtains a fresh block of the requested size and leaves its bytes uninitialised.

realloc( )

void *realloc(void *ptr, size_t size)

Changes the size of the existing block that ptr refers to, keeping the stored data up to the smaller of the old and the new size.

Cross-check

  • realloc(ptr, n) may extend the block where it stands when the neighbouring heap space is free; when it cannot, it takes a new block of n bytes, copies the old contents across, releases the old block and returns the new address. A failed call returns a null pointer while the original block stays valid, so the returned value is first stored in a separate pointer, as in q = realloc(p, n), and p is updated only after that pointer has been checked.

  • The remaining boundary case confirms the same job: a resize call whose first argument is a null pointer behaves like a plain allocation of n bytes, so one function covers both starting a block and re-sizing a block that already exists. The new size may be larger or smaller than the old one, and on a shrink the data beyond the new size is discarded.

  • calloc( ) and malloc( ) each begin a new lifetime and are handed sizes rather than a pointer, so neither can touch a block created by an earlier call; free( ) ends a lifetime and hands back no block to work with.

Result

The blank is therefore filled by realloc( ), the C allocation function that modifies the previously allocated space.

Explore the full course: Mppsc Assistant Professor Computer Science Paper 2

Loading lesson…