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

Memory Allocation Questions and Answers

31.

Which function is used to delete the allocated memory space?

Answer: B

Correct answer: free()

free returns a block to the allocator. It takes the pointer that malloc, calloc or realloc returned - not some address inside the block - and passing anything else is undefined behaviour.

There is no dealloc in standard C. Two conventions are worth keeping: free(NULL) is defined to do nothing, so no guard is needed, and setting the pointer to NULL immediately afterwards turns a later misuse into an immediate fault rather than a silent one.

Please log in or register to report this question.

32.

Among 4 header files, which should be included to use the memory allocation functions like malloc(), calloc(), realloc() and free()?

Answer: B

Correct answer: #include<stdlib.h>

stdlib.h declares malloc, calloc, realloc and free. It is the only header of those offered that is required.

string.h declares memset and strcpy, which are often used alongside allocation but are not part of it. memory.h is a non-standard header on some systems and should not be relied upon. Omitting stdlib.h is particularly dangerous because an older compiler will assume malloc returns int and truncate the address.

Please log in or register to report this question.

Answer: D

Correct answer: All of the above

All three statements hold. calloc zeroes what it returns while malloc leaves arbitrary data; malloc followed by memset reproduces that effect; and both return void *.

One difference the list does not mention is worth knowing: calloc multiplies its two arguments internally and is required to detect overflow in that product, whereas a hand-written malloc(m * n) can wrap silently and allocate far too little - a well-known source of security bugs.

Please log in or register to report this question.

Answer: D

Correct answer: calloc() initialises the allocated memory to zero while malloc() does not

calloc() takes the number of elements and the size of each, and clears the whole block to zero before returning it. malloc() takes a single size in bytes and leaves the contents indeterminate, so reading them before assignment is undefined behaviour.

Please log in or register to report this question.

36.

Which of the following statement is correct prototype of the malloc() function in c ?

Answer: D

Correct answer: void* malloc(size_t);

The prototype takes a size_t - an unsigned type large enough to hold the size of any object - and returns void *.

Both halves matter. The void * return is what allows one function to serve every type, and size_t rather than int is what allows allocations larger than an int can express on a 64-bit machine. It also means a negative size argument is converted to a very large positive one, so the allocation simply fails rather than misbehaving.

Please log in or register to report this question.

37.

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.

38.

malloc() returns a float pointer if memory is allocated for storing float's and a double pointer if memory is allocated for storing double's. A.

Answer: B

Correct answer: FALSE

malloc returns void * regardless of what will be stored in the block. It has no way of knowing: the only argument it receives is a number of bytes.

The conversion to float * or double * happens at the assignment, implicitly in C. What malloc does guarantee is that the block is suitably aligned for any type, which is what makes that later conversion safe whatever the caller has in mind.

Please log in or register to report this question.

39.

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.

40.

What is the output of this program?

#include <stdio.h>
#include <stdlib.h>
int main()
{
  int i, numbers[1];
  numbers[0] = 15;
  free(numbers);
  printf("Stored integers are ");
  printf("numbers[%d] = %d ", 0, numbers[0]);
  return 0;
}

 

Answer: A

he memory allocation function free() will not free or delete the content in the memory location, because free() can only free or delete the content in the memory, who's memory is allocated either by malloc() or calloc().

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