Memory Management Flashcards

1
Q

What is layout of x86 / x86_74 memory structure (top to bottom)

A
  1. Code/text segment
  2. Initialised data segment
  3. Uninitialized data segment
  4. Heap / Stack
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What is the in the text/code segment?

A

Binary instruction set of the program (what the program does). Read only.

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

What is in the initialised data segment?

A
  • Where you store Global + static variables with non-zero values. Values are copied from the text/code segment
  • Initiated before program execution
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What is in the Uninitialized data segment (.bss)?

A

Stores Global and static variables initialised to zero or not explicitly initialised in our code. Initiated before program execution.

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

What does the stack store?

A

Variables created and removed through execution. First in last out.

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

What does a stack pointer provide?

A

It points to the next available memory address.

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

What happens to the stack pointer when a variable is created?

A

When a variable is created, the stack pointer increases by the size of variable in bytes.

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

What happens to the stack when a variable is removed?

A

The stack pointer is decremented by the size of variables in bytes. The values aren’t cleared.

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

What happens to the stack when you enter a function?

A

When you enter a function it pushes a stack frame onto the stack.

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

What does a stack frame for functions contain?

A

A stack frame contains the return address where the function was called from, and any argument variables.

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

What happens to the stack when a function declares a variable?

A

The variable is pushed onto the stack during the execution of the function call (length of the scope).

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

What order are local variables removed from the stack on return of a function?

A

On returning from a function, local variables are removed in reverse order of their creation. (Push onto the top, pop off the top)

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

How big is the stack?

A

Size of the stack is fixed and known at execution time

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

When do stack overflows occur?

A
  • If we make a large number of variables or large variables within a function
  • Recursively call a function too many times.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What is the Heap?

A

Memory dynamically allocated by the programmer.

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

What are some guidelines for using the Heap?

A

Memory must be allocated and deallocated.

17
Q

What does the allocator function do?

A

Manage heap, reserving space and not double allocating it.

18
Q

What is the size of the heap?

A

Heap can use as much memory your system has available. The heap dynamically grows and shrinks, it starts at the end of the uninitialized data segment.

19
Q

What does the allocator function return?

A

Returns an address in memory reserve based on our request.

20
Q

When can allocation fail (Heap)?

A

When we run out of RAM

21
Q

What can you do with an address that has been deallocated?

A

Nothing, it cannot be used again. Read/write operations to that address are terminated.

22
Q

When can a SEGFAULT error be cause in memory managment?

A

When writing to an incorrect memory address.

23
Q

How do you manage memory in C (allocating/deallocating heap)?

A

functions:
- malloc()
- free()

24
Q

How do you manage memory in C++(allocating/deallocating heap)?

A

Keywords:
- new/new[]
- delete/delete[]

25
Q

What is the parameter for malloc()

A

(unsigned int size), the size bytes of memory on the heap.

26
Q

What doe malloc() return?

A

Returns the address to start of reserved memory.

27
Q

What does free(void *ptr) do?

A

Takes a pointer that points to something and removes the reservation of memory it was allocated with (frees memory).

28
Q

How do the stack and the heap differ?

A

Stack memory is a sort of memory allocation that the OS continuously manages and uses to store local variables in a FILO order. On the other hand, heap memory is a type of dynamic memory allocation used for storing objects and data structures that require a longer lifespan than stack memory.