Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

Kwizatz

Void pointers to pointers

This topic is 5316 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

Ok, suppose I have the following function:
void Callback(void *pData)
{
/*...*/
};
 
Where pData is going to point to a pointer of some struct , object or whatever, how do you cast the pointer so you are able to manipulate it (new and delete), I tried (example):
void Callback(void *pData)
{
 int *ptrData = (int *) *pData;
};
 
but that way it won''t compile with the error `void*'' is not a pointer-to-object type, I also did
void Callback(void *pData)
{
 int *ptrData = (int *) pData;
};
 
but I think that wont work, since it will be then a pointer to int and that int will be the other pointer and not the actual int I am looking for, finally I also did:
void Callback(void *pData)
{
 int *ptrData = (int *) &pData;
};
 
And it compiled, but I decided that on that last try I don''t even know what I am doing and instead decided to ask here Aeon Games

Share this post


Link to post
Share on other sites
Advertisement
void Callback(void *pData) {

int *ptrData = (int *)pData;

}

That will work, and allow you to modify the "contents" that pData pointed to. However, if you''re only using pData as a direct cast to an int type (with only an int pointer type given on calling Callback) then you might as well declare Callback as Callback(int *pData) and avoid the cast altogether. Keep in mind that if you use delete/delete[] on pData inside Callback, then the variable still referencing it outside of Callback will hold a now invalid value and will most likely crash the application if you use it again.

In other words, if:

{
int *intArray = new int[10];

Callback(intArray);

intArray[0] = 0;
}

will crash if:

void Callback(void *pData) {

int *ptrData = (int *)pData;

delete[] ptrData;
}


Share this post


Link to post
Share on other sites
Thanks,

no the int was for explicative porpuses in reality it sould be a pointer to a vector<> of structs, but that made the example too big.

thanks again

Share this post


Link to post
Share on other sites
Just to clarify... in your original post you said you were doing a pointer to a pointer:

quote:
Where pData is going to point to a pointer of some struct , object or whatever

If, by this, you meant you had something like:

int *something;
Callback( &something );

Then I think you''d want to do:

void Callback(void *pData)
{
int *ptrData = *(int **)pData;
}

Share this post


Link to post
Share on other sites
Assuming you meant pData pointed directly to an int, you'd want:

int ptrData = *((int *) pData);

If however you meant pData pointed to a pointer that held an int, you'd want:

int ptrData = **((int **) pData);

I'll explain what you tried to do assuming the first case is what you wanted, for each callback attempt:

1) Tried to create a pointer to an int called ptrData, and assign it to a void (*pData tries to change (void *) into a (void) or invalid type) cast to a pointer to an integer.

2) This is valid, in that ptrData now points to the integer in question.

3) Now this one is rather amusing. You try to assign a pointer to an integer, called ptrData, to the address of the pointer of the data, cast to a pointer to an integer. Before the (int *), &pData is of type (int **)... well really type (void **), but we want to use it as an (int **) .

Now, an example using a class:

class ExampleClass
{
public:
int Variable;
};

void Callback( void * pData )
{
//using a pointer to the data directly
ExampleClass * ptrData = (ExampleClass *) pData;
cout << "Callback Variable using pointers: " << endl;
cout << " " << (*ptrData).Variable << endl;
cout << " " << ptrData->Variable << endl;

//copying the data then using it directly
ExampleClass Data = *((ExampleClass *) pData);
cout << "Callback Variable using copying: " << endl;
cout << " " << Data.Variable << endl;

//using a 'reference' which dosn't copy, and is like a pointer, but can offly be assigned offce, at variable construction.
ExampleClass & Data = *((ExampleClass *) pData);
cout << "Callback Variable using references: " << endl;
cout << " " << Data.Variable << endl;
}


Note how the last two look nearly identical. The & indicates that this is a reference, not a new variable. Again, you can't reassign a refernece to point to something else, and after declaring it you use it exactly like a normal variable, but it isn't an entirely new copy either.

Using & in front of a pre-existing variable gets you the address of said variable. That is, an example useage of this callback would be:

void Call_Callback( void )
{
ExampleClass MyExample;
MyExample.Variable = 4;
Callback( (void *)&MyExample );
}


Which gets the address of MyExample (aka, a pointer to) and then casts it to a pointer to a void.

         -Mike  

edit: clarified the 3rd version type.

[edited by - MaulingMonkey on February 27, 2004 1:17:13 AM]

Share this post


Link to post
Share on other sites
Alright, thanks, *(int **) seems to be the one I want, this is for my gameSpace plugin, the data is related to a polyhedron, and I am using a pointer to a STL Vector (since I need per face Data and a Poly has as many faces as it needs), thats why I can''t just change the callback system, but shape the code to fit it

again, thanks for your help.

Share this post


Link to post
Share on other sites

  • 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!