Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

Esap1

Simple Stupid Question

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

Share this post


Link to post
Share on other sites
Advertisement
Well, inless you have a parameter less than 16bit, because then it would be faster to just take the number, right?

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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...)

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!