FDD-book/examples/part-01/ch04-a-first-look-at-the-c-programming-language/lab-memory-crash/lab1_crash.c

35 lines
779 B
C
Raw Permalink Normal View History

/*
* 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);
}