File: wrong3.c

package info (click to toggle)
duma 2.5.21-11
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,460 kB
  • sloc: ansic: 5,512; cpp: 2,205; makefile: 441; javascript: 191; sh: 129
file content (23 lines) | stat: -rw-r--r-- 480 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <stdio.h>

int main() {
  printf("Hello world!\n");

  int* pI = (int*)malloc(sizeof(int));
  int j;
  printf("Now reading uninitialized memory\n");
  j = *pI+2;
  printf("Did you notice? (value was %i)\n",j);
  free(pI);
  printf("(No memory leak here)\n");

  int* pJ;
  printf("Now writing to uninitialized pointer\n");
  *pJ = j;
  printf("Did you notice?\n");

  // valgrind reports 8, but that's ok
  printf("There should be 2 errors in this run\n");
  return 0;

}