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

Memory Allocation Questions and Answers

41.

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.

42.

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.

47.

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.

48.

Which one is used during memory deallocation in C?

Answer: C

Correct answer: free(p);

free is the standard C deallocation function. It returns the block to the allocator, which may reuse it for a later request.

The other names belong elsewhere: delete is C++, and remove in C deletes a file rather than memory. Two conventions are worth keeping with free: passing NULL is explicitly defined to do nothing, so no guard is needed, and setting the pointer to NULL afterwards turns a later misuse into an immediate fault.

Please log in or register to report this question.

49.

calloc() returns a storage that is initialized to.

Answer: A

Correct answer: Zero

calloc clears the block it returns, setting every byte to zero. That is its distinguishing feature, and the reason it is preferred where a zeroed structure or a counter array is wanted.

A caution worth knowing: all-zero bytes give an integer value of zero and, on essentially every current implementation, a floating-point zero and a null pointer as well - but the standard guarantees only the integer case. Code that relies 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.

50.

What will be the output of the program?

#include <stdio.h>
#include <stdlib.h>

int main()
{
    int *p;
    p = (int *)malloc(20); /* Assume p has address of 1314 */
    free(p);
    printf("%u", p);
    return 0;
}

 

Answer: A

Correct answer: 1314

free takes its argument by value and does nothing to the caller's pointer. The bits in p are exactly what they were, so it still prints 1314.

That is precisely what makes a dangling pointer dangerous: it looks entirely valid, and dereferencing it may appear to work while the freed block is untouched. The conventional discipline is to set the pointer to NULL immediately after freeing, so that a later misuse fails loudly rather than silently.

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