C Language Dynamic Memory Flashcards

1
Q

Think about why we returned a pointer to the newly created array instead of creating a local array inside the function create_array() and returning that array instead. How is stack memory different from heap memory?

A

Since local variables are stored on the stack, while we could return a pointer to one of them, we cannot rely on that memory to still be allocated once the function returns and the stack frame is popped from the stack. However, heap memory is accessible if we have a pointer to it. The programmer can manage the life time of the heap memory by calling malloc() (allocating memory) or free() (deallocating memory) unlike the stack memory.

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

Dynamic

A

means “at run-time” (not “at compile-time”);
compiler does not have enough information to make final decision. Unlike atomic or static memory allocation e.g. int a = 0

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

size_t

A

a typedef type for an unsigned int

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

void* malloc(size_t size)

A

returns a pointer to a region on the heap of size bytes. If successful, space is contiguous. If 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
5
Q

void free(void* ptr)

A

clears reservation for address pointed to be 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
6 rows.
You have to call it as many times as you call malloc, to the same pointer malloc returned and in the right order

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

sizeof( )

A
an operator (not a function)  in C that will compute the size of its operand in bytes. Works on any data type, even structs.
Note: sizeof() may give different output according to machine
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
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
8
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
9
Q
struct Node
{
	int data;
	struct Node* next;
};

struct Node node1;

node1. data = 1;
node1. next = NULL;

struct Node node2;

node2. data = 2;
node2. next = NULL;
node1. next = node2;

Make “struct Node* curr “ point first to node1 and then next of node 1

A
//Pointer to a node
struct Node* curr = &node1;
//next returns pointer to Node
curr = curr->next;
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Declaration

A

when you describe something to the compiler but don’t actually ask for space for it. Don’t instantiate it.
Function no curly brackets
vs. Definition, when you instantiate something

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

Steps for malloc( ):

A
○ char *str;
○ str = (char *) malloc(10); 
// better: str = (char*)malloc(10*sizeof(char));
// check that molloc did not return NULL
○ strcpy(str, "OMCIT593");
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
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
13
Q

Advantages of linked lists:

A

○ Growth of the list can be dynamic (occur while the programming is running)
○ More 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
14
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
15
Q

Memory leaks - Most common causes

A

■ 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 duration of 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
16
Q

Garbage collection:

A

○ Helps avoid leaks while saving you from having to call free( )
○ “Reclaims” everything in heap region that is not pointed to
○ C does not have this (but Java does)

17
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)

18
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( )

19
Q

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

A

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

20
Q

why is lengthof() array not possible

A

Array only passed with address in function calls.
sizeof works as pointers must by typed and we can infer the size of array
However we cannot infer the number of elements in that array

21
Q

Heap

A

Region in data memory set aside for variables whose size we don’t know until program is running.
Use malloc to ask for space

22
Q

What happens with
int length = 2 ;
int int_array [length]

A

compile error.
Data i.e. the length = 2 is not commited to the stack until program is running. as we are asking for user to specify length. Hence, compiler does not know how large the int array shall be

23
Q

Stack and Global Region vs heap

A

Static, global: known at compile time

heap: accessible at runtime

24
Q

return type of malloc

A

void*

Hence need always to cast it

25
Q

What will happen to ptr after: free(ptr)

A

ptr on stack will still refer to the address in malloc. However you cannot access the data there anymore. The reservation/table has been released
heap memory lasts until you free it.
It is freed for someone else to overwrite it, as long as this does not happen, data still there

26
Q

What is wrong

#include 
typedef struct cust_struct {
int id ;
char* name ;
} customer ;
int main () {
customer my_cust ; 
my_cust.id = 1234 ;
strcpy (my_cust.name, “Tom”) ;
return 0 ;
}
A

strcpy (my_cust.name, “Tom”) ; does not work
name is a pointer that is pointing to nothing as it has never been initialized. i.e. my_cust.name = NULL
if name would be defined e.g. as char name[4] it would work, because “name” would have space allocated