why does not "g++ -Wall" give warning message when compiling the following code?
所在版块:求学狮城 发贴时间:2005-10-30 00:44  评分:

用户信息
复制本帖HTML代码
高亮: 今天贴 X 昨天贴 X 前天贴 X 
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 <iostream>

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 << "before releasing darray" << endl;
delete [] darray;
cout << "darray released" << endl;
return 0;
}

.
欢迎来到华新中文网,踊跃发帖是支持我们的最好方法!

This page is intentionally left blank
 相关帖子 我要回复↙ ↗回到正文
why does not "g++ -Wall" give warning message when compiling the following code? icky   (660 bytes , 932reads )
delete array; not delete [] array; simon   (470 bytes , 648reads )
do u mean icky   (132 bytes , 389reads )
no la...as simon said, object vs primitive data type sarah   (0 bytes , 391reads )
。。。。。。ok... simon   (0 bytes , 289reads )
different btw 'object' and 'primitive data type' simon   (0 bytes , 343reads )
using linux/glibc? Segfault because you damaged the link list hash   (1304 bytes , 650reads )
a mistake hash   (233 bytes , 411reads )
but after icky   (103 bytes , 386reads )
The default libstdc++ operator new and delete use the libc malloc and free. hash   (86 bytes , 465reads )