Memory Management Flashcards

1
Q

How malloc/new works on memory allocation?

A

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.

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

In this code:

char* s =  new char[357];
...
delete[] s;

How delete knows how many memory needs to release?

A

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]

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

Which command can you use to check the memory of a variable with dgb?

A

$ x <expr>
of examinate
or
$ p /f <expr>
of print

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

What are the segments of memory that a program uses?

A

Stack
Heap
Global/Static
Code

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

What information is allocated into the Code Segment?

A

All the progrma to execute, in machine language (asm)

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

What information is allocated into the Static/Global segment

A

All the static/globla variables.

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

What information is allocated into the Heap segment?

A

All the Dynamic memory used by the program.

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

What is the information allocated into the Stack Segment.

A

All the stack that the execution produces, in each stack element, is allocated the information of the local variables that the function uses.

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

what is the output of next code

char* ptr = "12345";
size_t size{0};
while(*ptr != '\0')
{
  size++;
	ptr++;
}
cout << size;
A

It prints the size of the string, which is 5;

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

From where Does the address of Heap Memory grow?

A

From a low addres and it goes upward direction.

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

From where Does the address of Stack Memory grow?

A

From a high addres to downward direction.

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

What are the characteistics of Stack Memory

A

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.

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

What is the instrucction in c++ to get the addres of a variable/entity?

A

std::addressof(expr)

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

What is the effect of allocating and deallocating memory on the Heap (Free Sotre)

A

The Heap Memory is fregmented.

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

What is the difference from:
new and new(ptr)
operators?

A

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.

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

What is the consecuence of creating an instance with ::new?

A

The destructor isn’t invoked at the end of the life of the context, it’s neccesary to invoke the destructor in as specifyc way:

auto* memory = std::malloc(sizeof(User));
auto* user = ::new(memory) User("MyName")
...
user->~User(); //Destructor is invoked
std::free(memory); //free memory
17
Q

on C++ What is RVO?

A

RVO (Return Value Optimization) is a compiler optimization. In some cases, it allows not to create a local object that is used as a return value.

Instead, the returned object is constructed in place of the function call, eliminatin unnecessary move/copy constructor call.

Besides, with C++17, RVO is no longer an optimization, but a rule that compilers must follow. This rule is applied even if a move/copy constructor has side effects.

18
Q

Which of these functions apply RVO and RVO?

a)
~~~

// A)
std::vector<int> GetVector()
{
return std::vector<int>(1'000'000, 1);
}
// B)
std::vector<int> GetVector()
{
std::vector<int> result(1'000'000, 1);
return result;
}</int></int></int></int>

void foo()
{
auto vect = GetVector();
}
~~~

A

A as RVO because the object is constructed in place

B as NRVO because the compiler pre-allocates and initializes the object which is supposed to receive the result of a function.

19
Q

What is NRVO?

A

Named Return Value Optimization, Instead of creating a local return object and then moving/copying it in place of the function call, this optimization instantly creates it in the right place.

The compiler gets a code like this:
~~~
std::vector<int> GetVector2()
{
std::vector<int> result(1'000'000, 1);
return result;
}</int></int>

auto vect = GetVector();
~~~

to be optimized like this:
~~~
void GetVector2(std::vector<int> *x)
{
new (x) std::vector<int>(1'000'000, 0);
}</int></int>

auto *x = static_cast<std::vector<int> *>(
alloca(sizeof(std::vector<int>)));
GetVector2(x);
....
delete x;
~~~</int></int>

20
Q

Givining the number 5206981, how is transfered in Big Endien Vs Little Endien, if the byte sequence is 4F-73-C5?

A

Big Endian means that the Byte less significan is set first and the most significan is sent at last:
4F-73-C5

Mean while, little endia is the opposite, Most significan Byte is transfered at the begin mean while the less significant at the end
C5-73-4F

https://www.freecodecamp.org/news/what-is-endianness-big-endian-vs-littl

21
Q

How can you check if the OS work with Little Endian or Big Endian?

A

Using a cast to check if the less significant byte is stored in the same position when a cast is performed:

int main()
{
    short int word = 0x0001;
    char *b = (char *)&word;
    cout << (b[0] ? "LITTLE_ENDIAN" : "BIG_ENDIAN");
    return 0;
}