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;
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...