Skip to main content
gamedev.net
🔒 Locked

Void pointers to pointers

Started by Kwizatz Feb 26, 2004 at 4:53 PM 4 replies 500+ views
Original Post
Kwizatz
Kwizatz
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
sordid
sordid
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;
}


Kwizatz
Kwizatz
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
SantaClaws
SantaClaws
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;
}
MaulingMonkey
MaulingMonkey
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]
Kwizatz
Kwizatz
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.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.