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

Memory Allocation Questions and Answers

21.

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.

23.

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.

24.

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.

25.

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.

26.

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.

27.

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.

29.

How will you free the memory allocated by the following program?

#include <stdio.h>
#include <stdlib.h>
#define MAXROW 3
#define MAXCOL 4

int main()
{
    int **p, i, j;
    p = (int **) malloc(MAXROW * sizeof(int*));
    return 0;
}

 

Answer: D

Correct answer: free(p);

The pointer that malloc returned is the one free must be given, so free(p) releases the block of row pointers.

Note what has actually been allocated: only the array of MAXROW pointers. Had the rows themselves been allocated in a loop, each would need freeing individually before free(p) - releasing the outer array first would destroy the only record of the row addresses and leak every one of them.

Please log in or register to report this question.

30.

Output?

#include <stdio.h>
#include <stdlib.h>
  
void fun(int *a)
{
    a = (int*)malloc(sizeof(int));
}
  
int main()
{
    int *p;
    fun(p);
    *p = 6;
    printf("%d\n",*p);
    return(0);
}

Answer: A

Correct answer: May not work

The pointer is passed by value, so fun receives a copy. Assigning the result of malloc to that copy changes nothing in main, where p is still uninitialised.

Writing *p = 6 then dereferences an indeterminate pointer, which is undefined behaviour - it may crash, may appear to work, may corrupt something else. To change the caller's pointer the function would need a int ** parameter, or it would have to return the new address.

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