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

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

问题描述

  INT * PI;
{
    INT AR [百万]
    诠释一个= 3,B = 4;
    AR [3] = A * B
    PI = AR;
} // AR被销毁
INT AR2 [] = {5,6,7,8,9};
字符* F =zcxzsdaaaaaaaaa;
性病::法院LT&;< PI [3]<<的std :: ENDL; //打印12

我有2个问题:


  1. 我听说栈只包含指向数据。如果是其中存储的数据?例如的char * A =BBBB; A - 放在栈,BBBB - 其他地方。在哪里?


  2. 不上面code正常工作意味着100万字节的内存泄漏?变量 AR 被破坏,但它指向的数据依然存在。我们不能用delete在这里,因为 AR 不是动态分配。



解决方案

  

我听说栈只包含数据指针


您没听错。堆栈包含实际的数据。但是,如果该数据是一个指针则这就是被存储。


  

如果是这样其中存储的数据?例如字符* A =BBBB一个 - 放在栈,BBBB - 其他地方。在哪里?


A (指针)是存储在堆栈上。实际的字符串BBBB存储在可执行的固定部分。


  

犯规code以上的正常工作意味着100万字节的内存泄漏?可变AR被破坏的数据,但它指出,仍然存在。我们不能用delete在这里,因为AR没有动态分配。


没有,有数组和指针阵列之间的差异。 AR (全百万字节)将被存储在堆栈上。这是从字符常量* AR =...百万字符......不同; 。由于 AR 是在栈上,它会被释放自动。

 字符常量* A =ABCDE; // a是在栈上,指向ABCDE别的地方。
字符常量B〔6] =ABCDE; // * B的所有*是在栈中,所有6个字节

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

最后说明:虽然基本上所有的现代计算机体系结构使用调用堆栈中,C ++标准没有提到这一点。请注意,人们往往会说,一个变量是堆栈,但​​它可能实际上只是生活在一个寄存器中。例如,如果你的编译code,变量 PI 可能不会永远接触到纸堆,它可能会仅仅停留在一个登记册的持续时间功能,因为将要堆叠相对昂贵(相对于寄存器)。

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

I have 2 questions:

  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?

  2. 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

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

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

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

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.

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

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.

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天全站免登陆