C ++中的内存分配 [英] Memory allocation in C++

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

问题描述

我对C ++中的内存分配(如Const数据区,堆栈,堆,Freestore,堆和全局/静态区域)感到困惑。我想了解下面片段中的内存分配模式。任何人都可以帮助我理解这一点。
如果除了示例中提到的变量类型之外还有任何其他东西有助于更好地理解这个概念,请改变示例。

  class FooBar 
{
int n; //存储在堆栈?

public:

int pubVar; //存储在堆栈?

void foo(int param)//存储在堆栈中的param
{
int * pp = new int; // int是在堆上分配的。
n = param;
static int nStat; //存储在内存的静态区域
int nLoc; //存储在堆栈?
string str =mystring; //存储在堆栈?
..
if(CONDITION)
{
static int nSIf; //存储在内存的静态区域
int loopvar; //存储在堆栈中
..
}
}
}

int main(int)
{
Foobar bar ; // bar存储在堆栈中?还是其中的一部分?

Foobar * pBar; // pBar存储在堆栈

pBar = new Foobar(); //对象是在堆中创建的吗?对象的什么部分存储在堆上

}

编辑:令人困惑的是,如果 pBar = new Foobar(); 将对象存储在堆上, int nLoc; int pubVar; 是存储在堆栈中的对象的组件?听起来与我矛盾。不应该 pubvar pBar 的生命周期是否相同?

解决方案

堆和堆栈是过时的,不准确和混乱的存储期限相关的术语。



自动存储持续时间是什么傻的人称之为堆栈对象。它们是你将在函数内定义为局部变量的那些。



具有动态存储持续时间的对象是您在免费存储区上创建的对象帮助关键字 new (或者,如果你傻, malloc ),然后随时销毁使用关键字 delete (或如果你傻,免费)。



还有一些具有静态存储持续时间的对象,它们受到各种奇怪的初始化顺序和事物规则的约束。我们倾向于不使用它们在惯用的C ++中尽可能多地帮助它。



对于代码示例中的特定变量,您的注释都是准确的,



术语堆和堆栈是过时的,涉及到当最流行的运行时库使用这些数据结构来存储分别动态和自动分配的对象时(静态分配的对象适合不属于这两种类型)。



这些天并不总是真的,它当然不是由C ++标准强制的,它不关心存储的地方。它只关心如何创建和销毁它们,以及他们生活多久。


I am confused about the memory allocation in C++ in terms of the memory areas such as Const data area, Stack, Heap, Freestore, Heap and Global/Static area. I would like to understand the memory allocation pattern in the following snippet. Can anyone help me to understand this. If there any thing more apart from the variable types mentioned in the example to help understand the concept better please alter the example.

class FooBar
{
      int n; //Stored in stack?

      public:

          int pubVar; //stored in stack?

          void foo(int param)  //param stored in stack
          {
                int *pp = new int; //int is allocated on heap.
                n = param;
                static int nStat;  //Stored in static area of memory
                int nLoc;          //stored in stack?
                string str = "mystring"; //stored in stack?
                ..
                if(CONDITION)
                {
                    static int nSIf; //stored in static area of memory
                    int loopvar;     //stored in stack
                    ..
                }
          }
}

int main(int)
{
     Foobar bar;    //bar stored in stack? or a part of it?

     Foobar *pBar;  //pBar is stored in stack

     pBar = new Foobar();  //the object is created in heap?  What part of the object is stored on heap

}

EDIT:
What confuses me is, if pBar = new Foobar(); stores the object on the heap, how come int nLoc; and int pubVar;, that are components of the object stored on stack? Sounds contradictory to me. Shouldn't the lifetime of pubvar and pBar be the same?

解决方案

"Heap" and "stack" are outmoded, inaccurate and confusing terms relating to storage duration.

Objects with "automatic storage duration" are what silly people call "stack objects". They're the ones that you will define inside a function as "local variables". They go out of scope when their enclosing block ends.

Objects with "dynamic storage duration" are those that you create on the free store with the aid of the keyword new (or, if you're silly, malloc), and then destroy whenever you like with the keyword delete (or, if you're silly, free).

There are also objects with "static storage duration" that are subject to all sorts of bizarre rules of initialisation order and things. We tend not to use them in idiomatic C++ as much as we can help it.

As for the specific variables in your code example, your comments are all accurate, despite the failure in terminology.

Addendum:

The terms "heap" and "stack" are outdated, relating to back when the most popular runtime libraries used these data structures to store objects which were dynamically- and automatically-allocated, respectively (statically-allocated objects fit into neither category, incidentally).

These days that is not always true, and it's certainly not mandated by the C++ standard, which does not care where things are stored. It only cares about how they are created and destroyed, and about how long they live.

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

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