Passing Pointes vs not Passing Pointers

Started by
4 comments, last by dr_slash_uh 20 years, 3 months ago
I was wonder what is the performance hit of passing pointers. And I figured instead of passing pointers all the time, maybe I can define a pointer member and use that instead. I wouldn''t mind experimenting and trying for myself, but in my game I have many class templates and that would take hours to change my function parameters carefully without making a mistake. Would it be worth the performance increase. I don''t know if I am alone, but I am learning alot as I program causing me to do huge changes. class person { public: function1(int *ptr); function2(int *ptr); function3(int *ptr); function4(int *ptr); function5(int *ptr); function6(int *ptr); function7(int *ptr); function8(int *ptr); function9(int *ptr); } ///////////////////////////////// VS-VS-SV ////////////////////////////////// class person { public: person(){ptr = &addressOfSomeVariable} function1(); function2(); function3(); function4(); function5(); function6(); function7(); function8(); function9(); private: int *ptr } Thanks Alot Robert
Advertisement
passing a pointer as an argument gives you a minimal performance hit. (where "minimal" means "so insignificant that you shouldn''t waste your time worrying about it"
Any second opinions?(No offense) I have huge classes with functions that pass the same variable.

[edited by - dr_slash_uh on January 7, 2004 8:00:20 PM]
Passing a pointer should take not take any more or less time than passing any type of the same size. Often times pointers (or references) are passed to increase performance where the pointer is significantly smaller than the type (for example - a pointer to a class).

Now that that has been said, what you are trying to do is pointless. It also seems like your method of improving performance is actually slower.
-YoshiXGXCX ''99
As some people have said, passing pointers has a minimal performance hit. However, I think your question really is about design.

Assuming that ptr points to a member variable, why would you pass the address of a member variable to a member function regardless the fact that the member function can access the variable directly?

If ptr doesn''t point to a member variable but external variable, you still need to have a function like set_ptr(int* foo). You are still passing a pointer to a function.
Yea, the pointer actually points to another class. Thanks for the input.

Robert

This topic is closed to new replies.

Advertisement