ECEC 201 · Week 5
Modern Computer: Von Neumann
Memory you ask for with malloc is yours to give back with free.
Week 5 of 10 · Das Frite
ECEC 201 · Week 5 · In class
Memory you ask for with malloc is yours to give back with free.
- A linked list is the first structure where forgetting that costs something.
- Valgrind reports every block you did not free.
- A test is a program that checks your code and fails loudly: write one with
assertthat inserts, finds, and removes nodes, and run it under valgrind too.
ECEC 201 · Week 5 · In class
A struct is laid out in memory with padding between members so each one is aligned.
offsetofprints where each member starts.- Compute the offsets by hand first, then reorder the members and watch the size change.
ECEC 201 · Week 5 · Lecture
Every malloc has one free
mallocreturns a block orNULL; check it before you use it.- Each block is freed exactly once: never zero times (a leak), never twice.
- After
free(p),pstill holds the old address. Using it is a use-after-free. valgrind --leak-check=full ./proglists each block still allocated at exit, with the line that allocated it.
ECEC 201 · Week 5 · Lecture
Freeing a list without reading freed memory
#include <stdlib.h>
struct node { struct node *next; char *text; };
void list_free(struct node *head)
{
while (head) {
struct node *next = head->next;
free(head->text);
free(head);
head = next;
}
}
ECEC 201 · Week 5 · Lecture
A test that fails loudly
#include <assert.h>
#include "list.h" /* your own list API */
int main(void)
{
struct node *list = NULL;
list = list_insert(list, "a");
assert(list_find(list, "a") != NULL);
list = list_remove(list, "a");
assert(list_find(list, "a") == NULL);
list_free(list);
return 0;
}
ECEC 201 · Week 5 · Lecture
Padding: members start on their alignment
struct mixed { char a; int b; char c; };
| Member | offsetof | Why |
|---|---|---|
a | 0 | the first member starts at 0 |
b | 4 | 3 padding bytes, so int starts on a multiple of 4 |
c | 8 | right after b |
The size rounds up to the largest member's alignment: 12 bytes here, with a 4-byte int.
ECEC 201 · Week 5 · Lab
Lab
- Build a linked list of structs from the lines of a text file, then free it, and write an `assert`-based test that inserts, finds, and removes nodes.
- Run the program and the test under valgrind until both report no leaks, and print `sizeof` and `offsetof` for each struct member. Second pass: reorder the members to remove padding, print the new size, and run the test again.
ECEC 201 · Week 5 · Lab
Lab (continued)
- Assemble the lab hand-in: The program and its test, the valgrind summary with no leaks, and the member offset table before and after reordering.
ECEC 201 · Week 5 · Lab
Hand in
The program and its test, the valgrind summary with no leaks, and the member offset table before and after reordering.
ECEC 201 · Week 5 · Homework
Homework
- Compute each member's offset and the struct's total size by hand, with the alignment rule from the lecture.
ECEC 201 · Week 5 · Homework
Turn in
The hand-computed offset table.
ECEC 201 · Week 5 · Read
Read
On the lab site: the modules do not grade heap use or struct layout. This week does.
ECEC 201 · Week 5 · Next
Week 6
A C file becomes a running program in steps: the preprocessor, the compiler, the assembler, the linker, and the loader.