Module 13: C Language Dynamic Memory Flashcards

1
Q

dynamic

A

means “at run-time” (not “at compile-time”); compiler does not have enough information to make a final decition

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

size_t

A

a type_def type for an unsigned int

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

void* malloc(size_t size)

A

returns a pointer to a region on the heap of size bytes. if it’s successful, space is contiguous. if a heap can’t fulfill request, returns NULL, think of it as creating a reservation for a region of memory that only you can write to, until you call free() and release that reservation

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

void free(void* ptr)

A

clears the reservation for address pointed to by ptr and for all the rows initially reserved by the corresponding call to malloc(). example: you ask malloc() for 6 rows. when you call free, it will release the reservation on those six rows

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

sizeof()

A

an operator in C that will compute the size of the operand; works on any data type, even structs

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

memory leak

A

memory on heap you no longer have a reference to. example: int* my_pointer = malloc(4). my_pointer is a reference to the memory you have reserved. but if you then alter it: my_pointer = NULL, you have lost the only reference you had to your reservation on the heap. now you can never return to that region or even release it using free()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

linked list

A

a type of data structure itself, the purpose of which is to “link” groups of data together. we call the “groups” nodes in the context of a linked list

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

declaration

A

when you describe something to the compiler but don’t actually ask for space for it

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

header file

A

must end in .h but can only contain “declarations” and not “definitions”

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

steps for malloc():

A

char str;
str = (char
) malloc(10);
strcpy(str, “OMCIT593”);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

steps for free()

A

remember: each call to malloc() requires a corresponding call to free()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

advantages of linked lists

A
  • growth of the list can be dynamic (occur while programming is running)
  • make efficient use of memory
  • shrinking/reordering our list can also be dynamic
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

disadvantages of linked lists

A

pointer and memory management are tricky

more difficult to find things, i.e., traverse the list

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

memory leaks

A

most common cause:

  • you were done with memory you had allocated
  • then you overwrote the last pointer that pointed to it
  • but you forgot to call free() on it first

block is gone (cannot be reallocated) for the duration of the program

if you do this repeatedly, your program won’t have heap anymore!

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

garbage collection

A

helps avoid leaks while saving you from having to call free()
“reclaims” everything in heap that is not pointed to
C does not have this (Java does!)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

“buffer overflows”

A

example: ask for 10 bytes but write 11
bad to do in general, but especially bad in the heap
C does not check for this, but Java does

17
Q

Remember to check the return value of malloc()

A

malloc() is allowed to return NULL (out of memory)

should check for NULL after every call to malloc()

18
Q

Passing a pointer to the middle of allocated region to free()

A

free() won’t be able to find the block