🔒 Locked
References & Pointers .. which one ?
Advertisement
Original Post
I am using ALOT of pointers in my script engine''s code, im making it in C++ and i used pointers all over the place because of old habits.
most of my functions are outside classes (globals using hungarian notation) and almost all of them have:
function(script_object* obj,...)
should i be using:
function(script_object& obj,...)
is there any speed difference ??
is it worth changing my 4000 lines of code ??
i know its a little late, but i really want to get my script execution down to 30ms (its at 70-80ms)
No, you won''t get any speed difference by changing all your code from pointers to references. Behind the scenes, it is exactly the same thing. If you really want to speed up execution, you should first try profiling your app while it is running and see where it is actually spending its time. In most cases, changing an algorithm will buy you much more speed than optimizing through ''tricks''.
thanx, how do i understand this profiling info MSVC gives me ??
I personally use references, because there are times you *have* to use pointers. This kind of catagorize my code.
Just this, I think its worth it to change your 4000 lines of code. Just lots of replace all...remember to change the lines you called you fuctions. Maybe you should write a script to do it? (might be useful later on)
About performance...I know there is a overhead of dereferencing (I think its 9ns or even less?), but I dont see why this doesnt apply to references.
e.g.
requires you to de-reference 3 times.
void function( object* data )
{
data->func1();
data->func2();
data->func3();
return;
}
Is this the same as de-referencing 3 times?? (I think so, but correct me if I am wrong)
void function( object& data )
{
data.func1();
data.func2();
data.func3();
return;
}
Just this, I think its worth it to change your 4000 lines of code. Just lots of replace all...remember to change the lines you called you fuctions. Maybe you should write a script to do it? (might be useful later on)
About performance...I know there is a overhead of dereferencing (I think its 9ns or even less?), but I dont see why this doesnt apply to references.
e.g.
requires you to de-reference 3 times.
void function( object* data )
{
data->func1();
data->func2();
data->func3();
return;
}
Is this the same as de-referencing 3 times?? (I think so, but correct me if I am wrong)
void function( object& data )
{
data.func1();
data.func2();
data.func3();
return;
}
The fundamental difference between a Reference and a Pointer is that the Reference can be assigned an rvalue only once (when the object containing it is created), whereas the Pointer can be assigned and re-assigned at will and does not need to be initialised when it is declared (genarally considered to be a poor programming practice, so always initialise your pointers, either with valid data or with "0").
Roo, Pantheon Software
Roo, Pantheon Software
Please correct me if I am wrong, but I thougt using reference to a variable was the EXACT same thing as using the variable. In other words, a reference is just an alias for the variable which is decided at compile time and thus it''s impossible to change it after inialisation.
When the compiler generates the code all variables are converted to memory addresses right? and a reference are converted to same address as the variable it''s referencing to.
This is how I understood it...
Are there any c++ guru here who can tell how it is?
Johan Ersvik
When the compiler generates the code all variables are converted to memory addresses right? and a reference are converted to same address as the variable it''s referencing to.
This is how I understood it...
Are there any c++ guru here who can tell how it is?
Johan Ersvik
That is how it is. What Roo said was also true. Syntactically, a reference is just the same as the original variable. Implementationally, passing a reference is only as expensive as passing a pointer, which may be more efficient than passing a struct or class in many cases.
[ MSVC Fixes | STL | SDL | Game AI | Sockets | C++ Faq Lite | Boost | Asking Questions | Organising code files ]
[ MSVC Fixes | STL | SDL | Game AI | Sockets | C++ Faq Lite | Boost | Asking Questions | Organising code files ]
Well, that is not quite the case. This is because there is no possible way for a program to be able to allocate a definate memory address for any variable. Dynamic memory is taken randomly from the heap and local variables in functions are taken from the next available location in the stack. Therefore, a reference is a pointer, but only requires one time initialization. Personally, I like pointers better because it is a better representation of what is actually happening whereas a referenced variable could look like a local variable or a something that is passed through arguments. I hate having to look at old code that I don''t remember very well and deciding what is a reference and what is a local variable.
It is true that behind the scenes they are EXACTLY the same thing.
It is true that behind the scenes they are EXACTLY the same thing.
quote:
This is because there is no possible way for a program to be able to allocate a definate memory address for any variable.
Sure, but a reference need not to be allocated, it is the same as the variable, and if it wasn''t for scope you could replace all references with the variable name(in theory)and it would be the same assembler anyway, or what...!?
And... referencing should be slightly faster as it doesn''t require dereferencing a pointer!(?)
Johan Ersvik
If the function does not modify the parameter in any way, you should pass by constant reference.
If the function assumes the object is always valid, you might want to pass-by-reference - it''s a good idea to name the function such that it indicates that the parameter will be changed in this case.
If a null value is a valid case, use a pointer. Or use a pointer if you want to make it clear that you may modify the underlying object.
They also require the same dereferencing a pointer does - it just hides it from you. In special cases the dereferencing can be optimized out if a reference is used instead of a pointer (such as referencing a local variable).
If the function assumes the object is always valid, you might want to pass-by-reference - it''s a good idea to name the function such that it indicates that the parameter will be changed in this case.
If a null value is a valid case, use a pointer. Or use a pointer if you want to make it clear that you may modify the underlying object.
quote:
Original post by Ersvik
And... referencing should be slightly faster as it doesn''t require dereferencing a pointer!(?)
They also require the same dereferencing a pointer does - it just hides it from you. In special cases the dereferencing can be optimized out if a reference is used instead of a pointer (such as referencing a local variable).
quote:
Original post by Ersvik
int a = 0;
int& b = a;
when stepping through this code you could have a watch on the addresses of a and b, and they are equal! A reference is an alias, not a pointer, and thus it shouldn''t need dereferencing.
Johan Ersvik
we''re talking about references as functionparameters. references like this are the same as #define b a in the end, as we all know compilers do optimize..
but if you have a function
void func(somestruct& ref) {
}
this is the same as
void func(somestruct* ref) {
}
technically. they have to give the pointer to the object the reference goes to, else the func cannot know where its original somestruct is..
"take a look around" - limp bizkit
www.google.com
quote:
Original post by Ersvik
When the compiler generates the code all variables are converted to memory addresses right? and a reference are converted to same address as the variable it''s referencing to.
As I understand things, the reference variable holds the address of the item it is a reference to. That being so, the program code would load the value of the reference into one of its internal address registers and then uses that as a base pointer to access the member functions and data being referenced. In other words, some dereferencing takes place. I guess the only way to know for sure is to decompile some test code and see what it''s doing.
Roo, Pantheon Software
Topic Locked
This topic has been locked by a moderator. New replies are not allowed.
Sign in to reply to this topic.
Advertisement