mirror of
https://github.com/ebrandi/FDD-book.git
synced 2026-12-06 08:33:30 +00:00
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
34 lines
779 B
C
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);
|
|
}
|