# 'double *' to 'double'

hi

I am using gluUnProject to generate some opbject coordinates. I want to use these in a glVertex2d call to draw some lines on the screen. however, gluUnProject returns parameters of type double * and glVertex2d takes parameters of type double. How can I change it from double * to double? does it involve dereferencing it?
I really have no idea what I need to do here, and can’t find any information on it.

any help would be most appreciated

thanks
Gareth

Here is an example (I use this in my picking function):

GLdouble x, y, z;

gluUnProject ( static_cast<double> ( m_x ),
Invert_m_y,
Inverted mouse y.
Result.z,
Model,
Projection,
Viewport,
&x,
&y,
&z );

You are passing in pointers to x, y, z to gluUnProject. It will fill those memory locations with the required values. You simply use x, y, z as you would any ordinary double.

ie. :

GLdouble Distance = sqrt ( x * x + y * y + z * z );

etc…

[This message has been edited by Robbo (edited 04-25-2002).]

Hello!

Say you have a variable x that is of type double * then you can access the value with *x.
Example :
double *x;
x==pointer to the memory address of containing the value of x.
*x==value of x.

Osku

Not quite. Look:

double *x, y;

x is undefined - so could point to anywhere. Initialised to y looks like this:

x = &y; (x points to address of y).

now, *x = 12345.0 and then print out the value of y,

y = 12345.0

Because you put 12345 into the address that x points to (which is y) - so y now equals that value.

When you use gluUnProject, you are passing in the addresses of 3 variables so that OpenGL can store them in the place you want them. The variables themselves are GLdoubles, you just pass pointers to them into gl, ie. &x, &y and &z.

Hope this explains things somewhat.

If you use

``````GLdouble x, y, z;
``````

to define x, y, z then you don’t need to dereference them. In this case your call to gluUnproject looks like

``````gluUnproject(..., &x, &y, &z);
``````

and you use the values as shown in the following example.

``````len = sqrt(x * x + y * y + z * z);
``````

If you used

``````GLdouble *x, *y, *z;
``````

to define x, y, z then you have to dereference them to get their data. In this case your call to gluUnproject looks like

``````gluUnproject(..., x, y, z);
``````

and you use the values as shown in the following example.

``````len = sqrt(*x * *x + *y * *y + *z * *z);
``````

The problem with your second method, Furrage, is that you aren’t properly giving those pointers a memory location. Your code would likely cause a crash. When they are first created, a pointer points god knows where. You either need to give it the address to another variable, assign it the same value as another pointer that has a proper address, or dynamically allocate memory to it with new or malloc.

Always, always, ALWAYS, think about how you’re using memory when using pointers. It will save you tons of headaches.

[This message has been edited by Deiussum (edited 04-25-2002).]

Pointers need to either be initialized by allocating memory to it such malloc or the new operator or you assign the address of an existing allocated memory address…

So
GLfloat *x, *y, *z;

x = new GLfloat;// new GLfloat[size]; //Array
y = new GLfloat;// new GLfloat[size]; //Array
z = new GLfloat;// new GLfloat[size]; //Array

or

GLfloat w;
x = &w;

Pointers dont hold values, they hold a memory addresses.

Remember each call of malloc/new should have a corresponding free/delete to release the memory again. Otherwise we get many memory leaks I think for dfanuk’s purpose he should use the second alternative.

Oh yeah Jimmi… At least I expect the programmer to know this if they are using it… Can’t use it, without knowing it.
But I guess I should have posted that extra line of code for completeness.

Originally posted by jimmi:
Remember each call of malloc/new should have a corresponding free/delete to release the memory again. Otherwise we get many memory leaks I think for dfanuk’s purpose he should use the second alternative.

Originally posted by Deiussum:
The problem with your second method, Furrage, is that you aren’t properly giving those pointers a memory location. Your code would likely cause a crash. When they are first created, a pointer points god knows where. You either need to give it the address to another variable, assign it the same value as another pointer that has a proper address, or dynamically allocate memory to it with new or malloc.

Duh… I thought that was so standard that I didn’t need to mention it. Anyway, its good you mentioned it, as someone might have thought it was the complete code and get no end of errors if they tried to copy it

Originally posted by Furrage:
[b]
Duh… I thought that was so standard that I didn’t need to mention it. Anyway, its good you mentioned it, as someone might have thought it was the complete code and get no end of errors if they tried to copy it

[/b]

For experienced C/C++ programmers, it is pretty common knowledge and wouldn’t need mentioning. However, I think there are a lot of people who try and program OpenGL without getting a strong background in C/C++ first. (Just look at all the C/C++ questions that pop up here, including this one.) It seems that memory usage tends to be the least understood, and most common source of errors for many beginners.

[This message has been edited by Deiussum (edited 04-26-2002).]