C++ new / new[], how is it allocating memory?

后端 未结 3 708
遥遥无期
遥遥无期 2021-01-26 17:09

I would like to now how those instructions are allocating memory.

For example what if I got code:

x = new int[5]; 
y = new int[5];

If t

3条回答
  •  孤街浪徒
    2021-01-26 17:51

    Each process has different segments associated which are divided among the process address space : 1) The text segment :: Where your code is placed 2) Stack Segment :: Process stack 3) Data Segment :: This is where memory by "new" is reserved. Besides that it also store initialized and uninitialized static data (bss etc).

    So , whenever you call a new function (which i guess uses malloc internally , but the new class makes it much safer to handle memory) it allocates the specified number of bytes in the data segment. Ofcourse the address you print while running the program is virtual and needs to be translated to physical address..but that is not our headache and the OS Memory management unit does that for us.

提交回复
热议问题