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

Memory Allocation Questions and Answers

Answer: D

Correct answer: Both a & b are true

Both statements describe malloc correctly. On success it returns a pointer to n bytes of uninitialised storage - the contents are whatever the allocator hands over, which is the difference from calloc.

On failure it returns NULL, and that is its only means of reporting failure. Checking the return value is therefore the caller's responsibility, and neglecting it is a common fault: the program dereferences a null pointer and crashes some distance from the real cause.

Please log in or register to report this question.

Answer: A

As defined Use of fflush(stdin) in C : fflush() is typically used for output stream only. Its purpose is to clear (or flush) the output buffer and move the buffered data to console (in case of stdout) or disk (in case of file output stream).

Please log in or register to report this question.

23.

What will be the output of the following code −

#include <stdio.h>
#include <stdlib.h>
int main() {
   union my_union {
      int i;
      float f;
      char c;
   };
   union my_union* u;
   u = (union my_union*)malloc(sizeof(union my_union));
   u->f = 20.60f;
   printf("%f", u->f);
   free(u);
   return 0;
}

Answer: B

Correct answer: 20.600000

The value is stored in the union's float member and read back through the same member, so no type punning is involved and the value survives intact.

It prints as 20.600000 because %f defaults to six decimal places. The bare 20.6 is what the source contains but not what printf produces without a precision specifier. Note that a union holds only one member at a time - writing u->f overwrites whatever u->i or u->c held.

Please log in or register to report this question.

25.

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.

26.

What is the output of this program?

#include <stdio.h>
#include <stdlib.h>
int main()
{
  int i;
  char *ptr;
  char *fun();
  ptr = fun();
  printf(" %s", ptr);
  return 0;
}

char *fun()
{
  char disk[30];
  strcpy(disk, "letsfindcourse");
  printf("%s ",disk);
  return disk;
}

 

Answer: A

 

Here disk is an auto array, when array is used as return value, it will die when the control go back to main() function of a program. Thus memory in c alone prints.

Please log in or register to report this question.

27.

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.

28.

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.

29.

The function ____ obtains block of memory dynamically.

Answer: C

Correct answer: Both a & b

malloc and calloc both obtain blocks of memory dynamically from the heap. They differ in how the size is expressed and in whether the block is zeroed, not in where the storage comes from.

malloc takes a single total and leaves the contents arbitrary; calloc takes a count and an element size and clears the block. free is the counterpart that returns storage rather than obtaining it, and realloc resizes a block already held.

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.

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