堆和堆栈分配 [英] Heap vs Stack allocation

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

问题描述



例如,我有一个在堆栈上分配对象和在堆栈上分配的主题,以及如何调用delete()类矢量。我想建立一个数组。



我可以这样做

  Vector ** v = new Vector * [100]; //创建一个100个指针到100个Vector对象的数组

除了指针地址)在堆上?
所以要释放内存我需要:

  for(int i = 0; i <100; + i)
{
delete(v [i]);
}
delete(v);

 code> delete(v); 

足够吗?



  Vector * v = Vector [100]; 

在这种情况下会发生什么?分配在哪里发生?堆或堆?
我仍​​然需要调用

  delete(v); 

但这不是全部问题,对不起长期邮件。



例如:

  class Vector 
{
int x,y,z ;
}

Vector * v = new Vector();

x,y,z分配?堆或堆叠?



或如何:

  
{
int items [10];
}

Vector2 * v2 = new Vector2();

项目[10]分配在哪里?
如何删除v2?我需要自定义析构函数吗?



最后但同样重要的是:

 code> class Vector3 
{
int * items;
}

Vector3 v3 = Vector3();

项指针存储在哪里?堆或堆栈?如何删除此?



感谢和抱歉的长问题。

解决方案

我会从头开始开始...

  Vector ** v = new Vector * [100]; 

在堆上为Vector类型的对象分配100个指针的数组
返回一个



删除此100个点的数组:

  delete [] v; 

(使用 delete operator- delete 用于单个已分配对象, delete [] 用于数组)



下一个情况(我假设你的意思是 new Vector [100]

 code> Vector * v = new Vector [100]; 

向量在堆上,并得到一个指向其开始位置的指针v。
删除此数组:

  delete [ ] v; 

下一页...

  class Vector 
{
int x,y,z;
}

Vector * v = new Vector

这会在堆上分配一个Vector类的对象,并给你一个指针来跟踪它。因为你在堆上分配了整个对象,x,y和z都在堆上分配。



删除它:

  delete v; 


class Vector2
{
int items [10];
}

Vector2 * v2 = new Vector2();

这个有点棘手,但我会推理出来...



类是蓝图。你没有分配任何内存,直到你以某种方式实例化类,在这种情况下在堆上。因为类是一个蓝图,所以在堆上创建一个 Vector2 类的对象之前,不能分配 items 。我想我们可以合理地推断 items 是这样分配在堆上的。



使用

  delete v2; 

最后:

  class Vector3 
{
int * items;
}

Vector3 v3 = Vector3();

您在堆栈上分配了所有的Vector3类,其中的指针也被分配。堆没有任何东西,所以不要删除它。


I am alittle bit confused on topic of allocating objects on heap vs allocating on stack, and when and how delete() should be called.

For example I have class Vector. I would like to make an array of these.

I could either do this

Vector** v = new Vector*[100]; //create an array of 100 pointers to 100 Vector objects

This as I understand would allocate everything (well except pointer addresses) on a heap? So to free memory I would need to:

for (int i = 0; i < 100; ++i)
{
   delete(v[i]);
}
delete(v);

OR just

delete(v);

is enough?

Now another example:

Vector* v = Vector[100];

Whats happening in this case? Where does allocation happen? Heap or stack? Do I still need to call

delete(v);

But this is not all question, sorry for long post..

example:

class Vector
{
  int x, y, z;
}

Vector* v = new Vector();

wheres x, y, z allocated? Heap or stack?

or how about this:

class Vector2
{
   int items[10];
}

Vector2* v2 = new Vector2();

where are items[10] allocated? How do I delete v2? Do i need custom destructor?

Also last but not least how about this one:

class Vector3
{
   int* items;
}

Vector3 v3 = Vector3();

Where is items pointer stored? heap or stack? How do I delete this?

Thanks and sorry for long question. Ive been having trouble with this stuff for long time and couldnt really find any complete explanation on line.

解决方案

I'll start from the beginning...

Vector** v = new Vector*[100];

Allocates an array of 100 pointers to objects of type Vector on the heap It returns one pointer- v - the you can use to keep track of this array of pointers.

Delete this array of 100 points with:

delete[] v;

(Use the delete operator- delete for a single allocated object, delete[] for an array)

Next case (I'm assuming you mean new Vector[100]:

Vector* v = new Vector[100];

You allocated an array of 100 Vectors on the heap and got a pointer to its start location- v. Delete this array with:

delete[] v;

Next...

class Vector
{
  int x, y, z;
}

Vector* v = new Vector();

This allocates an object of class Vector on the heap and gives you a pointer to keep track of it. Because you allocated the entire object on the heap, x, y, and z are all allocated on the heap.

Delete it with:

delete v;


class Vector2
{
   int items[10];
}

Vector2* v2 = new Vector2();

This one is a bit trickier but I'm going to reason it out...

Classes are blueprints. You haven't allocated any memory at all until you instantiate the class somehow, in this case on the heap. Because the class is a blueprint, items could not have been allocated until you created an object of class Vector2 on the heap. I think we can reasonably infer that items is thus allocated on the heap.

Delete v2 with:

delete v2;

And finally:

class Vector3
{
   int* items;
}

Vector3 v3 = Vector3();

You allocated all of class Vector3 on the stack, the pointer inside of it items is also allocated thus. Nothing went on the heap, so don't delete it.

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

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