如何为局部变量分配内存? [英] how is memory allocated for local variables?

查看:105
本文介绍了如何为局部变量分配内存?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

int* pi;
{
    int ar[1000000];
    int a =3,b=4;
    ar[3]=a*b
    pi=ar;
}//ar is destroyed
int ar2[]={5,6,7,8,9};
char* f="zcxzsdaaaaaaaaa";
std::cout<<pi[3]<<std::endl;// prints 12

我有两个问题:


  1. 我听说堆栈只包含指向数据的指针。如果是这样,数据存储在哪里?例如 char * a =bbbb; a - 放在堆栈上,bbbb - 其他地方。在哪里?

  1. I heard that the stack contains only pointers to data. And if so where is the data stored? For example char* a="bbbb"; a - placed on stack, "bbbb" - somewhere else. Where?

上述代码是否正常工作意味着1000000字节的内存泄漏?变量 ar 被销毁,但它指向的数据仍然存在。我们不能使用delete,因为 ar 不是动态分配的。

Doesn't the above code working correctly mean a memory leak of 1000000 bytes? Variable ar is destroyed but the data that it pointed to still exists. And we can't use delete here since ar is not dynamically allocated.


推荐答案


我听说堆栈只包含指向数据的指针

I heard that stack contain only pointers to data


$ b b

你听错了。堆栈包含实际数据。

You heard wrong. The stack contains the actual data. However, if that data is a pointer then that is what is stored.


如果是,数据存储在哪里?例如char * a =bbbb; a - 放在堆栈上,bbbb - 别的地方。在哪里?

And if so where is data stored? For example char* a="bbbb"; a - placed on stack, "bbbb" - somewhere else. Where?

是, a 。实际的字符串bbbb存储在可执行文件的固定部分。

Yes, a (the pointer) is stored on the stack. The actual string "bbbb" is stored in a fixed part of the executable.


不正确的代码上面工作正常意味着内存泄漏1000000字节?变量ar被破坏,但它指向的数据仍然存在。我们不能在这里使用delete,因为ar不是动态分配的。

Doesnt code above working correctly mean memory leak of 1000000 bytes? Variable ar is destroyed but data that it pointed to still exists. And we cant use delete here since ar is not dynamically allocated.

不,数组和数组指针之间有区别。 ar (整个1000000字节)将被存储在堆栈上。这不同于 char const * ar =... 1000000 chars ...; ar 在堆栈上,它会自动释放。

No, there is a difference between arrays and pointers to arrays. ar (the whole 1000000 bytes) will be stored on the stack. This is different from char const* ar = "... 1000000 chars ...";. As ar is on the stack, it will be "freed" automatically.

char const* a = "abcde"; // a is on the stack, pointing to "abcde" somewhere else.
char const b[6] = "abcde"; // *all* of b is on the stack, all 6 bytes

pi 指向的堆栈上的东西已不再存在。它可能在那里,当你运行代码,因为释放数据在堆栈不做任何东西对非调试版本的数据。这不是一个内存泄漏,你只是有一个无效的指针。

The problem in your code is that pi is pointing to something on the stack which is no longer there. It may well be there when you run the code because "freeing" data on the stack doesn't do anything to the data in non-debug builds. It's not a memory leak, you just have an invalid pointer.

最后一点:虽然基本上所有现代计算机体系结构使用调用堆栈,C ++标准没有提到的。注意,人们通常会说一个变量是在堆栈上,但它可能实际上只是住在一个寄存器。例如,如果你编译你的代码,变量 pi 可能不会触及堆栈,它可能只是在函数的持续时间保持在寄存器,因为去到堆栈是相对昂贵的(与寄存器相比)。

Final note: Although essentially all modern computer architectures make use of a call stack, the C++ standard makes no mention of it. Note that people will often say that a variable is "on the stack", but it may actually just live in a register. For example, if you compiled your code, the variable pi probably wouldn't ever touch the stack, it would likely just stay in a register for the duration of the function because going to the stack is relatively expensive (compared to registers).

这篇关于如何为局部变量分配内存?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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