C: Using calloc() For Dynamic Memory Allocation.


Video demonstration: https://youtu.be/xhnOzx4TzH4

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

// Dynamic Memory Allocation - calloc
// ----------------------------------
// 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.
    
    int *save = a; // Save memory location to ptr a!
    free(a); // Deallocate memory!
    
    // Display contents of memory locations that were free'd.
    // free() does not delete data, it simply frees it for use again, therefore some data will remain.
    printf("save: %p\n", save); // Prints memory location of save.
    // Display memory after using free:
    for (int i=0; i<num_ints; ++i)
        printf("save[%d] = %d\n", i, save[i]);
    printf("\n"); // new line!

    return 0;
}

==215== Memcheck, a memory error detector
==215== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
==215== Using Valgrind-3.15.0 and LibVEX; rerun with -h for copyright info
==215== Command: ./calloc
==215==
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
save: 0x4a4e8c0
==215== Invalid read of size 4
==215==    at 0x1093AE: main (in /mnt/c/Users/perei/source/repos/c_programs/dyn_mem/calloc/calloc)
==215==  Address 0x4a4e8c0 is 0 bytes inside a block of size 20 free'd
==215==    at 0x483CA3F: free (in /usr/lib/x86_64-linux-gnu/valgrind/vgpreload_memcheck-amd64-linux.so)
==215==    by 0x109378: main (in /mnt/c/Users/perei/source/repos/c_programs/dyn_mem/calloc/calloc)
==215==  Block was alloc'd at
==215==    at 0x483DD99: calloc (in /usr/lib/x86_64-linux-gnu/valgrind/vgpreload_memcheck-amd64-linux.so)
==215==    by 0x10926C: main (in /mnt/c/Users/perei/source/repos/c_programs/dyn_mem/calloc/calloc)
==215==
save[0] = 0
save[1] = 1
save[2] = 2
save[3] = 3
save[4] = 4

==215== 
==215== HEAP SUMMARY:
==215==     in use at exit: 0 bytes in 0 blocks
==215==   total heap usage: 3 allocs, 3 frees, 2,068 bytes allocated
==215==
==215== All heap blocks were freed -- no leaks are possible
==215==
==215== For lists of detected and suppressed errors, rerun with: -s
==215== ERROR SUMMARY: 5 errors from 1 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