memory-related perils and pitfalls Flashcards
second half of the advanced memory allocation slides. i put the first half in with basic. tbh im sort of assuming that hw3 was more than enough studying on these topics. otherwise if you need more help just reread chap 9.9 imo.
1
Q
do not deference bad pointers
A
int x;
scanf(“%d”, x);
BAD ^ use &x
2
Q
do not assume un-initialized memory is 0
A
just set it equal to smth
3
Q
overwriting memory
A
make sure you are using what you malloc. do not fgets more than your buffer can handle.
4
Q
proper pointer arithmetic
A
int *p = 5;
*p is value
p is pointer
*p + 5 = 10
p + 5 = you dont sleep tonight
5
Q
remember: local variables die after function call. do not return their addresses!
A
remember!
6
Q
malloc free stuff
A
dont free the same thing twice
dont reference a freed block
dont forget to free stuff before returning, or whenever ur done w it
free the entire struct not just the struct itself, but its children too