wrong3.c 480 B

1234567891011121314151617181920212223
  1. #include <stdio.h>
  2. int main() {
  3. printf("Hello world!\n");
  4. int* pI = (int*)malloc(sizeof(int));
  5. int j;
  6. printf("Now reading uninitialized memory\n");
  7. j = *pI+2;
  8. printf("Did you notice? (value was %i)\n",j);
  9. free(pI);
  10. printf("(No memory leak here)\n");
  11. int* pJ;
  12. printf("Now writing to uninitialized pointer\n");
  13. *pJ = j;
  14. printf("Did you notice?\n");
  15. // valgrind reports 8, but that's ok
  16. printf("There should be 2 errors in this run\n");
  17. return 0;
  18. }