I have two blocks of code about new[]
and delete[]
:
1)
#include <string>
int main()
{
std::string *p = new std::string[0];
delete[] p;
return 0;
}
2) In this case, I merely change std::string
to int
int main()
{
int *p = new int[0];
delete[] p;
return 0;
}
My question is:
Why the first program crashes with the following message (in linux environment):
Segmentation fault (core dumped)
But the second program works well without any error?
EDIT
compiler: g++ (Ubuntu/Linaro 4.7.2-2ubuntu1) 4.7.2
I just use g++
without any argument to compile it.
If it is a compiler bug, should they crash or not according to the standard?