delete[] crash a program

All works fine but "delete[] cmd;" crash the program;
I tryed to find explaination in Internet but there was nothing. Here is the code:
1
2
3
4
5
6
7
int _tmain(int argc, TCHAR* argv[])
{
	LPCTSTR	cmd = new TCHAR[10];
	//some code
	delete[] cmd;
	return 0;
}
Last edited on
Are you forbidden to use std::string instead? Or even good old char?

1
2
3
4
5
6
int main(int argc, char *argv[])
{
    char * const cmd = new char[10];
    // some code
    delete[] cmd;
}


Anyway, it is possible that in your //some code you change the value of cmd.

If you do this, a crash becomes likely. To prevent such a problem is why in my example, cmd is a constant pointer.

Do you change the value of cmd?
closed account (S6k9GNh0)
In the Windows world, TCHAR is useful when it comes to determine between wchar_t /char_t.
TCHAR doesn't determine between wchar_t/char because it can be either. It's ambiguous by definition which is why I hate it and repeatedly recommend people avoid using it.
closed account (S6k9GNh0)
Well, I normally just tell people to stay away from Windows-isms but we all see how useful that is...
Last edited on
Topic archived. No new replies allowed.