04-memory_allocations.c 570 Bytes
Newer Older
Xavier Besseron's avatar
PS 2021  
Xavier Besseron committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#include <stdio.h>
#include <stdlib.h>

// Notes:
//
//  Add -Dfailedalloc -Ddoublefree -Dfreenonalloc or -Ddoublealloc to compile only the relevant part


int main(int argc, char** argv)
{

#ifdef failedalloc
  short SIZE = 1111;
  double *array = malloc( sizeof(double) * -SIZE );
  array[0] = 2.0;
  free(array);
#endif

#ifdef doublefree
  int *p = malloc( 2 * sizeof(int) );
  free(p);
  free(p);
#endif

#ifdef freenonalloc
  double d[100];
  free(d);
#endif

#ifdef doublealloc
  void *p;
  p = malloc( 100 );
  p = malloc( 10 );
  free(p);
#endif

  return 0;
}