C: Using realloc() To Resize An Array Of Ints.



 https://github.com/pereiradaniel/c_programs/blob/master/dyn_mem/realloc/realloc.c

// Dynamic Memory Allocation - realloc
// -----------------------------------
// This program accepts user input to create a dynamic array of ints.
// calloc zeroes the space first!

#include <stdio.h>
#include <stdlib.h> // calloc!

int main(int argc, char* argv[])
{
    // Prompt user for number of ints for dynamic memory allocation:
    int num_ints = 0;
    printf("This program will dynamically allocate memory for an array of ints.\nHow many ints would you like? ");
    scanf(" %d", &num_ints);

    // Create a dynamically allocated array of integers using user input:
    int *a = calloc(num_ints, sizeof(int));

    // Displays zeroed data first:
    printf("Using calloc zeroes all the data first:\n");
    for (int i=0; i<num_ints; ++i)
        printf("a[%d] = %d\n", i, a[i]);
    printf("\n"); // new line!

    // Use for loop to assign values to the int array a.
    for (int i=0; i<num_ints; ++i)
        a[i] = i;

    // Use for loop to print contents of array a.
    printf("After the ints have been assigned:\n");
    for (int i=0; i<num_ints; ++i)
        printf("a[%d] = %d\n", i, a[i]);
    printf("\n"); // new line!

    printf("a: %p\n", a); // Prints memory location of a.

    printf("Resizing a...\n");
    a = realloc(a, sizeof(int) * (num_ints + 5)); // Resize a.

    // Set new memory locations to value of 9
    for (int i=num_ints; i<(num_ints+5); ++i)
        a[i] = 9;
    printf("\n"); // new line!

    printf("After the array has been resized (new spaces filled with 9):\n");
    for (int i=0; i<(num_ints+5); ++i)
        printf("a[%d] = %d\n", i, a[i]);
    printf("\n"); // new line!

    // int *save = a; // Save memory location to ptr a!
    free(a); // Deallocate memory!
    
    return 0;
}

==273== Memcheck, a memory error detector
==273== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
==273== Using Valgrind-3.15.0 and LibVEX; rerun with -h for copyright info
==273== Command: ./realloc
==273==
This program will dynamically allocate memory for an array of ints.
How many ints would you like? 5
Using calloc zeroes all the data first:
a[0] = 0
a[1] = 0
a[2] = 0
a[3] = 0
a[4] = 0

After the ints have been assigned:
a[0] = 0
a[1] = 1
a[2] = 2
a[3] = 3
a[4] = 4

a: 0x4a4e8c0
Resizing a...

After the array has been resized (new spaces filled with 9):
a[0] = 0
a[1] = 1
a[2] = 2
a[3] = 3
a[4] = 4
a[5] = 9
a[6] = 9
a[7] = 9
a[8] = 9
a[9] = 9

==273== 
==273== HEAP SUMMARY:
==273==     in use at exit: 0 bytes in 0 blocks
==273==   total heap usage: 4 allocs, 4 frees, 2,108 bytes allocated
==273==
==273== All heap blocks were freed -- no leaks are possible
==273==
==273== For lists of detected and suppressed errors, rerun with: -s
==273== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)

Popular posts from this blog

C programming and relevancy

Shakespeare AI: My lady is more beauteous than a rose.

C: Temperature Conversion With Main Repeat