mirror of
https://github.com/ebrandi/FDD-book.git
synced 2026-12-06 00:23: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
33 lines
558 B
C
33 lines
558 B
C
/*
|
|
* lab1_fixed.c - corrected version of lab1_crash.c.
|
|
*
|
|
* Allocates memory for a struct data on the heap, checks for
|
|
* allocation failure, writes into the structure safely, prints the
|
|
* value, and frees the memory before exiting.
|
|
*/
|
|
|
|
#include <stdio.h>
|
|
#include <stdlib.h> /* malloc, free */
|
|
|
|
struct data {
|
|
int value;
|
|
};
|
|
|
|
int
|
|
main(void)
|
|
{
|
|
struct data *ptr;
|
|
|
|
ptr = malloc(sizeof(struct data));
|
|
if (ptr == NULL) {
|
|
printf("Allocation failed!\n");
|
|
return (1);
|
|
}
|
|
|
|
ptr->value = 42;
|
|
printf("Value: %d\n", ptr->value);
|
|
|
|
free(ptr);
|
|
|
|
return (0);
|
|
}
|