WalzoneInterview Prep
📞 Interviewing soon? Practice with a realistic AI mock phone interview — it calls you, then scores you. First 15 min FREE →

C · Intermediate · question 35 of 100

Explain the concept of dynamic memory allocation in C, and provide examples of functions used for this purpose.?

📕 Buy this interview preparation book: 100 C questions & answers — PDF + EPUB for $5

Dynamic memory allocation is the process of allocating memory at runtime instead of compile time. This means that the size of the memory required is not known until the program is running. Dynamic memory allocation is useful when the amount of memory required by a program is not known in advance or when memory needs to be allocated and deallocated dynamically during program execution.

In C, dynamic memory allocation is performed using the malloc(), calloc(), and realloc() functions, which are declared in the stdlib.h header file.

malloc() function: The malloc() function is used to allocate a block of memory of a specified size in bytes. The function returns a pointer to the first byte of the allocated memory block, or NULL if the allocation fails.

    int* ptr;
    ptr = (int*) malloc(5 * sizeof(int));
    if (ptr == NULL) {
        printf("Memory allocation failed.n");
    } else {
        // Use the allocated memory
        ptr[0] = 1;
        ptr[1] = 2;
        // ...
        free(ptr);  // Free the memory when done
    }

calloc() function: The calloc() function is used to allocate a block of memory of a specified size in bytes, and initializes the memory to zero. The function returns a pointer to the first byte of the allocated memory block, or NULL if the allocation fails.

    int* ptr;
    ptr = (int*) calloc(5, sizeof(int));
    if (ptr == NULL) {
        printf("Memory allocation failed.n");
    } else {
        // Use the allocated memory
        ptr[0] = 1;
        ptr[1] = 2;
        // ...
        free(ptr);  // Free the memory when done
    }

realloc() function: The realloc() function is used to reallocate a block of memory that has already been allocated using malloc() or calloc(). The function takes two arguments: a pointer to the previously allocated memory block, and the new size in bytes. The function returns a pointer to the first byte of the reallocated memory block, or NULL if the reallocation fails.

    int* ptr;
    ptr = (int*) malloc(5 * sizeof(int));
    if (ptr == NULL) {
        printf("Memory allocation failed.n");
    } else {
        // Use the allocated memory
        ptr[0] = 1;
        ptr[1] = 2;
        // ...
        ptr = (int*) realloc(ptr, 10 * sizeof(int));  // Reallocate the memory
        if (ptr == NULL) {
            printf("Memory reallocation failed.n");
        } else {
            // Use the reallocated memory
            ptr[5] = 6;
            ptr[6] = 7;
            // ...
            free(ptr);  // Free the memory when done
        }
    }

It is important to note that dynamically allocated memory must be explicitly deallocated using the free() function when it is no longer needed to avoid memory leaks. Also, when allocating memory dynamically, it is important to check the return value of the allocation functions to ensure that the memory was allocated successfully.

Reading is step one. Saying it out loud is the interview. Our AI interviewer calls your phone and runs a realistic C interview — then scores it.
📞 Practice C — free 15 min
📕 Buy this interview preparation book: 100 C questions & answers — PDF + EPUB for $5

All 100 C questions · All topics