删除数组。 [英] Deleting the Array.

查看:98
本文介绍了删除数组。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我编码如下。


int * x = new int [10];

int * y = x;

............

............. >

del [] y;

x = NULL;


当我们释放数组时,它应释放所有内存位置

对应所有元素。

编译器存储数组元素的数量,并相应地释放

内存。


所以如果我们使用del [] y;它知道要删除多少个位置吗?

Becuae y只是x的副本。

我不知道如何测试这个案例。请帮帮我。


谢谢

Sabiyur

Hi All,
I am coding as below.

int *x = new int[10];
int * y= x;
............
.............

del [] y;
x=NULL;

When we are freeing the array, it should free the all memory locations
corresponding to all elements.
The compiler stores the number of elements of array, and releases the
memory accordingly.

So If we use del [] y; Does it knows how many locations to delete?
Becuae y is just copy of x.
I don''t know how to test this case. Please help me.

Thanks
Sabiyur

推荐答案

Sabiyur写道:
Sabiyur wrote:

大家好,

我编码如下。


int * x = new int [10];

int * y = x;

...........

............


del [] y;

x = NULL;


当我们释放数组时,它应释放所有元素对应的所有内存位置



编译器存储数组元素的数量,并释放

相应的内存。


所以如果我们使用del [] y;它知道要删除多少个位置吗?

Becuae y只是x的副本。

我不知道如何测试这个案例。请帮我。
Hi All,
I am coding as below.

int *x = new int[10];
int * y= x;
...........
............

del [] y;
x=NULL;

When we are freeing the array, it should free the all memory locations
corresponding to all elements.
The compiler stores the number of elements of array, and releases the
memory accordingly.

So If we use del [] y; Does it knows how many locations to delete?
Becuae y is just copy of x.
I don''t know how to test this case. Please help me.



是的,确实如此。 x和y都只是值。该指针后面的数组中的元素数量是实现业务。它如何
表示指针值的数字完全取决于它。您可以根据需要多次复制该值,前提是您最终使用''删除[]''释放内存。


V

-

请在通过电子邮件回复时删除资金''A'

我请不要回复最热门的回复,请不要问

Yes, it does. Both ''x'' and ''y'' are just values. The number of elements
in the array behind that pointer is the implementation business. How it
figures that number from the pointer value is up to it entirely. You can
copy the value as many times as you wish, provided that you eventually
use ''delete[]'' to free the memory.

V
--
Please remove capital ''A''s when replying by e-mail
I do not respond to top-posted replies, please don''t ask




Sabiyur写道:

Sabiyur wrote:

大家好,

我编码如下。


int * x = new int [10];

int * y = x;

...........

............


del [] y;

x = NULL;


当我们释放数组时,它应该释放所有内存位置

对应所有元素。

编译器存储数组元素的数量,并相应地释放

内存。 />

所以如果我们使用del [] y;它知道要删除多少个位置?
Hi All,
I am coding as below.

int *x = new int[10];
int * y= x;
...........
............

del [] y;
x=NULL;

When we are freeing the array, it should free the all memory locations
corresponding to all elements.
The compiler stores the number of elements of array, and releases the
memory accordingly.

So If we use del [] y; Does it knows how many locations to delete?



是的,但不要接受我的话 - 用假的

级别来证明它。

如果你添加一个副本ctor和op =,那个假人就不再是假人了:

有用的调试技术。


#include< iostream>


class A

{

public:

A(){std :: cout<< " A()\\\
英寸; }

~A(){std :: cout<< "〜A()\\\
英寸; }

};


int main()

{

A * p_a = new A [5];

A * p_b = p_a;

删除[] p_b;

}


你可能感兴趣的其他东西:


#include< boost / shared_array.hpp>


int main()

{

boost :: shared_array<一个sp_a(新的A [5]);

}

Yes it does, but don''t take my word for it - proove it with a dummy
class.
If you add a copy ctor and op=, that dummy is no dummy anymore: a
usefull debugging technique.

#include <iostream>

class A
{
public:
A() { std::cout << "A()\n"; }
~A() { std::cout << "~A()\n"; }
};

int main()
{
A* p_a = new A[5];
A* p_b = p_a;
delete [] p_b;
}

Something else that might interest you:

#include <boost/shared_array.hpp>

int main()
{
boost::shared_array< A sp_a(new A[5]);
}


你可以避免许多与这种数组有关的问题使用

就像STL一样。包装这种数组的类正在管理

所有类型的低级数据。记忆管理,所以你不需要花时间来讨论这个问题......

You can avoid many problems relating to this kind of arrays by using
something like STL. Classes wrapping this kind of arrays are managing
all kind of "low level" memory managment so you needn''t invest time to
think about it...


这篇关于删除数组。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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