删除和删除[]之间有什么区别? [英] what's the difference between delete and delete[] ?

查看:151
本文介绍了删除和删除[]之间有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  int  main( int  argc, char  * argv [])
{
int * p = new int [ 10 ];
for int i = 0 ; i< 12 ; ++ i){
p [i] = i;
}
delete [] p;
return 0 ;
}





g ++引发错误:



< pre lang =text>*** glibc detect *** ./a.out:free():next size(fast):0x00000000019f9010 ***并退出





删除[]时VS2008无法退出[] a,我不知道后台发生了什么。



我有一些问题。

1.删除和删除[]之间的区别是什么?

2.使用delete []时,编译器如何知道数组的大小?

3.与示例一样,p有两个元素重载,编译器如何知道p的尾部并停止删除。



谢谢请注意。

解决方案

错误很明显,我想:只需更改



  //   for(int i = 0; i< 12; ++ i) 

for int i = 0 ;我< 10 ; ++ i)





或将数组大小更改为12.



1.删除和删除之间的区别[]



从标准(5.3.5 / 2):



在第一个替代(删除对象)中,delete的操作数的值应该是指向非数组对象的指针或指向表示此类对象的基类的子对象(1.8)的指针(第10条)。如果没有,行为是未定义的。



在第二个备选(删除数组)中,delete的操作数的值应该是由前一个引起的指针值数组new-expression。如果没有,行为是未定义的。



2,3。编译器在使用delete []时如何知道数组的大小?

编译器如何知道p的尾部并停止删除?



未指定大小信息的存储方式,因此每个编译器都可以在不同的位置实现它但是,常见的方法是在数组之前分配一个额外的块。也就是说,当你这样做时:



  int  * p =  new   int  [ 10 ]; 





它实际上分配了一个11个整数的数组,并将数组大小存储在第一个元素中


1。如果您使用 new 使用删除,如果您使用 new [] 然后使用删除[]



http://stackoverflow.com/questions/4255598/delete-vs-delete [ ^ ]



2.编译器如何知道编译器相关。



http://stackoverflow.com/questions/975792/how-does-delete-know-the-size-of-an-array [ ^ ]



3.见上文


int main(int argc, char* argv[])
{
	int* p = new int[10];
	for (int i = 0; i < 12; ++i) {
		p[i] = i;
	}
	delete []p;
	return 0;
}



g++ raise an error:

"*** glibc detected *** ./a.out: free(): invalid next size (fast): 0x00000000019f9010 ***" and exit



VS2008 can not exit when delete []a, I don't know what happened in background.

I have some questions.
1. What's the difference between delete and delete[] ?
2. How does the compiler know the size of a array when using delete [] ?
3. Like the example, p has two elements overloaded, How does the compiler know the tail of p and stop deleting.

Thanks for your attention.

解决方案

The error is clear, I think: just change

//for (int i = 0; i < 12; ++i)

for (int i = 0; i < 10; ++i)



or change the size of array to 12.

1. The difference between delete and delete[]

From the standard (5.3.5/2) :

In the first alternative (delete object), the value of the operand of delete shall be a pointer to a non-array object or a pointer to a sub-object (1.8) representing a base class of such an object (clause 10). If not, the behavior is undefined.

In the second alternative (delete array), the value of the operand of delete shall be the pointer value which resulted from a previous array new-expression. If not, the behavior is undefined.

2, 3. How does the compiler know the size of a array when using delete [] ?
How does the compiler know the tail of p and stop deleting?

It is unspecified how the size information is stored, so each compiler may implement it in a different way, but a common way to do it is to allocate an extra block before the array. That is, when you do this:

int* p = new int[10];



it actually allocates an array of 11 integers, and stores the array size in the first element


1. If you used new use delete, if you used new[] then use delete[].

http://stackoverflow.com/questions/4255598/delete-vs-delete[^]

2. How the compiler knows is compiler dependent.

http://stackoverflow.com/questions/975792/how-does-delete-know-the-size-of-an-array[^]

3. See above


这篇关于删除和删除[]之间有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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