Sign in to follow this  
MTclip

pointer ... or pointer pointer

Recommended Posts

.. --- I have Texture, Sprite, and Game classes the game class has 2 memebers Pointers to Texture and Sprite.. the Sprite has a member pointer to Texture.. I set the Sprite's Texture member equal to The Game's (i want a refrence to it) did i do this right .. or should the Sprite's be Pointer Pointer Texture.. also either way Do i need to delete the Sprite's Texture It never calls new ... thanks

Share this post


Link to post
Share on other sites
The pointer to pointer construct is typically used when you want to modify the actual pointer, and not just the object that the original pointer points to. For example


void ZeroMyPointer(int* psomeinteger)
{
psomeinteger = 0;
}

int main()
{
int myinteger;
int *pmyinteger = &myinteger;
ZeroMyPointer(pmyinteger);
}






The above code is poorly written, and attempts to zero a pointer passed into the ZeroMyPointer function. The problem with the function is that it does not modify the pointer that is passed in as an argument, it modifies a copy of that pointer. This should not be too suprising, as it is how pass-by-value arguments work. The address in the pointer pmyinteger is just copied into a new variable psomeinteger (the one defined in the parameter of ZeroMyPointer), and it is this copy that is modified.

This means that pmyinteger in the main() function is not modified at all.

If you want to modify the pointer, then one method of doing it would be to pass the pointer by pointer.


void ZeroMyPointer(int ** ppsomeinteger)
{
(*ppsomeinteger) = 0;
}

int main()
{
int myinteger;
int *pmyinteger = &myinteger;
ZeroMyPointer(&pmyinteger);
}





Here we have changed the parameter of ZeroMyPointer to a pointer to pointer to int. Again we have used pass-by-value semantics, but now we have the address of the pointer inside the function, and so we can derefernce this address to change the original pointer pmyinteger.

This is one of the main uses of pointer to pointers.

As for deleting it, you should only delete everything you allocate with new. You should only delete[] everything you allocate with new[], and you should only free() everything you allocate with malloc(). If you have two pointers pointing to the same memory that was allocated with new, you only call delete on one of those pointers. The problem this leaves you with is that the second pointer is left pointing to memory that does not exist anymore. So you need to be very careful with having two pointers that point to the same dynamic memory.

One solution to this problem is boost::shared_ptr. This employs a reference counting mechanism to ensure that the memory is only freed when all boost::shared_ptrs that point to the memory have been destroyed, this prevents leaving pointers pointing to unitialized memory. It also removes the need for you to worry about calling delete on memory that you allocate with new, as it handles the deleting for you.

[smile]

Share this post


Link to post
Share on other sites
1. You can perfectly have two pointers pointing to the same memory.
2. Each call to new should be paired with one call to delete.

So basically what you can do is.
a) Create a new texture (with new) in Game.
b) Create a new sprite in Game.
c) Set the sprite's pointer to the texture in Game.
d) Do stuff.
e) Delete the sprite (in Game). Sprite does nothing with its texture pointer.
f) Delete the texture (in Game).

Greetz,

Illco

Share this post


Link to post
Share on other sites
I'm not an expert on this, so this may be wrong, but you might want to try this.

Create var1 with the new operator.
Have var2 be a reference to var1.

Now, var1 and var2 point to the same address in memory (let's say 0x100000). If you delete either variable, the memory should be released. Then the other variable is just a pointer to memory that doesn't exist, and should not be used. After the program ends, your actual four byte pointer should be deleted, and there should not be a memory leak.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Mr. Jingo brings up the point of using pointers to pointers as function parameters. I would like to mention that since you're using C++, you have the option of using pointer references instead. Such code would look someting like this:


void ZeroMyPointer(int *& ptr)
{
ptr = 0;
}

int main()
{
int myinteger;
int *pmyinteger = &myinteger;
ZeroMyPointer(pmyinteger);
}


It's mostly a stylistic choice really.

Share this post


Link to post
Share on other sites
Quote:
Original post by Anonymous Poster
Mr. Jingo brings up the point of using pointers to pointers as function parameters. I would like to mention that since you're using C++, you have the option of using pointer references instead. Such code would look someting like this:

*** Source Snippet Removed ***
It's mostly a stylistic choice really.
That is exactly what I was going to say.

If you use the int **ptr way then IMHO you are obliged to assert(ptr != NULL); if you access *ptr.
With a reference to a pointer you can simply assume that it is a valid reference.

Share this post


Link to post
Share on other sites
Quote:
Original post by iMalc
If you use the int **ptr way then IMHO you are obliged to assert(ptr != NULL); if you access *ptr.


I concur. [smile]




Share this post


Link to post
Share on other sites
Quote:
Original post by MTclip
..
---

I have Texture, Sprite, and Game classes

the game class has 2 memebers Pointers to Texture and Sprite..
the Sprite has a member pointer to Texture..

I set the Sprite's Texture member equal to The Game's (i want a refrence to it)
did i do this right .. or should the Sprite's be Pointer Pointer Texture..

also either way Do i need to delete the Sprite's Texture
It never calls new ...

thanks


Use smart reference linked pointers to safely have two pointers point to the same thing, and have both set to NULL whenever the object (eg. texture) is deleted.

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