-
January 13th, 2005, 01:58 PM
#1
Why does it crash?
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.
-
January 13th, 2005, 01:58 PM
#2
Re: Why does it crash?
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...
Last edited by Zim327; January 13th, 2005 at 02:03 PM.
Best Regards,
--Zim If you find this post useful, please rate it.
_________________________________
"Have you the brain worms?!?!?"
-
January 13th, 2005, 02:04 PM
#3
Re: Why does it crash?
I understand that. But why will that make the program crash?
Originally Posted by Zim327
because you have to use the new command before you delete it
-
January 13th, 2005, 02:11 PM
#4
Re: Why does it crash?
Originally Posted by Zim327
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...
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 ).
--EJMW
-
January 13th, 2005, 02:27 PM
#5
Re: Why does it crash?
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?
Last edited by dullboy; January 13th, 2005 at 02:29 PM.
-
January 13th, 2005, 02:33 PM
#6
Re: Why does it crash?
Originally Posted by dullboy
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.
Arjay
-
January 13th, 2005, 02:40 PM
#7
Re: Why does it crash?
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?
Originally Posted by ejmw
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 ).
-
January 13th, 2005, 02:43 PM
#8
Re: Why does it crash?
Originally Posted by dullboy
I understand that. But why will that make the program crash?
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.
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
-
January 13th, 2005, 02:44 PM
#9
Re: Why does it crash?
Originally Posted by dullboy
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?
1) Wrong form of delete.
2) You don't assign to strings the way you are doing it.
Code:
#include <cstring>
int main()
{
char* str = new char[10];
strcpy(str,"hello");
delete [] str;
}
Regards,
Paul McKenzie
-
January 13th, 2005, 02:47 PM
#10
Re: Why does it crash?
Originally Posted by dullboy
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?
see difference between delete and delete[]
-
January 13th, 2005, 02:50 PM
#11
Re: Why does it crash?
Of course, I know what you suggested is the way to do it. My question is what is wrong with my way?
Originally Posted by Paul McKenzie
1) Wrong form of delete.
2) You don't assign to strings the way you are doing it.
Code:
#include <cstring>
int main()
{
char* str = new char[10];
strcpy(str,"hello");
delete [] str;
}
Regards,
Paul McKenzie
-
January 13th, 2005, 02:54 PM
#12
Re: Why does it crash?
Originally Posted by dullboy
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?
Here is what is happening:
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.
Hope that clears things up!
--EJMW
-
January 13th, 2005, 02:58 PM
#13
Re: Why does it crash?
Originally Posted by dullboy
My question is what is wrong with my way?
Because the C++ language specification says it's wrong (more precisely, undefined). You don't code an application that invokes undefined behavior.
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
Last edited by Paul McKenzie; January 13th, 2005 at 03:00 PM.
-
January 13th, 2005, 03:07 PM
#14
Re: Why does it crash?
Excellent! So simply say when I assigned "hello" to str, I changed the address str pointed to.
Originally Posted by ejmw
Here is what is happening:
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.
Hope that clears things up!
-
January 13th, 2005, 03:12 PM
#15
Re: Why does it crash?
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.
--EJMW
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|