Solution for Exercise 20-8

ex2008

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

int main()
{
    int *age;

    age = (int *)malloc(sizeof(int)*1);
    if(age==NULL)
    {
        puts("Out of Memory or something!");
        exit(1);
    }
    printf("How old are you in years? ");
    scanf("%d",age);
    *age *= 365;
    printf("You're over %d days old!\n",*age);
    free(age);

    return(0);
}

Notes

* Once upon a time, you could theoretically use combinations of malloc() and free() to determine how much memory was available in a computer. Those days are long gone. Today's computers have gigabytes of RAM bolstered by virtual memory.

* If you want to discover how much memory is available in the device you're programming, then you need to make an API call to that device's operating system. That process is far more involved than I want to get into on this web site, plus its unique for each operating system. I'm just letting you know that such a thing is possible, and it doesn't involve the malloc() or free() functions.