Use extra space in addition to that reserved by "malloc"

Asked

Viewed 80 times

1

When we request an amount of memory from the system and use much more than requested, what happens? I did this test and the code here compiled normally.

At first it worked as it should, I wanted to know what this implies, I believe that this is not something you do, but for educational purposes I wanted to understand about.

int main(){
    player *p = (player*) malloc(5 * sizeof (player));

    for (int x=0;x<15;x++){
        p[x].id = 10 + x;

       std::cout << "Endereço: " << p + x<< std::endl;
    }

    for (int x=0;x<15;x++)
         std::cout << "Endereço: " << p + x << ", Valor: " << p[x].id << " ou " << (p + x)->id << std::endl;

    return 0;
}
  • The code is incomplete. The second for, for example, does not have the keys {}; there is no statement of player.

  • @Pedroh.N.Vieira, For, If, Else, etc. are flow control commands that execute only the next command, when we have to execute only one command, there is no need for keys, what happens when we want more than one command to be executed as a result of a flow deviation is that the next command is a block, a block, that block is considered the next command and therefore it will be executed, so no problem there. And given the context and the question, it is not necessary to declare player.

  • Did any of the answers solve your question? Do you think you can accept one of them? Check out the [tour] how to do this, if you haven’t already. You would help the community by identifying what was the best solution for you. You can accept only one of them. But you can vote on any question or answer you find useful on the entire site.

2 answers

2

when we ask for an amount of memory to the system and use much more than requested what happens?

You write in an area not reserved for this and go over something else, obviously creating serious problems for the application since the value that was there is lost. The worst thing that can happen to you is that it works, because it’s wrong, but it doesn’t seem to work. That’s how a lot of system invasions occur.

In this case there was no problem because there is nothing else in the memory, but no real code is like this.

In the same way that it did not release memory, in this case it does not matter, but in real code there would possibly be problems.

Working is different than being right.

Carro Fiat 147 todo detonado andando pelas ruas

It’s not usually used malloc() in C++.

1

When we use much more than the memory we allocate, simply the Operating System will not know that what is left belongs to your program and any other process can use this space, then in the middle of running the program the data that is beyond what you have allocated can be modified and turned into garbage. On Windows usually does not give compilation problem, but using a memory analysis tool like Valgrind, you will notice many mistakes.

Allocating Memory means telling the Operating System: "Protect a memory space for me sequentially, and give me the address of the beginning of this block that you allocated, because I already know the size and I can manually manipulate, just protect this space so that no other process uses this memory until my process finishes or releases it."

Free Memory is the opposite, you don’t erase the data that is there, even, knowing the address, if no other process modified the data, it is possible that another process can access the data that was stored at the address created by another process, and this would be considered garbage for other processes, while in the context of its execution it serves some purpose.

Releasing Memory is saying to the Operating System: "Now you can allow any process to use the memory space that starts at the X address, size N"

Browser other questions tagged

You are not signed in. Login or sign up in order to post.