Memory Allocation Checker

Memory Allocation Checker

Easy C Error Handling 40 views
Explanation Complexity

Problem Statement

Allocate memory with malloc(). If NULL is returned (allocation failed) to handle the error, clean the memory that has already been allocated. Resource cleanup.

Input Format

Request to allocate dynamic memory using malloc().

Output Format

• Successful allocation message

• OR error handling with proper resource cleanup if allocation fails

Example

Request memory for multiple blocks
Memory allocation failed. Cleaned up allocated resources.

Constraints

• Memory allocation may fail

• Previously allocated memory must be freed to avoid memory leaks

Concept Explanation

When dynamic memory allocation fails, malloc() returns NULL.
Any memory that was successfully allocated before the failure must be released properly

Step-by-Step Explanation

1.Allocate the first memory block using malloc().

2.Check the return value:

• If it is NULL, handle the error and stop further execution.

3. Allocate the next required memory block using malloc().

4. Again, check the return value.

5. If this allocation fails (NULL is returned):

• Free all memory blocks that were successfully allocated earlier.

• Print an appropriate error message.

• Exit the function or return an error status.

6.If all allocations succeed:

•Continue normal program execution.

Concept Explanation

When dynamic memory allocation fails, malloc() returns NULL.
Any memory that was successfully allocated before the failure must be released properly

Step-by-Step Explanation

1.Allocate the first memory block using malloc().

2.Check the return value:

• If it is NULL, handle the error and stop further execution.

3. Allocate the next required memory block using malloc().

4. Again, check the return value.

5. If this allocation fails (NULL is returned):

• Free all memory blocks that were successfully allocated earlier.

• Print an appropriate error message.

• Exit the function or return an error status.

6.If all allocations succeed:

•Continue normal program execution.

Input / Output Format

Input Format
Request to allocate dynamic memory using malloc().
Output Format
• Successful allocation message

• OR error handling with proper resource cleanup if allocation fails
Constraints
• Memory allocation may fail

• Previously allocated memory must be freed to avoid memory leaks

Examples

Input:
Request memory for multiple blocks
Output:
Memory allocation failed. Cleaned up allocated resources.

Example Solution (Public)

C
#include <stdio.h>
#include <stdlib.h>

int main() {
    int *arr1 = NULL;
    int *arr2 = NULL;

    // First allocation
    arr1 = (int *)malloc(100 * sizeof(int));
    if (arr1 == NULL) {
        printf("Memory allocation failed for arr1n");
        return 1;
    }

    // Second allocation
    arr2 = (int *)malloc(200 * sizeof(int));
    if (arr2 == NULL) {
        printf("Memory allocation failed for arr2n");

        // Cleanup already allocated memory
        free(arr1);
        arr1 = NULL;

        return 1;
    }

    // Use the memory
    printf("Memory allocated successfully.n");

    // Normal cleanup
    free(arr2);
    free(arr1);

    arr2 = NULL;
    arr1 = NULL;

    return 0;
}

Official Solution Code

#include <stdio.h>
#include <stdlib.h>

int main() {
    int *arr1 = NULL;
    int *arr2 = NULL;

    // First allocation
    arr1 = (int *)malloc(100 * sizeof(int));
    if (arr1 == NULL) {
        printf("Memory allocation failed for arr1n");
        return 1;
    }

    // Second allocation
    arr2 = (int *)malloc(200 * sizeof(int));
    if (arr2 == NULL) {
        printf("Memory allocation failed for arr2n");

        // Cleanup already allocated memory
        free(arr1);
        arr1 = NULL;

        return 1;
    }

    // Use the memory
    printf("Memory allocated successfully.n");

    // Normal cleanup
    free(arr2);
    free(arr1);

    arr2 = NULL;
    arr1 = NULL;

    return 0;
}
Please login to submit solutions.
Editor
Output

                                        
Please login to submit solutions.