How does automatic memory allocation actually work in C++?

后端 未结 5 1337

In C++, assuming no optimization, do the following two programs end up with the same memory allocation machine code?

int main()
{     
    int i;
    int *p;         


        
5条回答
  •  北荒
    北荒 (楼主)
    2020-12-09 00:14

    It sounds like you don't know about the stack and the heap. Your first example is just allocating some memory on the stack which will be deleted as soon as it goes out of scope. Memory on the heap which is obtained using malloc/new will stay around until you delete it using free/delete.

提交回复
热议问题