PDA

View Full Version : Clarifying to the compiler indirection or multiplication



31337
04-18-2003, 02:50 PM
Is there another way to indicate multiplication besides the * sign? Visual C++ is *RETARDED* and for some reason everytime I try to multiply two floats it gives me an indirection error! What do I do? Here's my code:



float MD2Model::interpolate(float v1, float v2, float far) {
float v3;
float temp = far * v2;
float temp2 = far * v1;
v3 = temp - temp2 + v1;
return v3;
}

that function gives me not one, but TWO indirection errors on the temp and temp 2 equals lines. Any help would be greatly appreciated. Thanks

shinpaughp
04-18-2003, 03:02 PM
I think the problem is far. NEAR and FAR are pointer types used in 16 bit windows. So, change far to another name and you should be okay.

31337
04-18-2003, 03:54 PM
you nailed it man! Now my linear key frame based animation works perfectly. Way to go man, rock on.

Suppose it wasn't the variable name confusion. Is there a way to clarify if your intent is to multiply or to dereference?

shinpaughp
04-18-2003, 04:09 PM
I think the compiler is typically smart enough to figure it out... far could probably be considered a Windows keyword and as such can't be used as a variable name. Just as you couldn't name a variable "for", or "while" or "float", etc. The problem had nothing to do with the * as either multiplication or dereferencing operator.

And, it is illegal operation to multiply a pointer.

[This message has been edited by shinpaughp (edited 04-18-2003).]

31337
04-19-2003, 10:57 AM
It seems strange that you can't multiply a pointer. Why is that? I know there seems to be very limited application but still...

shinpaughp
04-19-2003, 11:18 AM
I disagree. There is no reason why anyone should want or need to multiply a pointer.

Pointer addition or subtraction is simple and all that is necessary. If you have a pointer to a struct of 56 bytes, then adding 1 to the pointer will add 56 bytes. Of course, you should only do this if you know you are in a contiguous block of that struct type.

Multiplication would give unknown results and be pointing into memory that would probably not be what you wanted or expected. Good example is if pointer were at 0xEFFFFFFF. Multiply that by 2 and it results in 0x40000002(wrap around). And pointers are dynamic, so you are highly likely not to get the same pointer address with successive running of the app, so results would be impossible to determine before hand. ANother example, even better is if pointer points to 0x80000000 and multiply that by 2 = 0x00000000 or NULL. Confusing...

In fact you would likely be reading and writing to other apps memory or even application code in memory or even system memory or system code in memory. Likely cause for pointer multiplication - system instability, application instability, blue screen of death on WIndows, etc, etc.

So, multiplication of pointers is illegal (maximum fine $5000 and 5 years imprisonment http://www.opengl.org/discussion_boards/ubb/biggrin.gif).


[This message has been edited by shinpaughp (edited 04-19-2003).]

31337
04-19-2003, 11:52 AM
hehe, I'm really interested in the low level aspects of pointers. Where can I go?

I thought when you added 1 to a pointer, it just made that pointer point 1 byte further into memory?? I actually had a bug with that because my original thoughts were as you said (the struct example), but once I changed it to being +(1 * sizeof(myStruct) it worked. Also, I dont see how you get NULL after multiplying 0x80000000 by 2.

Of course pointers are unpredictable, however there has to be some possible application. For example suppose I was writing an operating system where memory assignment was very predictable. Then wouldn't there be an application?

shinpaughp
04-19-2003, 12:21 PM
For 32 bit processors, the maximum value of unsigned int is 0xFFFFFFFF. For the example of 0x80000000 * 2 = 0x00000000 is because of truncation. The actual answer is 0x100000000, but the 1 is truncated because of the 32 bit limit.

Just trust me, pointer multiplication would be useless. Personally, I cannot see any uses for it, too unpredictable.

As for where to read up on pointers, I dunno. Probably get yourself a good book on C and/or C++. I can't really give you any good ones because all of mine are pre-standard C++. I probably should buy one myself.

I'm sure you could probably also find some websites through google.com if you search for "C pointer" or something of the sort.

But, any programmer of C/C++ should have a good understanding of pointers, and not be afraid of using them. So, learn them from somewhere. Pointers are your friend.

Good luck! http://www.opengl.org/discussion_boards/ubb/smile.gif