06-uninitialized_value.c 839 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
39
40
41
42
43
44
45
46
47
48
49
50
#include <stdio.h>
#include <stdlib.h>

// Notes:
//
//  Add -Duninitstatic -Duninitdynamic or -Duninitnonalloc to compile only the relevant part



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

#ifdef uninitstatic
  double x,y;
  x = y + 2;
  printf("x = %f\n",x);
  printf("y = %f\n",y);

#endif

#ifdef uninitdynamic
  int size = 10;
  double *array = malloc( sizeof(double) * size );

  int i;
  for( i = 1 ; i < size ; i++ )
    array[i] = array[i-1];

  for( i = 0 ; i < size ; i++ )
    printf(" array[%i] = %f\n", i, array[i] );

  free(array);
#endif

#ifdef uninitnonalloc
  int size = 10;
  double *array1, *array2 = malloc( sizeof(double) * size );

  int i;
  for( i = 0 ; i < size ; i++ )
    array2[i] = array1[i];

  for( i = 0 ; i < size ; i++ )
    printf(" array2[%i] = %f\n", i, array2[i] );

  free(array2);
#endif

 return 0;
}