Advertisement

Simple Stupid Question

Started by May 07, 2000 03:25 PM
18 comments, last by Esap1 24 years, 7 months ago
I was thinking, for every function you write, wouldnt it be faster to make every parameter a pointer. Or does this not make sence, and are there reasons not to, just thinking
Well, inless you have a parameter less than 16bit, because then it would be faster to just take the number, right?
Advertisement
Yah, that''s what you should *ALWAYS* do.
In your x86 Win32 programs, a pointer is 32bits, so you''ll push a full word on the stack for every parameter no matter what size it is. So you don''t gain anything for chars, short ints or long ints. Furthermore every computation involving the argument will have to be indirected, which wastes clock cycles. Also because your arguement values are no longer all on the stack frame, you''ve increased the size of your working set by the size of your pointers, which leads to increased chances of L1 cache misses.
Most of the time it''s better NOT to use pointers as parameters, when it''s not needed!

In some cases it can be better. For example: when you are using lots of variables, and put them all in a structure. Then use only the struct pointer as a single parameter.

Hope it helps,
Bas.
what about using pass by reference? (C++)

i would assume it''s pretty much the same as passing pointers around, except a bit easier to code. but maybe someone here knows if there is a difference?


nathany.com
Advertisement
Good point nathany,
C++ compilers internally add an object pointer to their function calls.

A struct is like a class.
A struct pointer is like an C++ object.
An object only has some more information (function pointers, maybe classID, and maybe even more?)

C++ is the perfect solution!
(But still... ptrs as parameters... not so good!)
(If you want speed... C is the best... or assembly... but nowadays will get your head spinning...)
Call by reference in most implementations of C++ is essentially syntactic sugar around passing pointers. However, I''ve seen implementations where references are actually pointers to pointers, making using call by reference a lot more expensive.
Ok, a couple of points here...

"C++ compilers internally add an object pointer to their function calls."
... this is misleading. They only add an object pointer where one is needed, and that is for member functions. So this:

void MyClass::SomeFunction(int param1, int param2);

ends up -similar- to this:

void SomeFunction(MyClass* this, int param1, int param2);

They wouldn''t change a global or static function.

Passing -everything- by pointer would mean dereferencing it all at the other end. This would incur a slight performance hit. I do believe Java passes nearly everything by reference, though.

The last point I want to make is that the MSVC docs mention actually passing a pointer for you behind the scenes, even though you say to pass the whole struct. Look into the calling convention section of the docs to find out more.
The only time a pass by reference or use pointers is when I want to change the value of a variable and want to return the new value, or when I am passing classes and structs.
Like it was said earlier by Sicrane, pointers are 32bits and you don''t get any speed increase or save the stack by using variables whose sizes are <= 32 bits.

Domini

This topic is closed to new replies.

Advertisement