0
0
Cprogramming~5 mins

realloc function - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What is the purpose of the realloc function in C?
The realloc function changes the size of a previously allocated memory block. It can increase or decrease the size without losing the existing data up to the minimum of old and new sizes.
Click to reveal answer
intermediate
What happens if realloc cannot enlarge the existing memory block in place?
If realloc cannot extend the current block, it allocates a new block of the requested size, copies the old data to the new block, frees the old block, and returns a pointer to the new block.
Click to reveal answer
intermediate
What does realloc(ptr, 0) do?
Calling realloc with size 0 is implementation-defined: it may either free the memory pointed to by ptr and return NULL, or return a unique pointer that should not be dereferenced.
Click to reveal answer
intermediate
How should you safely use realloc to avoid memory leaks?
Always assign the result of realloc to a temporary pointer first. If it returns NULL, the original pointer is still valid. Only update the original pointer if realloc succeeds.
Click to reveal answer
beginner
What header file must be included to use realloc?
You must include <stdlib.h> to use the realloc function in C.
Click to reveal answer
What does realloc do if the new size is smaller than the old size?
AIt reduces the memory block size and keeps the first part of the data.
BIt frees the old block and returns NULL.
CIt increases the memory block size.
DIt leaves the memory block unchanged.
If realloc fails to allocate new memory, what happens to the original pointer?
AIt remains valid and unchanged.
BIt points to freed memory.
CIt becomes NULL.
DIt points to the new memory block.
Which header file is required to use realloc?
A<code>&lt;stdio.h&gt;</code>
B<code>&lt;stdlib.h&gt;</code>
C<code>&lt;string.h&gt;</code>
D<code>&lt;memory.h&gt;</code>
What is the correct way to avoid losing the original pointer when using realloc?
AUse <code>malloc</code> instead of <code>realloc</code>.
BAssign <code>realloc</code> result directly to the original pointer.
CCall <code>free</code> before <code>realloc</code>.
DUse a temporary pointer to store <code>realloc</code> result first.
What does realloc(NULL, size) do?
AReturns NULL.
BFrees memory.
CActs like <code>malloc(size)</code>.
DCauses a runtime error.
Explain how the realloc function works and when you would use it.
Think about changing the size of a box without losing what is inside.
You got /4 concepts.
    Describe the safe way to use realloc to avoid memory leaks or losing data.
    Imagine you want to move your stuff to a bigger box but keep the old box until the new one is ready.
    You got /4 concepts.