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

#define NUMBER_OF_ELEMENTS 10
#define NEW_NUMBER_OF_ELEMENTS 15

//use echo %ERRORLEVEL% to display the programs exit code in windows, tried only locally
//#define NUMBER_OF_ELEMENTS 1000000000
//#define NEW_NUMBER_OF_ELEMENTS 1500000000

#define NO_MEMORY -1;

//--------------------------------------------------------------------
int main()
{
  // requesting dynamic allocation of memory for the array
  int *test_array = malloc(NUMBER_OF_ELEMENTS * sizeof(int));
  
  //no more memory
  if (test_array == NULL)
    return NO_MEMORY;
  
  unsigned int count = 0;
  
  // fill array with values
  for (count = 0; count < NUMBER_OF_ELEMENTS; count++)  
    test_array[count] = count;
  
  // print values
  for (count = 0; count < NUMBER_OF_ELEMENTS; count++)
    printf("Element %d has value %d\n", count, test_array[count]);
  
  //---------------------

  // realloc new memory
  int *temp = realloc(test_array, NEW_NUMBER_OF_ELEMENTS * sizeof(int));

  // errorhandling (might be better the other way round?!)
  if (temp != NULL)
    test_array = temp;
  else
  {
    free(test_array);
    test_array = NULL;
    temp       = NULL;
    return NO_MEMORY;
  }

  // add new elements 
  for (count = NUMBER_OF_ELEMENTS; count < NEW_NUMBER_OF_ELEMENTS; count ++)
    test_array[count] = count ;    

  // print values
  for (count = 0; count < NEW_NUMBER_OF_ELEMENTS; count ++)
    printf("Element %d has value %d\n", count, test_array[count]);
  
  free(test_array);
  test_array = NULL;
  return 0;
  
}
