CS Atlas GATE 2027
PDS course
0 / 103 complete

PDS/Phase 7 · Integration & Final Test

Chapter review L1.7 · Item 32 of 103

Dynamic memory

Read at your own pace. Try the questions before opening the answers.

On this page

ACompressed recap

🧾

The heap provides memory at run time: malloc (garbage), calloc (zeroed), realloc (resize, may move), free (give back). Heap blocks live until freed, so returning them from functions is fine, unlike the address of an automatic local. Bugs: dangling pointers (using freed or popped memory), leaks (losing the last pointer), double free. Set pointers to NULL after free.

BMerged rule card

1.7 Dynamic memory: malloc, calloc, realloc, free; stack vs heap; dangling pointers, memory leaks, double free

  • Regions: code, static data, heap (malloc), stack (frames).
  • malloc(n) garbage; calloc(k, s) zeroed; realloc(p, n) may move; free(p).
  • Use n * sizeof(T); check NULL; use realloc’s result.
  • Heap lives until free; stack locals die on return.
  • Dangling: using freed/popped memory. Leak: losing the last pointer. Double free: freeing twice. All undefined or wasteful.
  • After free(p), set p = NULL.

CInterleaved GATE-style questions

Q1MCQ1 markSizes

To reserve room for 10 doubles, which call is right?

  • (A) malloc(10)
  • (B) malloc(sizeof(double))
  • (C) malloc(10 * sizeof(double))
  • (D) calloc(10)

Q2MCQ2 marksOutput

What does this print?

int rows = 3, cols = 4, i, j;
int **g = malloc(rows * sizeof(int *));
for (i = 0; i < rows; i++)
    g[i] = malloc(cols * sizeof(int));
for (i = 0; i < rows; i++)
    for (j = 0; j < cols; j++)
        g[i][j] = i * cols + j;
printf("%d %d\n", g[2][1], g[1][3]);
for (i = 0; i < rows; i++)
    free(g[i]);
free(g);
  • (A) 9 7
  • (B) 2 1
  • (C) 6 11
  • (D) 9 8

Q3MCQ1 markBug

int *p = malloc(8); int *q = p; free(p); free(q); has:

  • (A) a double free
  • (B) a dangling read only
  • (C) a memory leak
  • (D) no bug

Q4NAT1 markcalloc

How many bytes does calloc(6, sizeof(short)) reserve (2-byte short)?

Numerical answer: ________

Q5MCQ2 marksLifetime

Which outlives the function that created it?

  • (A) a block from malloc
  • (B) an automatic local variable
  • (C) a parameter
  • (D) a local array

Q6MSQ2 marksErrors

Which are undefined behavior?

  • (A) Reading *p after free(p)
  • (B) Calling free(NULL)
  • (C) Freeing the same block twice
  • (D) Using memory from malloc before writing it (reading garbage as an int)

Q7MCQ1 markrealloc

After q = realloc(p, 100); succeeds, which pointer should be used?

  • (A) p
  • (B) either, they are always equal
  • (C) neither
  • (D) q

Q8MCQ2 marksOutput

What does this print?

int *makeArray(int n)
{
    int *p = malloc(n * sizeof(int));
    int i;
    for (i = 0; i < n; i++)
        p[i] = 10 * (i + 1);
    return p;                 /* fine: heap memory outlives the call */
}

int main(void)
{
    int *v = makeArray(3);
    printf("%d %d %d\n", v[0], v[1], v[2]);
    free(v);
    return 0;
}
  • (A) 10 20 30
  • (B) 1 2 3
  • (C) 0 10 20
  • (D) undefined: the memory belonged to makeArray

DAnswer Vault

Answer Vault

Try the questions first. Open when you are ready to check.

Q1 Answer: (C)1 mark

Bytes = count × size.

  • (A) is wrong: Only 10 bytes.
  • (B) is wrong: Only one double.
  • (D) is wrong: calloc takes two arguments.

Re-read: Lesson 1.7 — Dynamic memory: malloc, calloc, realloc, free; stack vs heap; dangling pointers, memory leaks, double free

Q2 Answer: (A)2 marks

g[i][j] = 4i + j: g[2][1] = 9, g[1][3] = 7.

Output

9 7
  • (B) is wrong: Values are 4i + j, not indices.
  • (C) is wrong: Rows have 4 columns: 4i + j.
  • (D) is wrong: g[1][3] = 4 + 3 = 7.

Re-read: Lesson 1.7 — Dynamic memory: malloc, calloc, realloc, free; stack vs heap; dangling pointers, memory leaks, double free

Q3 Answer: (A)1 mark

Both pointers name one block, freed twice.

  • (B) is wrong: It frees twice, not reads.
  • (C) is wrong: The block is freed (twice).
  • (D) is wrong: Freeing twice is undefined.

Re-read: Lesson 1.7 — Dynamic memory: malloc, calloc, realloc, free; stack vs heap; dangling pointers, memory leaks, double free

Q4 Answer: 121 mark

6 × 2 = 12.

Re-read: Lesson 1.7 — Dynamic memory: malloc, calloc, realloc, free; stack vs heap; dangling pointers, memory leaks, double free

Q5 Answer: (A)2 marks

Heap blocks live until freed.

  • (B) is wrong: Dies with the frame.
  • (C) is wrong: Dies with the frame.
  • (D) is wrong: Dies with the frame.

Re-read: Lesson 1.7 — Dynamic memory: malloc, calloc, realloc, free; stack vs heap; dangling pointers, memory leaks, double free

Q6 Answer: (A) (C)2 marks

free(NULL) does nothing; reading uninitialized malloc memory gives unpredictable values but the classic UB here are A and C.

  • (B) is wrong: free(NULL) is defined to do nothing.
  • (D) is wrong: The value is unpredictable, but the case GATE marks as the bug is use-after-free and double free.

Re-read: Lesson 1.7 — Dynamic memory: malloc, calloc, realloc, free; stack vs heap; dangling pointers, memory leaks, double free

Q7 Answer: (D)1 mark

The block may have moved; p may dangle.

  • (A) is wrong: p may be stale.
  • (B) is wrong: They may differ.
  • (C) is wrong: q is valid.

Re-read: Lesson 1.7 — Dynamic memory: malloc, calloc, realloc, free; stack vs heap; dangling pointers, memory leaks, double free

Q8 Answer: (A)2 marks

Heap memory survives the return.

Output

10 20 30
  • (B) is wrong: Values are multiplied by 10.
  • (C) is wrong: p[i] = 10(i + 1).
  • (D) is wrong: malloc’d memory outlives the call.

Re-read: Lesson 1.7 — Dynamic memory: malloc, calloc, realloc, free; stack vs heap; dangling pointers, memory leaks, double free

Ready for the next idea?

Mark this item complete when you’ve finished reading and working through it.