Memory Allocation Questions with Answers

11 practice questions on Memory Allocation (C) with answers and step-by-step solutions. Try 10 samples below, then practise all 11 free in the AptiNest app.

Sample questions

Question 1Medium

calloc() returns a storage that is initialized to.

  1. Zero
  2. Null
  3. Nothing
  4. One

Answer: A. Zero

Explanation

calloc() sets every byte of the block it allocates to zero, so the storage starts as zero. malloc() does not do this.

Step-by-step solution

  1. Calloc allocates memory for a number of elements and sets every byte to zero.
  2. So the memory it returns starts zero-filled.
  3. The answer is Zero, which is option A.

Why the other options are wrong

  • B. Null is returned only when allocation fails, not as the initial value.
  • C. Nothing is not a value; calloc fills the memory with zeros.
  • D. One is wrong because the bytes are cleared to zero.

Tip: Calloc zeroes memory; malloc does not.

Question 2Medium

What function should be used to free the memory allocated by calloc()?

  1. dealloc();
  2. malloc(variable_name, 0)
  3. free();
  4. memalloc(variable_name, 0)

Answer: C.

Explanation

free() releases memory that was allocated with calloc(), malloc() or realloc(). It takes one pointer argument.

Step-by-step solution

  1. Memory from calloc or malloc is released with free, which takes the pointer as its argument.
  2. Dealloc is not a standard C function.
  3. Malloc and memalloc take sizes, not pointers, and they do not free memory.
  4. The answer is free();.

Why the other options are wrong

  • A. Dealloc is not a standard C function.
  • B. Malloc allocates memory and does not free it.
  • D. Memalloc is not a standard C function, and it does not free memory.

Tip: Every block from malloc, calloc or realloc is released with free.

Question 3Medium

Why do we write (int *) before malloc?

int *ip = (int *)malloc(sizeof(int));
  1. It is for the syntax correctness
  2. It is for the type-casting
  3. It is to inform malloc function about the data-typ
  4. None of the mentioned

Answer: B. It is for the type-casting

Explanation

malloc returns a pointer (void*) to the allocated memory, void pointer is generic pointer.
This can be assigned to any type of pointer.

Step-by-step solution

  1. Malloc returns a void pointer in C.
  2. The result is stored in an int pointer, so the void pointer must be converted to int *.
  3. The (int *) prefix is a cast that makes this conversion explicit.
  4. The cast does not tell malloc the data type, because malloc only receives the number of bytes from sizeof(int).
  5. So the answer is It is for the type-casting.

Why the other options are wrong

  • A. In C the cast is not needed for syntax, because void * converts to other pointer types on its own.
  • C. Malloc is not told the data type; it only gets the size in bytes.
  • D. Option B is correct, so none of the mentioned is wrong.

Tip: The cast before malloc is optional in C and required only in C++.

Question 4Medium

The function ____ obtains block of memory dynamically in c.

  1. calloc()
  2. malloc()
  3. Both a & b
  4. free()

Answer: C. Both a & b

Explanation

malloc()

The name malloc stands for "memory allocation".
The function malloc() reserves a block of memory of specified size and return a pointer of type void which can be casted intopointer of any form.
Syntax of malloc()

ptr = (cast type*) malloc (byte-size)

Here, ptr is pointer of cast-type.
The malloc() function returns a pointer to an area of memory with size of byte size.
If the space is insufficient, allocation fails and returns NULL pointer.

ptr = (int*) malloc (100*sizeof(int));

This statement will allocate either 200 or 400 according to size of int 2 or 4 bytes respectively and the pointer points to the address of first byte of memory.

calloc()

The name calloc stands for "contiguous allocation".

The only difference between malloc() and calloc() is that, malloc() allocates single block of memory whereas calloc() allocates multiple blocks of memory each of same size and sets all bytes to zero.

Syntax of calloc()

ptr = (cast-type*) calloc (n,element-size);

This statement will allocate contiguous space in memory for an array of n elements.
For example:

ptr = (float*)calloc (25,sizeof(float));

This statement allocates contiguous space in memory for an array of 25 elements each of size of float, i.e, 4 bytes.

Step-by-step solution

  1. malloc() allocates one block of memory of the requested size.
  2. calloc() allocates a number of elements and sets all the bytes to zero.
  3. Both functions obtain a block of memory dynamically, so both are correct.
  4. free() releases memory and does not obtain any.
  5. So the answer is Both a & b.

Why the other options are wrong

  • A. Calloc alone is correct, but malloc is also correct.
  • B. Malloc alone is correct, but calloc is also correct.
  • D. free() releases memory, it does not obtain it.

Tip: Malloc and calloc get memory, free gives it back.

Question 5Medium

Memory allocation using malloc() is done in?

  1. Static area
  2. Stack area
  3. Heap area
  4. Both b & c

Answer: C. Heap area

Explanation

malloc() takes memory from the heap, which is the area used for dynamic allocation. The static and stack areas are used for other kinds of variables.

Step-by-step solution

  1. malloc() allocates memory dynamically.
  2. Memory allocated by malloc() is stored in the heap area.
  3. Therefore, the correct answer is C (Heap area).

Why the other options are wrong

  • A. Static area is allocated at compile time, not during execution.
  • B. Stack area stores local variables and function calls, not dynamically allocated memory.
  • D. Option C alone correctly identifies the heap area.

Question 6Medium

Specify the 2 library functions to dynamically allocate memory?

  1. malloc() and memalloc()
  2. alloc() and memalloc()
  3. malloc() and calloc()
  4. memalloc() and faralloc()

Answer: C. malloc() and calloc()

Explanation

malloc() and calloc() are the two standard C library functions for dynamic memory allocation. memalloc(), faralloc() and alloc() are not standard C functions.

Step-by-step solution

  1. Dynamic memory allocation in standard C uses malloc, calloc, realloc and free.
  2. Malloc allocates a block of bytes without setting them to zero.
  3. Calloc allocates space for a number of items and sets them to zero.
  4. So the two standard functions are malloc and calloc.
  5. The answer is malloc() and calloc().

Why the other options are wrong

  • A. Memalloc is not a standard C function.
  • B. Alloc and memalloc are not standard C functions.
  • D. Memalloc is not standard, and faralloc is a non-standard function from older compilers.

Tip: The standard allocation functions are malloc, calloc, realloc and free.

Question 7Medium

Which header file should be included to use functions like malloc() and calloc()?

  1. memory.h
  2. stdlib.h
  3. string.h
  4. dos.h

Answer: B. stdlib.h

Explanation

stdlib.h is used because malloc() comes under it.The defination of malloc() is in a stdlib.h header file.

Step-by-step solution

  1. Malloc and calloc allocate memory on the heap.
  2. Their declarations are in stdlib.h, along with free and exit.
  3. string.h holds string functions, and dos.h holds DOS functions, so neither is the right header.
  4. So the answer is stdlib.h.

Why the other options are wrong

  • A. memory.h is not the standard header for malloc and calloc.
  • C. string.h holds string functions such as strlen, not malloc.
  • D. dos.h holds DOS-specific functions, not memory allocation.

Tip: Malloc, calloc and free are in stdlib.h.

Question 8Medium

free(p)

p is a?

  1. int
  2. Pointer returned by malloc()
  3. Pointer returned by calloc()
  4. Both b & c

Answer: D. Both b & c

Explanation

If you have allocated a memory block with the functions malloc(), calloc() or realloc() then you need to free the previously allocated memory.
Usage of free():

void free ( void * ptr );

Parameters:
The parameter is a pointer to a memory block that was allocated with malloc(), calloc() or realloc(). If you pass a null pointer then there will no action occur.

Return value:
No return value by the function free().

Step-by-step solution

  1. P is a pointer that was returned by malloc() or calloc().
  2. free() works for memory returned by both malloc() and calloc().
  3. So p can be a pointer from either, which is option D: Both b & c.

Why the other options are wrong

  • A. An int is not a pointer, and free() needs a pointer.
  • B. A malloc() pointer is valid, but it is only one of the two valid cases.
  • C. A calloc() pointer is valid, but it is only one of the two valid cases.

Tip: free() accepts any pointer from malloc, calloc or realloc.

Question 9Medium

What is the output of this C code?

 #include <stdio.h>
 void main()
 {
   char *p = calloc(100, 1);
   p = "welcome";
   printf("%s\n", p);
 }
  1. Segmentation fault
  2. Garbage
  3. Error
  4. welcome

Answer: D. welcome

Explanation

char *p

reserved for 100 char but it store only welcome string and print it.

Step-by-step solution

  1. Calloc allocates 100 zeroed bytes and stores the address in p.
  2. The next line overwrites p with the address of the string literal welcome, so the allocated block is no longer referenced.
  3. Printf with %s prints the string at p, which is welcome followed by a newline.
  4. The answer is welcome.

Why the other options are wrong

  • A. P points to a valid string literal, so there is no crash.
  • B. P does not point to garbage; it points to the literal welcome.
  • C. The program runs normally, so no error is printed.

Tip: The last assignment to a pointer decides what it points to; the earlier memory is just lost.

Question 10Medium

void * malloc(size_t n)

returns?

  1. Pointer to n bytes of uninitialized storage
  2. NULL if the request cannot be satisfied
  3. Nothing
  4. Both a & b are true

Answer: D. Both a & b are true

Explanation

The function malloc() reserves a block of memory of specified size and return a pointer of type void whichcan be casted into pointer of any form.

Step-by-step solution

  1. Malloc returns a pointer to n bytes of storage that is uninitialized.
  2. If the request cannot be satisfied, malloc returns NULL.
  3. Both statements are true of malloc.
  4. The answer is Both a & b are true.

Why the other options are wrong

  • A. This is true, but option D also covers option B.
  • B. This is true, but option D also covers option A.
  • C. Nothing is wrong here, since malloc does return a value.

Tip: Malloc gives uninitialized memory and returns NULL on failure.

Practise all 11 questions free

The full question bank with timed practice, progress tracking and Vidya, your AI tutor. Free, no ads.

Open this topic in the app →

Related topics

See all C topics · All topics