Memory Allocations – Let us C https://c.praveshagrawal.com Example C Programs Sat, 02 Dec 2023 11:14:51 +0000 en-US hourly 1 https://wordpress.org/?v=6.5.3 Memory Reallocation https://c.praveshagrawal.com/2023/12/02/memory-reallocation/ https://c.praveshagrawal.com/2023/12/02/memory-reallocation/#respond Sat, 02 Dec 2023 11:14:51 +0000 https://c.praveshagrawal.com/?p=101

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

int main() {
    // Memory reallocation
    int *dynamicArray;
    int newSize;

    dynamicArray = (int *)malloc(5 * sizeof(int));

    if (dynamicArray == NULL) {
        printf("Memory allocation failed.\n");
        return 1;
    }

    // Use the dynamically allocated array

    printf("Enter the new size of the array: ");
    scanf("%d", &newSize);

    dynamicArray = (int *)realloc(dynamicArray, newSize * sizeof(int));

    if (dynamicArray == NULL) {
        printf("Memory reallocation failed.\n");
        return 1;
    }

    // Use the resized array

    // Free the allocated memory
    free(dynamicArray);

    return 0;
}
]]>
https://c.praveshagrawal.com/2023/12/02/memory-reallocation/feed/ 0
Memory Allocation for Strings https://c.praveshagrawal.com/2023/12/02/memory-allocation-for-strings/ https://c.praveshagrawal.com/2023/12/02/memory-allocation-for-strings/#respond Sat, 02 Dec 2023 11:14:32 +0000 https://c.praveshagrawal.com/?p=102 #include <stdio.h> #include <stdlib.h> #include <string.h> int main() { // Memory allocation for strings char *dynamicString; dynamicString = (char *)malloc(50 * sizeof(char)); if (dynamicString == NULL) { printf("Memory allocation failed.\n"); return 1; } // Use the dynamically allocated string // Free the allocated memory free(dynamicString); return 0; } ]]> https://c.praveshagrawal.com/2023/12/02/memory-allocation-for-strings/feed/ 0 Dynamic 2D Array Allocation https://c.praveshagrawal.com/2023/12/02/dynamic-2d-array-allocation/ https://c.praveshagrawal.com/2023/12/02/dynamic-2d-array-allocation/#respond Sat, 02 Dec 2023 11:14:15 +0000 https://c.praveshagrawal.com/?p=103 #include <stdio.h> #include <stdlib.h> int main() { // Dynamic 2D array allocation int rows, cols; printf("Enter the number of rows: "); scanf("%d", &rows); printf("Enter the number of columns: "); scanf("%d", &cols); int **dynamic2DArray = (int **)malloc(rows * sizeof(int *)); for (int i = 0; i < rows; i++) { dynamic2DArray[i] = (int *)malloc(cols * sizeof(int)); } // Use the dynamically allocated 2D array // Free the allocated memory for (int i = 0; i < rows; i++) { free(dynamic2DArray[i]); } free(dynamic2DArray); return 0; } ]]> https://c.praveshagrawal.com/2023/12/02/dynamic-2d-array-allocation/feed/ 0 Dynamic Array Allocation https://c.praveshagrawal.com/2023/12/02/dynamic-array-allocation/ https://c.praveshagrawal.com/2023/12/02/dynamic-array-allocation/#respond Sat, 02 Dec 2023 11:13:48 +0000 https://c.praveshagrawal.com/?p=99 #include <stdio.h> #include <stdlib.h> int main() { // Dynamic array allocation int *dynamicArray; int size; printf("Enter the size of the array: "); scanf("%d", &size); dynamicArray = (int *)malloc(size * sizeof(int)); if (dynamicArray == NULL) { printf("Memory allocation failed.\n"); return 1; } // Use the dynamically allocated array // Free the allocated memory free(dynamicArray); return 0; } ]]> https://c.praveshagrawal.com/2023/12/02/dynamic-array-allocation/feed/ 0