FDD-book/examples/part-01/ch04-a-first-look-at-the-c-programming-language/lab-memory-crash/lab1_crash.c
Edson Brandi 6e6dac94fe Add book companion files under examples/
Organize companion code and materials by book part:

  part-01/      Foundations
  part-02/      Kernel & driver fundamentals
  part-03/      Character & block devices
  part-04/      Networking & storage
  part-05/      Buses & advanced topics
  part-06/      Debugging & performance
  part-07/      Real-world drivers
  appendices/   Supplementary material
  README.md     Overview and build instructions
2026-04-25 16:51:00 +00:00

34 lines
779 B
C

/*
* lab1_crash.c - DELIBERATELY BROKEN.
*
* Companion for "Hands-On Lab 1: Crashing with an Uninitialized
* Pointer" in Chapter 4. This program compiles cleanly but almost
* always segfaults at runtime because it dereferences an
* uninitialised pointer. That is the whole point of the exercise.
*
* Compare with lab1_fixed.c in this directory for the correct
* version using malloc().
*/
#include <stdio.h>
struct data {
int value;
};
int
main(void)
{
struct data *ptr; /* declared but not initialised */
/*
* At this point, 'ptr' points to whatever bytes happened to be
* on the stack when main() started. Writing through it is
* undefined behaviour and will very likely crash.
*/
ptr->value = 42;
printf("Value: %d\n", ptr->value);
return (0);
}