/*
* Author: Hyrum D. Carroll
* Description: Simple introduction of allocating (and deallocating) dynamic memory
*/

#include <stdio.h>   // for printf()
#include <stdlib.h>  // for malloc(), free() and NULL

const int ARRAY_SIZE = 10000;

int main(){

    /*
     * Display the address of a stack variable
     */
    int stackVariable = -1;

    printf("&stackVariable: %p\n", &stackVariable);

    /*
     * Allocate memory (on the heap) for a single int
     */
    int* myIntPtr = NULL;
    myIntPtr = (int*) malloc( sizeof(int) );

    printf("myIntPtr: %p, *myIntPtr: %d\n", myIntPtr, *myIntPtr);

    *myIntPtr = 42;
    printf("myIntPtr: %p, *myIntPtr: %d\n", myIntPtr, *myIntPtr);


    /*
     * Allocate memory (on the heap) for ARRAY_SIZE ints
     */
    int* myArrayPtr = (int*) malloc( ARRAY_SIZE * sizeof( int) );
    printf("myArrayPtr: %p, *myArrayPtr: %d\n", myArrayPtr, *myArrayPtr);

    myArrayPtr[ 55 ] = 7;

    /*
     * Deallocate memory
     */
    free( myIntPtr );
    free( myArrayPtr );

    return 0;
}