Why would you ever... Return a Pointer or reference..

Ok, so I understand pointers and references, ive used them before plenty of times. Thing is that I just doin't understand the concept of returning a pointer or a reference..

Are you trying to return a dynamic allocated object??? To an awaiting pointer that points to allocated stack memory... Thats the only reason I would think is necessary to return a pointer or reference.

But can anybody explain to me other reasons for returning a pointer or reference.

Re: Why would you ever... Return a Pointer or reference..

Posted 11 January 2013 - 01:46 PM

Let me elaborate a bit more in mojo666's example above. The find() could be implemented to return a copy of the entire record. But if the record was 64KB, now you'll have 64KB on the stack for the returned record, and at least another 64KB for whatever data structure is holding the record. Consider than on Linux, a typical stack size is much less than 64KB. On Windows 64KB is the typical size, but your other variables will also be using the stack. Also consider the time it will take to copy 64KB around. So the better implementation is to return a pointer or reference to the block so that instead of returning 64KB, only 4 or 8 bytes is returned.

Without pointers, consider how you would implement a linked list or tree.

Re: Why would you ever... Return a Pointer or reference..

Posted 11 January 2013 - 01:49 PM

The [] operator of container classes returns a reference, so that you can write things like my_collection[x] = y;. Generally whenever you want to be able to assign to the result of a function call, the function needs to return a reference.

Another reason why you'd want to return a reference would be to avoid creating copies. If you return one of your parameters (assuming they're references too) or something else which you know will live longer than your function (i.e. something that's not a local variable), it can make sense to return by (const) reference for that reason.

Skydiver, on 11 January 2013 - 09:46 PM, said:

So the better implementation is to return a pointer or reference to the block so that instead of returning 64KB, only 4 or 8 bytes is returned.

Perhaps more importantly a find method that returned by value would be useless if you intend on modifying the record you found.

Re: Why would you ever... Return a Pointer or reference..

Ok, so I understand pointers and references, ive used them before plenty of times. Thing is that I just doin't understand the concept of returning a pointer or a reference.

if you understand pointers/references they surely you understand the need to return them from functions! They are forms of variables and you will need to manipulate them and that generally means passing them into and out-of functions.

I personally love to return references from function, ESPECIALLY member functions that return a reference to *this!

--but you see the point here is not tricks you can play with syntax, the point is that pointers and references are variables and manipulating the data in variables is what programming is really kind of all about. So IF you understand pointers and references then returning them from functions is just something you find yourself doing.