Memory Management in C: malloc, calloc, free Quiz Quiz

Challenge your understanding of dynamic memory allocation in C with this quiz focused on malloc, calloc, and free. Strengthen your grasp of syntax, error prevention, and best practices for safe and effective memory management in C programming.

  1. Identifying Differences between malloc and calloc

    What is a key difference between malloc and calloc when allocating memory for an array of 10 integers?

    1. calloc does not require the number of elements as an argument
    2. malloc and calloc both initialize memory to one
    3. calloc allocates memory on the stack, malloc on the heap
    4. malloc does not initialize memory, while calloc initializes all bytes to zero

    Explanation: The correct answer is that malloc does not initialize memory, whereas calloc initializes every byte of allocated memory to zero, which helps prevent the use of garbage values. Option two is incorrect since neither function sets memory to one. The third option is wrong; calloc requires both the number of elements and the size of each element. The last option is false because both malloc and calloc allocate memory only on the heap, not the stack.

  2. Proper Use of free with Dynamically Allocated Memory

    Which statement best describes how to properly release memory allocated using malloc or calloc?

    1. Memory allocated with malloc is freed automatically at program termination
    2. Set the pointer to NULL without using free
    3. Call free and pass the pointer returned by malloc or calloc as the argument
    4. Use realloc with size zero to free memory

    Explanation: You must use the free function and pass it the pointer previously returned by malloc or calloc to release the allocated memory. Setting a pointer to NULL does not free memory; it just detaches the reference, causing a memory leak. Using realloc with zero size can free memory for some implementations, but free is the standard and clear approach. Memory allocated with malloc is not freed automatically on program termination in all cases, especially with long-running or embedded systems.

  3. Detecting Memory Leaks in a Simple Program

    In the following C snippet, which best describes the memory management issue?nnint *arr = malloc(100 * sizeof(int)); /* ...some code... */ arr = malloc(50 * sizeof(int));

    1. malloc is incorrectly used for allocating integers
    2. Memory is properly managed and there are no issues
    3. The program causes a memory leak by overwriting the original pointer
    4. The code leads to a double free error

    Explanation: Overwriting the pointer arr with a new malloc result without freeing the previously allocated memory leads to a memory leak. There is no double free as free is never called in the snippet. The memory is not properly managed, so option three is incorrect. The usage of malloc for allocating integers is correct, so option four is invalid.

  4. Correct Syntax for calloc Allocation

    Which is the correct way to allocate memory for an array of 20 floats using calloc in C?

    1. float *ptr = malloc(20);
    2. float *ptr = malloc(sizeof(float) * 20, 0);
    3. float *ptr = calloc(20, sizeof(float));
    4. float *ptr = calloc(sizeof(float), 20);

    Explanation: The correct calloc usage is to provide the number of elements (20) and the size of each (sizeof(float)), as in the first option. The second option has the arguments reversed, which may lead to errors or incorrect memory allocation. The third option with malloc only allocates 20 bytes, not enough for 20 floats on most systems. The last option is an incorrect function signature for malloc, which only takes one argument.

  5. Handling free with NULL Pointers

    What happens if you call free on a pointer that has already been set to NULL in a C program?

    1. It is safe and nothing happens; free ignores NULL pointers
    2. The pointer will be reset to an uninitialized state
    3. The program will crash with a segmentation fault
    4. The memory pointed to by NULL will be freed

    Explanation: Calling free on a NULL pointer is defined as a safe no-operation; the function simply does nothing. There is no segmentation fault or program crash in this scenario, so option two is false. The pointer itself remains NULL after the call and no uninitialized state occurs, making option three incorrect. Freeing memory at NULL is not an action since NULL does not refer to allocated memory, so option four is also inaccurate.