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 ofplayer
.– Pedro H. N. Vieira
@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.
– Zeero
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.
– Maniero