RE: Uses for pointers

Started by
25 comments, last by stubble 20 years, 10 months ago
My post was exclusively for C and not C++. In C the declaration swap (int &a, int &b) is invalid as far as i know.

Regards.

Advertisement
This:
quote:
void swap (int &a, int &b){
int temp = a;
a = b;
b = temp;
}


and this:
quote:
inline void swap (int &a, int &b){
__asm
{
mov ax, a
mov bx, b
mov a, bx
mov b, ax
}
}


are the same on 99% of compilers, except you need to use eax and ebx. (actually, whoever wrote the ''what does a compiler do'' thread should look at this as an example of an optimization)
Back to the topic:

When I was learning pointers, the way I learned their uses was to imagine that if a function needed to "save" a value into one of the arguments, I had to use pointers.

Imagine this function: GetPlayerPosition(). Assuming it''s return value is bool (for success or failure), how do you return 2 values from this function? You can''t. But you CAN pass 2 pointers to 2 integers and then de-reference (de-pointer?) them to save their value into the arguments. So your function looks like this:

bool GetPlayerPosition(int* x, int* y)
{
//stuff
*x = player_x;
*y = player_y;
}

You just ''saved'' the value into the arguments themselves. References are just like pointers, only the compiler does all the de-referencing for you.

Does this make sense?
quote:Original post by kdogg
...

Well, yeah, that''s why there''s pretty much no reason to use inline assembly anymore. I was just playing around.

As for eax, aren''t ints 16-bit by default? I think you have to use unsigned long to get a 32-bit variable.

As far as MSVC goes, check MSDN:

int, unsigned int - 4 bytes
long, unsigned long - 4 bytes


They''re the same thing, really. This is MS specific though, so YMMV with other compilers.

Wielder of the Sacred Wands
[Work - ArenaNet] [Epoch Language] [Scribblings]

This is news to me.

What''s the point of all these random typedefs if everything always uses one type?! Then BOOL == int == DWORD... mind-boggling.
Also - don''t forget that you''ll have tough time using polymorphism in C++ without using pointers.

So, for example, you might have an array of pointers to base a class - say "Shape". You want to loop through this array and call the virtual render() method so the shape draws itself. In reality the pointers will point a variety of subclasses of "shape" - eg square, circle, triangle - all of which know how to render themselves.

This just wouldn''t be possible without pointers for a variety of reasons, including:

1) the amount of memory that each subclass needs may be different - so they couldn''t be held in the same array explicitly - as the element size would change. The good thing about a pointer is that it is always the same size - no matter what it points to - so each array element is the same size.

2) virtual tables are only used on pointers and references

3) References (&) can not be re-assigned - so they are not very useful for this sort of thing either

This topic is closed to new replies.

Advertisement