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

Memory Allocation Questions and Answers

Answer: A

Correct answer: allocated memory is never released and its pointer is lost

A leak is memory that remains allocated while the program has no pointer left with which to free it. The block stays reserved for the life of the process, and in a long-running program repeated leaks exhaust the heap.

Please log in or register to report this question.

92.

Which of the following will return a result most quickly for searching a given key?

Answer: D

Correct answer: Binary Search Tree

A balanced binary search tree finds a key in \( O(\log n) \) comparisons, halving the search space at each step.

An unsorted array and a sorted linked list both require \( O(n) \), the list because it cannot be indexed and so cannot be bisected however well ordered it is. A sorted array also permits binary search at \( O(\log n) \), so the two are comparable on lookup - but the tree keeps that performance while supporting insertion and deletion in \( O(\log n) \), where the array would need elements shifted.

Please log in or register to report this question.

Answer: C

Correct answer: Preprocessor, Compiler, Assembler, Linking

The order follows what each stage needs from the last. The preprocessor runs first, handling #include, #define and conditional compilation to produce a single translation unit of pure C.

The compiler turns that into assembly, the assembler turns assembly into an object file of machine code with unresolved references, and the linker joins the object files and libraries into an executable, resolving those references. A missing function is a link error precisely because the linker is the first stage that looks across files.

Please log in or register to report this question.

Answer: B

Correct answer: It is for the type-casting

The cast converts the void * that malloc returns to an int *. It tells the compiler how to treat the value; malloc itself never sees it and learns nothing about the intended type.

In C the cast is unnecessary, since void * converts implicitly to any object pointer type, and it can do harm: if stdlib.h has been forgotten, the compiler assumes a return type of int, and the cast silences the very warning that would have exposed the problem. In C++ it is required.

Please log in or register to report this question.

95.

Point out the error in the following program.

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

int main()
{
    int *a[3];
    a = (int*) malloc(sizeof(int)*3);
    free(a);
    return 0;
}

 

Answer: B

Correct answer: Error: We cannot store address of allocated memory in a

int *a[3] declares an array of three pointers. An array name is not a modifiable lvalue, so the compiler rejects any assignment to a.

What the code appears to intend is int *a;, a single pointer able to hold the returned address. The parentheses make all the difference in such declarations, and reading them from the identifier outwards - a is an array of 3 pointers to int - is the reliable technique.

Please log in or register to report this question.

96.

Specify the 2 library functions to dynamically allocate memory?

Answer: C

Correct answer: malloc() and calloc()

malloc and calloc are the two allocation functions in standard C. malloc takes a single total size and leaves the block uninitialised; calloc takes a count and an element size and zeroes the result.

The standard library also provides realloc to resize an existing block and free to release one, so four functions in all - but only two of them allocate fresh storage. The others named in the options are not standard C at all.

Please log in or register to report this question.

Answer: A

Correct answer: The number of elements and the size of each element

calloc() multiplies the two arguments to find the total number of bytes, then clears them. Splitting the request in two lets the library detect an overflow in that multiplication, which a single-argument malloc() call cannot do.

Please log in or register to report this question.

98.

If malloc() successfully allocates memory it returns the number of bytes it has allocated.

Answer: B

Correct answer: FALSE

malloc returns a pointer to the allocated block, not a count of bytes. On success it is the address of the storage; on failure it is null.

The number of bytes is what the caller supplied as the argument, so returning it would convey nothing. There is in fact no portable way to ask how large an allocated block is once it has been made, which is why programs that need to know must record the size themselves alongside the pointer.

Please log in or register to report this question.

Loading…
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