自动内存分配如何在C ++中实际工作? [英] How does automatic memory allocation actually work in C++?

查看:91
本文介绍了自动内存分配如何在C ++中实际工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在C ++中,假设没有优化,以下两个程序最终是否使用相同的内存分配机器码?

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;
}

int main()
{
    int *p = new int;
    delete p;
}


推荐答案

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

几乎没有什么 - 只是几个指令来调整堆栈指针,

does almost nothing - just a couple of instructions to adjust the stack pointer, but

int main() 
{ 
    int *p = new int; 
    delete p; 
}

在堆上分配一块内存,然后释放它,这是一个很多的工作(我这里认真 - 堆分配不是一个微不足道的操作)。

allocates a block of memory on heap and then frees it, that's a whole lot of work (I'm serious here - heap allocation is not a trivial operation).

这篇关于自动内存分配如何在C ++中实际工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆