Jump to content
  • Advertisement
Sign in to follow this  
chillypacman

Question about pointers

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

I was working with directx but this is more of a newbie question. Basically I created a function that took a pointer to a directx device in the constructor. I passed the pointer to an instance of the function before initializing the directx device. I figure since it's a pointer once the device is initialized the pointer will point to an initalized directx device. However that didn't quite work out, it seems that the device pointer I had passed was still pointing to null space. I don't get it, it's a pointer, so it shouldn't matter if the place it is pointing to is initailized or not provided that by the time the pointer is used the place it is pointing to is initialized.

Share this post


Link to post
Share on other sites
Advertisement
Variable defined as pointer is a number (like int or float). When such value is declared, it said to be uninitialized. Only after you initialize it, does it contain a "pointer" to memory address it references.

By passing initial pointer you passed the address by value:

X *pointer; // has value of 1234 (random, not initialized, could be anything)
initialize( pointer ); // initialize stores 1234
pointer = new X(); // pointer now has value of 3148
(*pointer) = 10; // memory at address 3148 now has value of 10




You can think of pointers as offsets into an array.

int memory[10000000]
int pointer = 1234;
initialize( pointer ); // remembers that the memory address is 1234
pointer = 3148;
memory[pointer] = 10;





What you would need for your code to work as you intended, would be pointer to pointer. That was common practice in C to provide return values.

Share this post


Link to post
Share on other sites
Quote:
Original post by chillypacman
I was working with directx but this is more of a newbie question.

Basically I created a function that took a pointer to a directx device in the constructor. I passed the pointer to an instance of the function before initializing the directx device. I figure since it's a pointer once the device is initialized the pointer will point to an initalized directx device.

However that didn't quite work out, it seems that the device pointer I had passed was still pointing to null space.

I don't get it, it's a pointer, so it shouldn't matter if the place it is pointing to is initailized or not provided that by the time the pointer is used the place it is pointing to is initialized.


You mean:

MyFunction(Direct3DDevice *d3ddev)
{
// do something
}

main()
{
Direct3DDevice *mainDevice;
MyFunction(mainDevice);
InitializeDirect3DDevice...
UsePointerInMyFunction // error at using pointer
}
?

If so, that will always crash, because you are passing the address
that mainDevice pointer is pointing to to the MyFunction routine, and, since
mainDevice is not initialized, it points to nothing, therefore it's
a NULL pointer. You basically then have the NULL value be given to the
d3ddev pointer, which will hold a NULL afterwards.

Now, to correctly do this you need to pass the address at which the pointer
variable is located, not the address at which the pointer points at.
So these changes should be made:

MyFunction(Direct3DDevice **d3ddev)
{...}
called MyFunction(&mainDevice);

Unfortunately, this will require a further operation whenever you want to use
the DirectX device itself.

Also, if you plan on using your original solution, by putting the MyFunction call after initializing the DirectX Device, you should know that, while I suspect it would be safe with DirectX, working with Windows objects in general you should not assume that an old pointer to them is always valid. Objects are sometimes moved and relocated in memory, reallocated, so your old pointer to them may have become useless at the time you are using it, pointing to an invalid memory range (say hello, memory read/write access fault).

Share this post


Link to post
Share on other sites
It is called pass by value. When you call the funcition and pass the uninitalized pointer it is copyed. This copy isn't updated when you initalize the pointer. That is the source of your problem.

Share this post


Link to post
Share on other sites
Quote:
If so, that will always crash, because you are passing the address
that mainDevice pointer is pointing to to the MyFunction routine, and, since
mainDevice is not initialized, it points to nothing, therefore it's
a NULL pointer. You basically then have the NULL value be given to the
d3ddev pointer, which will hold a NULL afterwards.


No it wasn't like that.

More like:

class func
{
func(pointer* p1)
pointer* p;
}

...

pointer* p2;
func d = new func(p2);

p2 = new pointer();



Anyway, I understand whats going on now (from Antheus), since the pointer isn't initalized there is no a ctual place it's pointing to (besides garbage) so pointing to that pointer is no good since when that pointer gets initialized it will point someplace else entirely.

Share this post


Link to post
Share on other sites
If you use a pointer to a pointer something like pointer** p; then when you pass in p** to a class (p*) is copied (so it will no longer be modifyable by code outside the class), and p** will update the way you want it to.

Share this post


Link to post
Share on other sites
Sign in to follow this  

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