For example,
char* str = "Hi, there!";
delete str;
I understand I shouldn't use delete str here. But I don't understand why delete str will make program crash? Thanks for your advices.
Printable View
For example,
char* str = "Hi, there!";
delete str;
I understand I shouldn't use delete str here. But I don't understand why delete str will make program crash? Thanks for your advices.
because you have to use the new command before you delete it
in other words, you created the string on the stack and then delete tries to delete str on the heap which doesn't exist there...
I understand that. But why will that make the program crash?
Quote:
Originally Posted by Zim327
Actually, this isn't quite true for this situation. The compiler will put a string literal for your string into the const data segment of the compiled executable, so your string pointer is set to point to a variable that is not changeable. It would be like trying to change the assembly instructions of your own executable from inside that executable. To use delete you have to be pointing to something that was allocated on the heap (or the free store, for those standards purists ;) ).Quote:
Originally Posted by Zim327
Thanks for you guys' explaination! I always enjoy learning something from trivial questions. Also I have another question. Does that mean a pointer might point to stack?
A pointer can pretty much point to anything - even an invalid location. If you try to dereference an invalid pointer, the program will crash.Quote:
Originally Posted by dullboy
Arjay
How is the example in the following,
char* str = new char[10];
str = "hello";
delete str;
still the program crashed! See, I already use new to allocate the memory for str then delete str to deallocate the memory. Why did it still crash?
Quote:
Originally Posted by ejmw
This all depends on how the compiler's heap manager is coded. There is nothing stopping a heap manager to recognize that "str" wasn't previously allocated, so it doesn't do anything, making your app "safe". That is definitely not how the heap manager is coded for your particular version of VC++, but maybe another version of VC++ makes this check in the heap manager, or does something else to avoid (or sidestep) the crash.Quote:
Originally Posted by dullboy
Therefore the behavior is undefined -- expect anything to happen if you attempt to use "delete" on a pointer that was not returned by "new".
Regards,
Paul McKenzie
1) Wrong form of delete.Quote:
Originally Posted by dullboy
2) You don't assign to strings the way you are doing it.
Regards,Code:#include <cstring>
int main()
{
char* str = new char[10];
strcpy(str,"hello");
delete [] str;
}
Paul McKenzie
see difference between delete and delete[]Quote:
Originally Posted by dullboy
Of course, I know what you suggested is the way to do it. My question is what is wrong with my way?
Quote:
Originally Posted by Paul McKenzie
Here is what is happening:Quote:
Originally Posted by dullboy
Hope that clears things up!Code:char* str = new char[10]; //a new character buffer is allocated on the heap.
//Let's pretend the address you get back is 0x0bb000a0 just for fun.
//Now the value of the 'str' variable equals 0x0bb000a0, right?
str = "hello"; //OK. The compiler had set aside a constant with the value
//"hello" in the data segment of your program.
//This might be at, say, memory address 0x010000aa.
//Now you're setting the value of str = 0x010000aa. So you now have
//nothing pointing to your allocated string that is still hanging around at
//0x0bb000a0!
delete str; // Now, you're trying to delete the memory at location
//0x010000aa! This is in your program's const data segment and can't be
//deleted! Not only that, but you don't have any way to clean up the string
//at 0x0bb000a0, either, so you'll get a memory leak to boot.
Because the C++ language specification says it's wrong (more precisely, undefined). You don't code an application that invokes undefined behavior.Quote:
Originally Posted by dullboy
Others may point to underlying code, but there is no need to. The C++ language spec clearly states that this is undefined behavior, regardless of what the underlying code may state. Even if it worked without crashing, you don't write code that does this.
Regards,
Paul McKenzie
Excellent! So simply say when I assigned "hello" to str, I changed the address str pointed to.
Quote:
Originally Posted by ejmw
Yes, that is exactly right. That is the important thing to understand about pointers; changing their value only means that you're changing the location that they are pointing to. It doesn't mean that you are changing what is contained at that location.