Memory Allocation Flashcards

1
Q

Why use dynamic memory allocation rather then just initializing like this, “ int variable[100]”

A

The arrays capacity might not be known till runtime, so it might be too much or to little capacity.

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

What is the stack?

A

changes size continually, out of programmers control

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

Can you manage the Heap? How?

A

Yes, referenced through pointers with the following library,
#include <stdlib.h>
void *malloc(size_t size);
void free(void * ptr);
void *calloc(size_t nmeb, size_t size);
void *realloc(void *ptr, size_t size);</stdlib.h>

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

In python what happens when objects are no longer referenced

A

garbage collection

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

what does malloc function do?

A

allocates a block of memory of specified size, from the heap, its uninitialized.

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

if memory cant be allocated with malloc, what is returned?

A

NULL (the null pointer value)

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

what does malloc return?

A

returns a pointer to the allocated block.

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

what does the free() function do?

A

deallocates the block of memory pointed to by ptr.

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

when will free() work?

A

only if the ptr is a pointer previously retuned by malloc

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

when will free do nothing?

A

when the pointer you are freeing is NULL

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

What are the objectives of the lecture on memory allocation?

A

The objectives include understanding the limitations of automatic memory allocation, explaining how program data is stored in computer memory, reviewing memory management in Python, and introducing C functions to handle dynamic memory.

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

In imperative programming languages, where are a function’s parameters and local variables typically allocated?

A

They are allocated in an activation frame, which is automatically created when the function is called and deallocated when the function returns.

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

How is memory allocated for local variables in Python?

A

In Python, a local variable is allocated the first time its name is bound to an object.

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

In C, when are parameters and local variables allocated?

A

Parameters and local variables whose scope is the entire function are allocated when the function is called.

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

What are automatic variables in the context of memory allocation?

A

Automatic variables are local variables in functions that are allocated and deallocated automatically when the function is called and returns, respectively.

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

What is the potential problem with the lifetime of automatic variables?

A

The values stored in automatic variables are not retained when the function where the variable is declared returns.

17
Q

How does virtual memory work in computer systems?

A

Virtual memory allows the operating system to mimic non-existing memory blocks using a dedicated part of the hard drive, known as swap space or swap file.

18
Q

What are the two main parts of virtual memory?

A

The two main parts are Kernel Space, where the OS kernel resides and is unavailable to processes, and User Space, where processes stay, work, and interact, each having its own sandbox and unable to access other sandboxes.

19
Q

what should always be done after allocating memory with malloc?

A

use the assert function to verify that the pointer does not return NULL

20
Q

What does assert function do?

A

terminates the program if the condition given in parameters does not return true

21
Q

How to you allocate a struct?

A

typedef struct {
int x;
int y;
} point_t;

point_t *point;
point = malloc(sizeof(point_t));
assert(point != NULL);

22
Q

what should you always do after freeing a block?

A

set the pointer that you freed to NULL, this will avoid any issues with free in the future regarding that specific block.

23
Q

How do you allocate an array of 10 ints?

A

int *pa;
pa = malloc(10 * sizeof(int));
assert(pa != NULL);
// Access array elements by using []
// or pointer-plus-offset
pa[0] = 0; // *pa = 0;
*(pa + 1) = 0; // pa[1] = 0;

24
Q

write a function that allocates an array of integers
with a specified capacity, and returns a pointer to the first
element

A

int *alloc_array_of_ints(int capacity){
int *pa;
assert(capacity > 0);
pa = malloc(capacity * sizeof(int));
assert(pa != NULL);
return pa;
}

25
Q

What error may occur if free is passed a pointer to heap memory that was already deallocated by a previous call to free?

A

A “double free” error may occur, resulting in undefined behavior.

26
Q

How can the occurrence of a “double free” error be prevented?

A

After freeing a block of memory, it’s advisable to set the pointer to that block to NULL.

27
Q

What is a “dangling pointer” error in memory management?

A

A “dangling pointer” error occurs when a pointer still points to deallocated memory after being freed.

28
Q

What precaution should be taken to avoid using freed memory?

A

It’s important never to use memory that has been freed to avoid accessing dangling pointers

29
Q

What is one defensive programming practice to mitigate the risk of a runtime error after freeing memory?

A

After freeing a block of memory, setting the pointer to that block to NULL can help avoid potential runtime errors.

30
Q

How can you allocate an array of integers dynamically in C?

A

You can use malloc to allocate memory for the array, specifying the required size in bytes. For example: pa = malloc(10 * sizeof(int));.

31
Q

What is the purpose of the assert statement in dynamically allocating memory for an array?

A

The assert statement is used to ensure that memory allocation was successful. It causes the program to terminate if the condition specified is false.

32
Q

How can you allocate an array of integers with a specified capacity using a function?

A

You can write a function, such as alloc_array_of_ints, that takes the capacity as an argument, allocates memory for the array using malloc, and returns a pointer to the first element.

33
Q

What precaution is taken in the alloc_array_of_ints function to ensure proper memory allocation?

A

The function asserts that the capacity is greater than 0 and checks if memory allocation was successful before returning the pointer to the allocated memory.

34
Q

What does the sizeof operator yield in C programming?

A

The sizeof operator yields the amount of memory, in bytes, required to store an object of the type of its operand.

35
Q

What value does malloc return if it fails to allocate memory?

A

If malloc fails to allocate a block with the specified size, it returns NULL.

36
Q

Why is it important to check the pointer returned by malloc before dereferencing it?

A

It’s important to check the pointer returned by malloc before dereferencing it because if malloc fails and returns NULL, dereferencing a NULL pointer can lead to undefined behavior.

37
Q

What should a program do if malloc returns NULL?

A

A program should terminate if malloc returns NULL, unless it’s possible to recover. The assert macro provides a concise way to accomplish this.

38
Q

What is the purpose of the assert macro in C programming?

A

The assert macro is used to specify a condition that must be true for program execution to continue. If the condition evaluates to false (0), assert displays a descriptive error message and terminates the program.

39
Q
A