Jump to content
  • Advertisement

Archived

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

jtmerchant

Why have pointers to pointers for parameters that are interfaces?

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

Why does DirectX use pointers to pointers on their interfaces such as IDirect3DDevice9**? For example, when you are trying to call a function, such as IDirect3D9::CreateDevice, one of the parameters is a pointer to a pointer to your new device. Now this got me thinking, and I tested some things. I came to some interesting facts: A IDirect3DDevice9* seems to be acting not like a pointer, but the actual object. If you use this as a parameter to one of your own functions, and you initiate the device from inside the function, you're initiating a device to a copy of the programmer's object, leaving the real object as it is, just like any regular variable, such as an int. But if you use a IDirect3DDevice9**, it acts like, say, a pointer to an int, where you can change the variable inside the function and it will retain the value even after execution is out of the scope of the function. Why is this so?? Correct me if information is inaccurate. [edited by - jtmerchant on January 12, 2004 4:58:13 PM]

Share this post


Link to post
Share on other sites
Advertisement
DirectX uses the system called COM under windows. This is a system where there is a particular layout in the memory of the way the code is called. This system is language independant and allows you to extend the functionality of your app using things like OLE. It also allows you to maintain backwards compatibility as DirectX has done with previous versions of DirectX.

James

Share this post


Link to post
Share on other sites
Because DX will set ptr for you.

eg.

Your device is a actually a pointer - IDirect3DDevice9* pDevice.

To create device, call CreateDevice, DX will create a device and assign a ptr that you past (to modify a pointer not it''s value you need a pointer to that pointer).

You don''t explicitly call delete pDevice but you call Release() because somebody else may be using device

class IDirect3DDevice9 : public IUnknown;

IUnknown::Release()
{
--refCount;
if (!refCount)
delete this;
}

Share this post


Link to post
Share on other sites
generally using pointer to a pointer is used for the memory managment either in Directx or any other C++ application.think of the pointer as (box) in memory where you are storing the variable or object.pointer to a pointer is a box that contains the no or index. of the box containing the variable or object.so the run time can change the place in which it is storing the variable or object through changing the pointer while the pointer to the pointer is unchanged.your application can access the variable or object through the pointer to pointer with no lost data or errors.but if you just use a pointer to variable or object and the run time changed the place where it is storing the variable you won''t be able to access it. this is the way in which the operating system does the memory managment.

Share this post


Link to post
Share on other sites
I don't know precisely what goes on under the hood in DirectX. But here's a simple example of a use for a pointer-to-a-pointer that's fairly transparent: an array of arrays; a 2d array.


//Create array
double ** arry = new double * [100];
for(int j=0; j<100; j++)
{
arry[j] = new double[20];
}

//Do stuff

//Delete array
for(int j=0; j<100; j++)
{
delete [] arry[j];
}
delete [] arry;


[edited by - Terranfury on January 12, 2004 6:05:12 PM]

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!