Machine-translated by Google. Questions are authored in English.
Top

Memory Allocation Questions and Answers

41.

malloc() allocates memory from the heap and not from the stack.

Answer: A

Correct answer: TRUE

malloc obtains storage from the heap, a region the program grows at run time through the operating system. That is what gives the block a lifetime independent of any function - it persists until free is called, whatever calls return in the meantime.

Stack storage behaves quite differently: it is allocated by the function prologue and reclaimed on return, which is why returning the address of a local is a fault and returning the result of malloc is not.

Please log in or register to report this question.

42.

calloc initialises memory with all bits set to zero.

Answer: A

Correct answer: true

calloc sets every bit of the allocated block to zero, and the standard requires it. That is the guarantee, not a compiler-dependent convenience.

One subtlety is worth knowing: all-zero bits reliably give integer zero, but the standard does not promise that they give a null pointer or a floating-point zero - those hold on essentially every current implementation but are not guaranteed. Code depending on a zeroed block containing valid null pointers is portable in practice rather than in principle.

Please log in or register to report this question.

43.

Memory returned by malloc() is automatically initialised to zero.

Answer: B

Correct answer: False

malloc() leaves the contents of the block indeterminate, so reading it before writing is undefined behaviour. calloc() is the function that clears the memory to zero, and it is the reason calloc() is preferred when a zeroed buffer is required.

Please log in or register to report this question.

44.

If p is a pointer to a block allocated with malloc(), what does sizeof(p) give ?

Answer: D

Correct answer: The size of a pointer on that platform

sizeof is resolved at compile time and knows nothing of what malloc() did at run time, so it reports the size of the pointer itself, typically 8 bytes on a 64-bit system. C provides no portable way to recover the size of an allocated block, so the program must remember it.

Please log in or register to report this question.

45.

What is the return type of malloc() or calloc()?

Answer: C

Correct answer: void *

Both allocators return void *, a pointer to untyped storage. The caller assigns it to a pointer of whatever type it needs, and in C that conversion is implicit.

The design is what lets a single pair of functions serve every type in the language. It also means the allocator has no idea what will be stored in the block - alignment is guaranteed to suit any type, but the size must be computed by the caller, which is why sizeof appears in almost every call.

Please log in or register to report this question.

Answer: D

Correct answer: Those that allow dynamic data structures

A heap is needed when the amount of storage required is not known until run time, and when objects must outlive the function that created them. Dynamic data structures - lists, trees, growable arrays - require exactly that.

Recursion needs only a stack, since each activation is destroyed in reverse order of creation. Dynamic scoping is a name-resolution rule, and global variables have static storage decided at compile time. None of those three forces a heap.

Please log in or register to report this question.

Answer: B

Correct answer: Until it is explicitly freed or the program ends

Heap memory has no automatic lifetime: it persists until free() is called or the process exits. This is precisely why a function can allocate a block and return its address safely, whereas returning the address of a local variable cannot work.

Please log in or register to report this question.

Answer: C

Correct answer: Preprocessor -> Compiler -> Assembler -> Linking

Each stage consumes what the previous one produced. The preprocessor handles #include, #define and conditional compilation, emitting a single translation unit of pure C.

The compiler turns that into assembly, the assembler into an object file holding machine code with unresolved external references, and the linker joins the object files and libraries into an executable. A call to a function that was declared but never defined is a link error precisely because the linker is the first stage to look across files.

Please log in or register to report this question.

Answer: D

Correct answer: All of the above

All three statements are correct. calloc zeroes the memory it returns while malloc leaves whatever the allocator hands over, which is why an uninitialised malloc block holds arbitrary data.

malloc followed by memset to zero achieves the same result, which is essentially what calloc does internally. And the signatures differ: calloc(n, size) takes a count and an element size, while malloc(size) takes a single total.

Please log in or register to report this question.

50.

Consider the following program, where are i, j and k are stored in memory?

#include <stdio.h>
#include <stdlib.h>
int i;
int main()
{
     int j;
    int *k = (int *) malloc (sizeof(int));
}

 

Answer: C

Correct answer: i is stored in BSS part of data segment, j is stored in stack segment. *k is stored on heap.

Three objects, three regions. i is a global without an initialiser, so it has static storage duration and is zero-initialised; such objects occupy the BSS, which the loader clears rather than reading from the executable.

j is automatic and therefore on the stack. The object k points to came from malloc and lives on the heap. The pointer k itself is automatic and sits on the stack beside j - the pointer and the thing it points to are in different places.

Please log in or register to report this question.

Loading…

Other Subjects

Tags: Memory Allocation Questions and Answers || Memory Allocation MCQ Questions and Answers || Memory Allocation GK Questions and Answers || Memory Allocation GK MCQ Questions || Memory Allocation Multiple Choice Questions and Answers || Memory Allocation GK || GK on Memory Allocation || C Programming Questions and Answers || C Programming MCQ Questions and Answers || C Programming GK Questions and Answers || GK on C Programming