Exam prep Flashcards
What are stack and heap in a program, and how are variables stored in them?
Stack is a region of memory used for temporary storage of variables in a program, where variables are pushed and popped in a Last In, First Out (LIFO) manner.
Heap, on the other hand, is a region of memory used for dynamic memory allocation, where variables are stored in a more flexible manner.
What does it mean that a variable is placed in a memory on the stack?
When a variable is placed in a memory on the stack, it means that the memory space for that variable is reserved during the compilation time, and the variable is automatically deallocated when it goes out of scope.
What does it mean that a variable is planed in a memory on the heap?
When a variable is placed in a memory on the heap, it means that the memory space for that variable is allocated during runtime, and the variable remains in memory until it is explicitly deallocated by the program.
How do we define and allocate memory for a variable of type int on the stack?
To define and allocate memory for a variable of type int on the stack, we can simply declare the variable within a function. For example: int x = 10;
How do we define and allocate memory for a variable of type in on the heap?
To define and allocate memory for a variable of type int on the heap, we need to use dynamic memory allocation functions such as malloc() or new. For example: int* x = new int(10);
What can we do with the allocated memory on the heap, that we cannot do with the memory on the stack?
We can perform dynamic memory allocation and deallocation on the heap, which allows us to allocate memory of arbitrary size during runtime. This is not possible on the stack, where memory allocation is fixed during compilation time.
Additionally, the memory allocated on the heap can be accessed from different parts of the program, whereas the memory on the stack is only accessible within the scope it was declared in.
int *x();
a) x is a pointer to a function that returns an int
b) x is a function that returns a pointer to an int
c) x is a variable of type int
d) x is a pointer to a pointer to a function that returns a variable of type int
b) x is a function that returns a pointer to an int
int ( * x [] ) ();
a) x is an array of pointers to functions that return int
b) x is a function which takes as an argument an array of pointers to variables of type int
c) x is a function that takes an array as an argument and returns a pointer to int
d) x is a pointer to a function that takes as an argument an array of integers
a) x is an array of pointers to functions that return int
char * ( * ( * x [ ] [8] ) ( ) ) [ ];
a) x is array of array of 8 pointers to a pointer to functions returning pointer to array of pointer to char
b) x is an array of 8 pointers to pointer to function returning pointer to array of pointer to char
c) x is array of array of 8 pointers to a function returning pointer to array of pointer to char
d) x is a function that takes as input an array of 8 pointers to pointer to an array and returns a pointer to array of pointer to char
a) x is array of array of 8 pointers to a pointer to functions returning pointer to array of pointer to char.
int ( * ( * x ) [ ] ) ( );
a) x is an array of pointers to functions that take no arguments and return pointers to int
b) x is a function that takes as argument an array of pointers to functions and return a pointer to int
c) x is a function that takes as an argument a pointer to an array of pointers and returns a pointer to int
d) x is a pointer to an array of pointers to functions returning an int
d) x is a pointer to an array of pointers to functions returning an int
char ( * * x ( ) ) [20];
a) x is an array of pointers to functions returning pointers to functions returning pointers to
char
b) x is a pointer to a function returning a pointer to an array of 20 elements of type char
c) x is a function returning a pointer to a pointer to an array of 20 elements of type char
d) x is an array of 20 pointers to functions returning char
b) x is a pointer to a function returning a pointer to an array of 20 elements of type char
Declare x as function returning pointer to pointer to array[20] of char
What is paging in computer systems?
Paging is a memory management scheme used to allocate and manage memory in a computer system. It divides the physical memory and virtual memory used by applications into fixed-size blocks called pages.
How does addressing of memory work in modern computer systems?
Addressing of memory is the process of mapping a logical address to a physical address.
In modern computer systems, this mapping is done through hardware components such as the Memory Management Unit (MMU).
What is a Memory Management Unit (MMU) and what is its role in memory management?
A Memory Management Unit (MMU) is a hardware component that maps virtual memory to physical memory in a computer system.
What is the process of mapping a virtual address to a physical address?
Address mapping involves translating a virtual address into a corresponding physical address. This is done by hardware components such as the MMU.
How is a physical address calculated from a virtual address?
To calculate the physical address from a virtual address, the MMU splits the virtual address into a page number and an offset, looks up the corresponding physical page number in a page table, and concatenates it with the offset.