0
0
Cprogramming~5 mins

free function - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What is the purpose of the free function in C?
The free function releases memory that was previously allocated with malloc, calloc, or realloc. It helps prevent memory leaks by returning the memory back to the system.
Click to reveal answer
intermediate
What happens if you call free on a pointer that was not allocated dynamically?
Calling free on a pointer not allocated by malloc or similar functions causes undefined behavior. This can crash the program or corrupt memory.
Click to reveal answer
intermediate
Can you use free more than once on the same pointer without reassigning it?
No. Using free multiple times on the same pointer without resetting it to NULL leads to undefined behavior, often causing crashes or memory corruption.
Click to reveal answer
beginner
Show a simple example of allocating and freeing memory for an integer in C.
Example:<br>
int *p = malloc(sizeof(int));
if (p != NULL) {
  *p = 10;
  free(p);
}
<br>This allocates memory for one integer, assigns 10, then frees the memory.
Click to reveal answer
intermediate
Why is it important to set a pointer to NULL after calling free?
Setting a pointer to NULL after free avoids accidental use of a pointer that points to freed memory, which can cause bugs or crashes.
Click to reveal answer
What does the free function do in C?
AAllocates new memory
BCopies memory from one location to another
CReleases dynamically allocated memory back to the system
DInitializes a pointer to NULL
What happens if you call free on a pointer twice without resetting it?
AUndefined behavior, possible crash
BNothing, it is safe
CMemory is allocated again
DPointer automatically resets to NULL
Which pointer should you pass to free?
APointer to a local variable
BPointer returned by <code>malloc</code>, <code>calloc</code>, or <code>realloc</code>
CPointer to a string literal
DPointer to a global variable
What is a good practice after calling free on a pointer?
ASet the pointer to NULL
BUse the pointer immediately
CCall <code>free</code> again on the same pointer
DAssign the pointer to another variable
If malloc fails and returns NULL, what happens if you call free on that NULL pointer?
AProgram crashes
BUndefined behavior
CMemory is allocated
DNothing happens, safe to call
Explain how the free function works and why it is important in C programming.
Think about memory management and what happens if you don't free memory.
You got /4 concepts.
    Describe common mistakes when using free and how to avoid them.
    Consider what can cause crashes or bugs related to freeing memory.
    You got /4 concepts.