Our Test Programs

The source snippets given in the following overviews the most basic problems with dynamic memory allocation and usage. These examples are closely related to the examples given in the lecture Programmieren 2.

Source file: error01.c, access without allocation

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

int main()
{
   long *ptr;
   fprintf(stderr, "uninitialized pointer: %p\n", ptr);
   fprintf(stderr, "trying to assign 15 \n");
   *ptr = 15; // Error: memory could not be allocated!
   return 0;
}

Source file: error02.c, access before allocation

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

int main()
{
   long *ptr;

   fprintf(stderr, "uninitialized pointer: %p\n", ptr);
   fprintf(stderr, "assign 15 to ptr[5]:\n");
   ptr[5] = 23;  // error .. access to uninitialized pointer 
   if (!(ptr = malloc(10 * sizeof(long))))
   {
      fprintf(stderr, "Error: memory could not be allocated!\n");
      exit(-1);
   }
   printf("%ld\n", ptr[5]);

   return 0;
}

Source file: error03.c, no validation of allocation

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

int main()
{
    long *ptr;
    unsigned long size = LONG_MAX * 1.5;

    ptr = malloc(size); //what happens when malloc returns 0?
    fprintf(stderr, "Pointer to malloc(size): %p\n", ptr);
    ptr[0] = 1;
    free(ptr);

    return 0;
}

Source file: error04.c, wrong size for allocation

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

int main()
{
   long *ptr, i;

   ptr = malloc(10); /* incomplete specification of memory size */
   fprintf(stderr, "malloc(10) pointer: %p\n", ptr);
   ptr = malloc(10 * sizeof(char)); /* incomplete specification */
   for (i = 0; i < 10; ++i) ptr[i] = 1;
   fprintf(stderr, "malloc(10 * sizeof(char)) pointer: %p\n", ptr);

   free(ptr);   
   return 0;
}

Source file: error05.c, access of uninitialized memory

#include <stdio.h>
#include <stdlib.h>
void printArray(long *feld, long len)
{
  long n;
  for(n = 0; n < len; n++)
    printf("%ld ", feld[n]);
  printf("\n");
}
int main()
{
  long *ma;
  long anzahl = 10;
  /* input of  number */
  if ((ma = malloc(anzahl * sizeof(long))) != 0)
  {
    fprintf(stderr, "Access to uninitialized array of size %ld at  %p\n", anzahl, ma);
    printArray(ma, anzahl); // Error: access without initialization!
    free(ma);
  }
  else
  {
    fprintf(stderr, "Error: memory could not be allocated!\n");
    exit(-1);
  }
  return 0;
}

Source file: error06_01.c, memory leak

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

int main()
{
   long *ptr1;
   long *ptr2;
   if (!(ptr1 = malloc(10 * sizeof(long))))
   {
      fprintf(stderr, "Error: memory could not be allocated!\n");
      exit(-1);
   }
   fprintf(stderr, "Pointer ptr1: %p   ptr2: %p\n", ptr1, ptr2);
   fprintf(stderr, "Assignment of ptr1 to ptr2  => value of ptr1 (%p) is lost\n", ptr1);

   ptr1 = ptr2;  /* memory leak */
   free(ptr1);
   return 0;
}

Source file: error06_02.c, memory leak

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

void foo()
{
  //...
  long *ptr;
  if(!(ptr = malloc(10 * sizeof(long))))
  {
     fprintf(stderr, "Error: memory could not be allocated!\n");
     exit(-1);
  }
  //...
  return; /* Loss of access to memory pointed to by ptr because of return */
}
int main()
{
  //...
  foo(); /* allocation of memory but inaccessible */
  //...
  return 0;
}

Source file: error07.c, access to already freeed memory

#include <stdio.h>
#include <stdlib.h>
int main()
{
  char *ptr;
  ptr = malloc(8*sizeof(char));
  fprintf(stderr, "Allocation of memory at %p (size %d)\n", ptr, 8 * sizeof(char));
  free(ptr);
  fprintf(stderr, "memory freeed\n");
  //... 
   /* Error: memory was already freeed */
  fprintf(stderr, "Access of already freeed memory at %p\n", ptr);
  *ptr = "hello";

  return 0;
}

Source file: error08.c, free and non-valid pointer

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

int main()
{
  char zeichen;
  char *puffer = &zeichen;

  fprintf(stderr, "Attempting to free pointer %p", puffer);
  free(puffer); /*Error by attempt to free variable */

  long zahlen[10];
  //...
  fprintf(stderr, "Attempting to free pointer  %p", zahlen);
  free(zahlen); /* Error by attempt to free static array */

  return 0;
}