Jump to content
  • Advertisement
Sign in to follow this  
KuroKage

Help: A very simple pointer problem and yet...

This topic is 4429 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi, This is a weird problem I'm having. I tested a simple class: class SomeObject { public: float x, y, z; }; This piece of code: SomeObject* obj; obj->x = 5; Should be an error right? Since obj doesn't point to anything yet, it's a dangling pointer. However, in a console app, with nothing else coded except for the class definition and the code above (inside main()), the program runs fine. I even tested it with something like: int x = obj->x; cout << x; And it even displays "5". I can't get the same concept running in a window app. I'm guessing that the compiler automatically allocates whenever an uninitialized pointer is used. Is that it? Cause this piece of code really crashes (which I believe should be the case) but in a console app, it's fine. Please Help! Thank You

Share this post


Link to post
Share on other sites
Advertisement
The compiler does not know about uninitialized pointers, so it cannot complain about your construct. From the syntax point of view, it's perfectly okay.

What you do in your program is writing the number 5 at a random place im memory where you sometimes happen to have the right to write, so everything is fine (except you corrupted your memory ofcourse :) )

If you initialize your pointers like

SomeObject* obj = 0;

you should get an access violation in either setup.

Share this post


Link to post
Share on other sites
This can happen because this code:
Quote:

SomeObject* obj;
obj->x = 5;


doesn't initialise your SomeObject pointer to NULL, under debug (when using MSVC anyway) it will initialise it to NULL but in release this won't happen and it will be left with whatever value was in memory where and wehn it was allocated.

Quote:

SomeObject* obj=NULL;
obj->x = 5;


Would give you a runtime error just as you'd expect it too. You should NEVER just start assigning values to a dereferenced pointer without initialising to a safe target first!

Quote:

SomeObject* obj= new SomeObject;
obj->x = 5;
cout << obj->x;
delete obj;


And no it definately does NOT automatically allocate when an uninitialised pointer is used, if you're using that code somewhere else then that is definately the cause of your crash.

Andy

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
So your trying to break your compiler and know that what you are doing is wrong. So what is the problem?

Share this post


Link to post
Share on other sites
Accessing an unitialised pointer has undefined behaviour. It is not required that it crash your program. The simplest answer to your question is you got unlucky (yes unlucky).

Most memory wouldn't be addressable to your program, but your program managed to get a pointer to within its addressable memory. Ask yourself, who owns that memory? Its either your programs instructions, its data, or just a bit of free space that has yet to be used.

If you had been lucky, your program would have crashed thus showing you that there was an error in your code. Instead you have written the value "5" somewhere in your programs memory. It could be in another variable now. Good luck tracking that bug down [smile]

Share this post


Link to post
Share on other sites
This is sort of implied in some of the other responses, but it's worth stating in direct form: "Correct" code in the sense that it compiles does not in any way shape or form guarantee "correct" code in the sense that it will do what you intend or want. If this weren't the case, all code that compiled would be bug-free. :)

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!