🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

Why doesn't this work?

Started by
6 comments, last by gone_postal 22 years, 7 months ago
Why doesn't this work? Sorry, I'm new to pointers, although sometimes I can get them to work! #include int main(int argc, char* argv[]) { int myInt; int * pInt; pInt = &myInt *pInt = 5; std::cout << myInt << "\n"; delete pInt; return 0; } Edited by - gone_postal on November 24, 2001 7:11:07 PM
Advertisement
Because you are trying to delete a statically allocated int.
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
uh... Huh?
Could you explain that in terms that my tiny brain could understand?
Dont call ''delete'' on a pointer unless you allocated it memory with ''new''. You''re pointer is pointing to memory that was statically allocated (ie variable that is not a pointer), which you cannot call ''delete'' on.
-----------------------"When I have a problem on an Nvidia, I assume that it is my fault. With anyone else's drivers, I assume it is their fault" - John Carmack
OH!!!! Thanks a lot!
Variables that are declared with the new allocation (or static for that matter) go in the data area of the program memory, not the stack. I don''t see why you wanted to delete the pointer anyway, because it is at the end of your program anyway, and will be deconstructed as soon as the main program finishes.

If you had this bit of code in a function, the pointer would be deconstructed as well, because it was declared in the function. You would have to use the new allocation for it to have a lifetime long enough for you to use later in your program.

Out of curiosity, what are you passing into your main program?
If at first you don't succeed, use profanity and try, try again.
grandmlee, thoughts like that is what makes so many programs have memory leaks. you should never ever rely on the OS to release all the memory. You will develop the bad habit of not releasing memory when you need to or worse yet never releasing memory at the end of a program, yet the memory is still unavaible for some time after the program exits. C++ does not readily support garbage collection, may you should code in java.
also he is not passing anything to the main program, it is simple a test app trying out pointer stuff.
quote: Original post by GrandMLee
I don''t see why you wanted to delete the pointer anyway, because it is at the end of your program anyway, and will be deconstructed as soon as the main program finishes.

Not always true. The memory may be released, but the objects are not necessarily destroyed. Calls to abort() come to mind (if your program is abort()''ed, files are not closed, objects not destroyed, etc)

And with windows, it''s even worse, since some objects (e.g. COM objects) are allocated outsite of the application space, as "global" operating system objects.

quote:
Out of curiosity, what are you passing into your main program?


argc is the number of command line arguments (at least one, the program name)
argv is an array of strings (char arrays), each of which holds a command line argument.
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan

This topic is closed to new replies.

Advertisement