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

34 lines
558 B
C
Raw Permalink Normal View History

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