Instances of Classes

Started by
16 comments, last by SamCN 21 years, 10 months ago
quote:Original post by SabreMan

Original post by George2
Also when you're passing parameter's to a function, it is best to pas a pointer to an object instead of the object it self.

It would be better still to pass a reference.

---------------end of SabreMan's quote ----------
(stupid quoting stuff)

What in your opinion makes a reference better than a pointer ?

Passing a pointer to an object is passing the objects addres, how does a reference differ from this, seeing a reference is basicly providing an alias for an object ?

[edited by - George2 on June 1, 2002 2:23:54 PM]

[edited by - George2 on June 1, 2002 2:24:38 PM]

[edited by - George2 on June 1, 2002 2:25:36 PM]
"THE INFORMATION CONTAINED IN THIS REPORT IS CLASSIFIED; DO NOT GO TO FOX NEWS TO READ OR OBTAIN A COPY." , the pentagon
Advertisement
A pointer can refer to nothing, while a reference can''t.
A pointer can easily refer to something invalid, while you really have to want to screw it when you try that with a reference.

Usually, functions that take a pointer are supposed to not cause an access violation if it is zero, while references indicate crucial arguments. Also, in the function itself, the null-checking can be skipped. In time-critical functions that could mean a pretty performance gain (besides the fact that it looks neater).
quote:Original post by Crispy

Ah! Irony I sense! Benchmark it. Pass a pointer to an object to and an actual object to a function some 10 million times, time it, and compare the results...

You''re not serious, are you?
About the benchmark or the ten million part? I am serious about the benchmark. As to the figure - go ahead try it ten times. Just remember to find a veeery slow computer...

I personally think occasianlly benching simple stuff like this is quite a bit of entertainment... Then again, maybe I''m the only one...

Crispy
"Literally, it means that Bob is everything you can think of, but not dead; i.e., Bob is a purple-spotted, yellow-striped bumblebee/dragon/pterodactyl hybrid with a voracious addiction to Twix candy bars, but not dead."- kSquared

    #include <iostream>class Foo{public:        int a[1024];};void doCopyStuff(Foo t){        return;}void doPointerStuff(Foo* t){        return;}int main(int argc, char* argv[]){        Foo t;        std::cout << "Starting 'pass by value'\n";        for(int i = 0; i < 10000000; ++i)                doCopyStuff(t);        std::cout << "Done!\n";        std::cout << "Starting 'pass by pointer'\n";        for(int i = 0; i < 10000000; ++i)                doPointerStuff(&t);        std::cout << "Done!\n";        return 0;}    

Guess which one takes ages, and which one is essentially instant? Then change a[1024] to a[128], and watch it run 10 times faster.

[edited by - Martee on June 1, 2002 6:25:36 PM]
ReactOS - an Open-source operating system compatible with Windows NT apps and drivers
What hasn''t been mentioned in this discussion is what it means to copy an object onto the stack. If you don''t pass a pointer or reference to a function then the object is passed by value. That means the copy constructor is called to create a new instance of the object! That means any changes made to the object inside the function wont effect the object that was passed to the function (just like passing an int by value, which may have been your intention). The big problem is that the default copy constructor that the compiler generates for you doesn''t do a "deep" copy (i.e. it will copy pointer members, not what they point to). So if the object does have pointer members, then anything done to those members will effect the object that was passed to the function (which is almost certainly not what you wanted). If thats the case then you should supply a copy constructor that does do a deep copy, or pass the parameter as a const pointer or reference.

As for which way is faster. I can''t see how constructing a new object can be faster than simply pushing an address onto the stack, unless the object itself only has one 32 bit member.

Cheers,

Ryan.
When things are dim give ''em VIM
quote:Original post by Crispy
About the benchmark or the ten million part? I am serious about the benchmark. As to the figure - go ahead try it ten times. Just remember to find a veeery slow computer...

I personally think occasianlly benching simple stuff like this is quite a bit of entertainment... Then again, maybe I''m the only one...

Crispy

Oh, I agree that you should benchmark before you optimize. However, it doesn''t take a profiler to realize that copying a pointer is worlds more efficient than copying a huge object.
quote:Original post by Saib
A pointer can refer to nothing, while a reference can''t.
A pointer can easily refer to something invalid, while you really have to want to screw it when you try that with a reference.

Usually, functions that take a pointer are supposed to not cause an access violation if it is zero, while references indicate crucial arguments. Also, in the function itself, the null-checking can be skipped. In time-critical functions that could mean a pretty performance gain (besides the fact that it looks neater).


Your first arguments maybe good, but optimizing time critical functions by eliminating one if-test isn''t gonna get you much of performance gain(nearly nothing). How long do you think it''s gonna take to do an if-test ?

In my opinion the -> operator is far neater then the . operator.

"THE INFORMATION CONTAINED IN THIS REPORT IS CLASSIFIED; DO NOT GO TO FOX NEWS TO READ OR OBTAIN A COPY." , the pentagon

This topic is closed to new replies.

Advertisement