Lecture 5 Flashcards

Memory Allocation

1
Q

what are the sections of a program’s memory?

A

stack, heap, read and write data segment, read only data segment

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

are a program’s sections in memory shared with any other program running?

A

no, each program gets its own stack, heap, read and write data segment, and read only data segment

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

what kind of memory are the read & write and read only data segments?

A

static memory

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

what 3 types of data does the read only segment store?

A

machine instructions (aka code), constant global variables, and string literals

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

what 2 types of data does the read and write segment store?

A

global and static variables

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

what is the lifetime of the variables stored in the read only and read & write segments?

A

1) created when program starts
- stored in these
segments
2) destroyed when program completes
- exits function normally or
abnormally

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

const double p = 3.14;
double area = 0;

int main() {
static int rad = 10;
area = p * rad;
printf(“%.2f\n”, area);
return 0;
}

what variables are stored in the read only segment?

A

p and %.2f
(const global and string literal)

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

const double p = 3.14;
double area = 0;

int main() {
static int rad = 10;
area = p * rad;
printf(“%.2f\n”, area);
return 0;
}

what variables are stored in the read and write segment?

A

area and rad
(global and static)

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

what does static do to a variable when defining it?

A

gives the variable global scope

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

what does const do to a variable when defining it?

A

constant variable –> read only
bc cannot change value
- only can access it or “get” it

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

when is heap memory allocated?

A

allocated using a function call during code execution
ex)
malloc()
calloc()
realloc()

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

when is heap memory deallocated?

A

deallocated using a function call during execution
ex)
free()

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

what happens when malloc() is called?

A

1) system goes to heap segment in memory that is assigned to this program
2) OS will try to allocate that amount of bytes in heap
- if space is allocated: returns
pointer that points @ the address
in heap of where the allocated
memory starts (*no initialization
in memory –> leftover values
stored there will fill space)

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

how does malloc() allocate the correct amount of bytes?

A

uses sizeof() to help calculate the number of bytes that we want malloc() to allocate
ex) malloc(sizeof(int))

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

int* p = (int*)malloc(sizeof(int));

what is happening here?

A

based on the sizeof function calculation, malloc will find 4 bytes in the heap to store this int and return the pointer that points at that location where the int is stored in the heap

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

what happens when free() is called?

A

1) the addresses originally stored that the pointer points to are now unallocated
- “freed” memory
the memory is given back to the program so you can use it elsewhere

17
Q

why does free() have a pointer as an argument?

A

bc have to use pointer malloc returns to know where the address starts in the memory you want to deallocate

18
Q

is stack dynamic or static

A

static

19
Q

is heap dynamic or static

A

dynamic