C++ allocates abnormally large amout memory for variables

后端 未结 9 780
名媛妹妹
名媛妹妹 2020-12-10 14:13

I recently got to know an integer takes 4 bytes from the memory.

First ran this code, and measured the memory usage:

int main()
{
   int *pointer;
         


        
9条回答
  •  难免孤独
    2020-12-10 14:45

    Two explanations:

    1. Dynamic memory allocation (on the heap) is not necessarily contiguous. When using new you perform dynamic allocation.
    2. If you are including debug symbols (-g compiler flag) your memory usage may be larger than expected.

提交回复
热议问题