Memory Management Flashcards
How malloc/new works on memory allocation?
According with the size of the memory requested, there will be one blocks of 2^n size wich fits the required size.
The size of 2^n is used to avoid fragment memory.
In this code:
char* s = new char[357]; ... delete[] s;
How delete knows how many memory needs to release?
s points to the address where the memory request is llocated, but there is another sector of memory immideatly before that addres, usually the same side of the word that OS uses, which indiicates the size of bytes that OS had assigned to in a previous new.
[Size of Bock][Data]
The addresss points to the begin of the [Data]
Which command can you use to check the memory of a variable with dgb?
$ x <expr>
of examinate
or$ p /f <expr>
of print
What are the segments of memory that a program uses?
Stack
Heap
Global/Static
Code
What information is allocated into the Code Segment?
All the progrma to execute, in machine language (asm)
What information is allocated into the Static/Global segment
All the static/globla variables.
What information is allocated into the Heap segment?
All the Dynamic memory used by the program.
What is the information allocated into the Stack Segment.
All the stack that the execution produces, in each stack element, is allocated the information of the local variables that the function uses.
what is the output of next code
char* ptr = "12345"; size_t size{0}; while(*ptr != '\0') { size++; ptr++; } cout << size;
It prints the size of the string, which is 5;
From where Does the address of Heap Memory grow?
From a low addres and it goes upward direction.
From where Does the address of Stack Memory grow?
From a high addres to downward direction.
What are the characteistics of Stack Memory
a) It never gets fragmented, it’s assignemt is contiguous.
b) It has a limit that raise an stack overflow if it is exceded.
c) It assignment is consecotive.
d) Each thread has it’s own stack memory.
e) Allocating memory in the stack is fast and efficient.
What is the instrucction in c++ to get the addres of a variable/entity?
std::addressof(expr)
What is the effect of allocating and deallocating memory on the Heap (Free Sotre)
The Heap Memory is fregmented.
What is the difference from:
new and new(ptr)
operators?
a) new allocate and initizalice the memory.
b) new(ptr) creates a new object from a previous block of memory allocated, like:
~~~
auto* memory = std::malloc(sizeof(User));
auto* user = ::new(memory) User(“MyName”)
~~~
This last one is well know as placement new operator.
> :: ensures is to use the global namespace and an overlaped operator new.