//-----------------------------------------------------------------------------
// memoryTroubles.c
//
// program will demonstrate the dynamic use of memory
// it shows typical problems when using dynamic memory
//
// group: group 1,13 study assistant AltiHar
//
// author: Altinger Harald 0630xxx
//
// last change: created all (Altinger Harald)
//-----------------------------------------------------------------------------
//

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

//----------------------------------------------------------------------------- 
/// 
/// The main program. 
///
/// making troubels :-)
/// 
///  @return int  always zero 
// 
int main()
{
  int *memory = NULL;
  int *backup = NULL;
  int count = 0;
  
  memory = malloc(sizeof(int)); //get some momory
  
  if(memory == NULL)
  {
    printf("ERROR: xxx\n");
    return 1; //terminate program, in case of 0 byte left!
  }
  
  //little work arround for demonstration;
  //in reallity backup would be backup = memory, memory = malloc(sizeof(int));
  backup = memory + 1;
  
  printf("pointer to memory:       %p\n",memory);
  printf("pointer to backup:       %p\n",backup);
  
  backup[0] = 'c';
  printf("stored value at backup:  %d == %c\n",backup[0],backup[0]);
  
  //now assign some data to the memory
  for(count = 0; count < 2; count++)
  {
    memory[count] = 0x61;
  }
  
  for(count = 0; count < 4;count++)
  {
    printf("memory[%d] =              %d\n",count,memory[count]);
  }
  
  printf("stored value at backup:  %d == %c\n",backup[0],backup[0]);
  
  free(memory);
  
  return 0;
}

