Sign in to follow this  
esunder

Seeking an explanation about c++ and pointers

Recommended Posts

esunder    122
Hi all, This is not a time valuable question. In fact, I have just solved an error that I was having but I am looking for an explanation to why it happened in the first place. I have two classes that use forward declarations so that each class can know about the other. After I create an instance I pass a ptr of each to the other so that they can call each others functions. However, in one of them, I forgot to save that ptr that came in. Now the problem was obviously a seg fault when I tried to use that pointer later on. But the seg fault did not happen when I tried to use the pointer. In fact, it happened about half way through the execution of the function that belonged to the ptr that was not valid. It called the function on the invalid pointer and appeared to be executing correctly (it printed out cout's etc). However, once it tried to use variables of the pointer that did not exist, it crashed. Any explanation to why this happened? I am assuming it's because each class knew about the other but I still dont see why it didnt crash when I tried to use a ptr that was not initialized. Thanks for any clarification that anyone can bring to the table.

Share this post


Link to post
Share on other sites
Washu    7829
You ended up in the land of undefined behavior, where anything can happen.

However, in your case here is a reasonable guess of which part of the land of undefined behavior you landed in:
The method has no way of knowing that its this pointer is valid, so it works fine, until it decides to try and access anything associated with "this", at which point it goes: Oh shit! and bombs.

Share this post


Link to post
Share on other sites
Antheus    2409
When you de-allocate mamory (delete, free, by going out of scope), that memory is made available to application.

Whenever the application allocates memory, it will re-use and overwrite old contents.

Of course there is no way to tell if or when this memory will be re-used, leading to unexpeted crashes.

This happens when you have a pointer pointing to such memory contents. It doesn't know whether that particular address is still valid, it'll simply use whatever is stored there.

In your case, during one run, the memory was still intact, during the other, it was overwritten.

Object methods are passed a pointer to where the object stores its data. If the object was deleted, but you keep the pointer to it, the method still knows where to find the code, runs it, but the data this code uses is now possibly (but not necessarily) corrupted. And this leads to random crashes.

Share this post


Link to post
Share on other sites
Crypter    748
Lesson be learned:

-Always initialize pointer members to NULL (0) to indicate "bad pointer"

-Always insure the pointer is valid before attempting to use it.

Doing this will solve *alot* of pointer problems in the future[smile]

Share this post


Link to post
Share on other sites
Zahlman    1682
Quote:
Original post by Crypter
Lesson be learned:

-Follow the RAII paradigm.

-Use anything other than raw pointers when you can, and raw pointers when you really have to.


Fixed [smile]

Share this post


Link to post
Share on other sites
janta    345
Quote:
Original post by Crypter
-Always insure the pointer is valid before attempting to use it.


IMO Stuff like "if(p) p->Stuff()" everywhere will juste make many bugs harder to find. Just let your game crash (or use assertions...) so you know there is a problem. Only leave the if() in situations where you expect your pointer might be NULL and that wouldnt be an error.

And if the pointer is never supposed to be NULL, try to use a reference instead.

Share this post


Link to post
Share on other sites
Crypter    748
I personally prefer code that does not break, under any reason. The
primary reason is that of the end user. Most people (I know I do) would
rather their game not crashing, but continue if it can.

assert() is another good alternitave though, but imo, it should only be
used in debug build.

I useually follow the pre/post condition rule to insure all possible problems
are resolved in some manner.

I second Zahlman's suggestions. Use refrences instead of pointers, when
possible, and you wont have the problems associated with pointers.

This is just my opinion[smile]

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this