delete array; not delete [] array;
登录 | 论坛导航 -> 华新鲜事 -> 求学狮城 | 本帖共有 3 楼,分 1 页, 当前显示第 1 页 : 本帖树形列表 : 刷新 : 返回上一页
<<始页  [1]  末页>>
作者:simon (等级:10 - 炉火纯青,发帖:6859) 发表:2005-10-30 11:15:53  楼主  关注此帖评分:
why does not "g++ -Wall" give warning message when compiling the following code?When u run the code, you will get segmentation fault as "flag" is used after the memory released, however, the segmentation fault happens at the "delete [] darray" sentence, why is it so? I thought it should happen at "flag[i] = 8". the compiler should be able to detect such mistake, why does not "g++ -Wall" give any warning message? please reply. #include using namespace std; int main () { bool * flag = new bool[8]; delete [] flag; double * darray = new double[10]; for (int i = 0; i < 8; i ++) flag[i] = true; cout
delete array; not delete [] array;
delete [] arr;

is equivalant to

delete arr[0]; delete arr[1]; ... delete arr[k];


------------------------------------------------

defination from msdn:
The delete operator destroys the object created with new by deallocating the memory associated with the object.
------------------------------------------------

however your array is array of primative type bool


now try this

Class Bool
{
public bool myBool;
};

Bool *flag2 = new Bool[8];
delete [] flat2;

You may say I'm a dreamer
but I'm not the only one...
欢迎来到华新中文网,踊跃发帖是支持我们的最好方法!原文 / 传统版 / WAP版所有回复从这里展开收起列表
作者:simon (等级:10 - 炉火纯青,发帖:6859) 发表:2005-10-30 11:16:41  2楼
delete array; not delete [] array;delete [] arr; is equivalant to delete arr[0]; delete arr[1]; ... delete arr[k]; ------------------------------------------------ defination from msdn: The delete operator destroys the object created with new by deallocating the memory associated with the object. ------------------------------------------------ however your array is array of primative type bool now try this Class Bool { public bool myBool; }; Bool *flag2 = new Bool[8]; delete [] flat2;
different btw 'object' and 'primitive data type'
欢迎来到华新中文网,踊跃发帖是支持我们的最好方法!原文 / 传统版 / WAP版所有回复从这里展开收起列表
作者:simon (等级:10 - 炉火纯青,发帖:6859) 发表:2005-10-31 01:04:01  3楼
no la...as simon said, object vs primitive data type
。。。。。。ok...
欢迎来到华新中文网,踊跃发帖是支持我们的最好方法!原文 / 传统版 / WAP版所有回复从这里展开收起列表
论坛导航 -> 华新鲜事 -> 求学狮城 | 返回上一页 | 本主题共有 3 篇文章,分 1 页, 当前显示第 1 页 | 回到顶部
<<始页  [1]  末页>>

请登录后回复:帐号   密码