#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <string.h>

extern int etext, edata, end; /* text and data delimiters */

char *buffer; /* Defined here because if in main it will not be in the data region */

#define SHOW_ADDRESS(STR_NAME, NAME) printf ("Variable %s at address:%8X\n", STR_NAME, NAME) 
/* this just in case i need it */

int main()
{
	char *buffer2;
        printf("etext: %6X \t edata: %6X \t end: %6X \n", &etext, &edata, &end);
        
        /*
         * In this case the sbrk function will return the value that break held 
         * before invokation. So if we pass it a 0 as the size then it should 
         * not change it but only have the property of checking the break value.
         */

        /* So the next line gives us the initial break value */
        printf("Initial break: %8X \n", sbrk(0));
        /*
         * In order to test if malloc and free change break I use them 
         * and then check the break value with the method above.
         */
        buffer=malloc((unsigned) 100);
        printf("After malloc() break: %8X \n", sbrk(0));
        
        free(buffer);
        printf("After free() break: %8X \n", sbrk(0));
        
	/*
	 * Testing a member of the localy defined variables doesn't really matter since
	 * they get stored into the stack rather than into the data region and therefore 
	 * do not affect the position of break
	 */

        /*
         * Now we want to show that break does change after sbrk() so
         * I just pass it some number of bytes and then display break again.
         */

        sbrk(100);
        printf("After sbrk(100) break: %8X \n", sbrk(0));

        /* Check the values of the etext, edata, and end again*/
        printf("etext: %6X \t edata: %6X \t end: %6X \n", &etext, &edata, &end);
        
        return(0);

        /*
         * So after going through the output of the program we can see that after 
         * allocating memory with malloc and freeing it with free the break value 
	 * only changes after malloc but not after free. 
         * The break value does change after using sbrk() which allocates 
	 * memory to the data region going down (increasing addresses).
         */ 
}

