Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations MikeeOK on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Rules for when to return pointer or address of 1

Status
Not open for further replies.

titanandrews

Programmer
Feb 27, 2003
130
US
Hi,
This should be a simple enough question for the C++ veterans...
If I have a function that returns something, how do I know when I should return a pointer (*) to the object or the address (&). It seems like most of the code I have seen in books when returning a string, they return the address of. But, if I return the address of let's say a list<string>, compiler gives me a warning saying &quot;warning C4172: returning address of local variable or temporary&quot; Now this does make sense, because I believe that once the block (function) has exited the destructor will get called on the local object which means that whoever gets the address of this object is not going to like it. What doesn't make sense to me is why the compiler does not complain about returning address of string object.
I appreciate your help!



thanks,

Barry
 
If you return a pointer to a string this may be ok, if the pointer points to a string &quot;that's always there&quot; (like a const string in memory) :
{
char * localptr = &quot;ALWAYS THERE&quot;; // Const memory
return localptr; // Ok
}

If the string is allocated on the stack you have a problem :
{
char localstring[32]; // Stack variable
strcpy(localstring, &quot;TempData&quot;);
return localstring; // Not Ok.
}
because the stack will be resued for other things when the function returns.

The same goes for a local string object : It will be gone with the stack reuse. However you could return the entire string object which will be copy-constructed during before exiting the function :
{
string s;
s = &quot;abc&quot;;
s += &quot;def&quot;;
return s; // Copy constructed
}

By the way : A pointer * and address of & is the same thing.

/JOlesen
 
&quot;A pointer * and address of & is the same thing&quot;

Whoa!!! Now you're confusing me. If that's true, it seems like they would be interchangeable. The following code will not compile because of the returnAddres() function which is expecting to return address of, but I return pointer instead. Could you please clarify what you mean by they are the same?

As to your other explanation, I think I understand. But how do you know if it's const memory or a stack variable?
I really appreciate your time!

Barry


Code:
int* returnPointer()
{
    int myInt = 0;
    int *pMyInt = &myInt;

    return (pMyInt);
}


//Does not work
int& returnAddress()
{
    int myInt = 0;
    int *pMyInt = &myInt;

    return (pMyInt);    
}


int main(int argc, char *argv[])
{

}
 
From your original post :
>>how do I know when I should return a pointer (*) to the object or the address (&)

Maybe I was a little unprecise : To return a pointer to an object or return the address of an object : That's the same thing.

An automatic variable located within the scope of a function is always stack-based.

I think you will always be safe if you follow this rule :
Dont ever return the address of an object located within a function unless the object is static. Anyone : Please correct me if you know an exception to this.

Again from your original post :
>>What doesn't make sense to me is why the compiler does not complain about returning address of string object

Please post an example.


//Does not work
int& returnAddress()
{
int myInt = 0;
int *pMyInt = &myInt;

return (pMyInt);
}

Here you do not return a reference to an integer like you are supposed to. You return a reference to a pointer - which by the way is also unsafe because the pointer is located on the stack.


/JOlesen

 
Here you do not return a reference to an integer like you are supposed to. You return a reference to a pointer - which by the way is also unsafe because the pointer is located on the stack.

Light bulb!
Now it's making sense. I believe I understand this whole concept of reference and pointers now (mostly). I am going to play around a bit more just to make sure I know exactly what's going on.

Don't worry about the example of returning reference to string. I was wrong. It gives the same warning as anything else.



thanks,

Barry


 
>>Here you do not return a reference to an integer like you are supposed to. You return a reference to a pointer - which by the way is also unsafe because the pointer is located on the stack.

Very very sorry if this will confuse you ! It should read :

Here you do not return a reference to an integer like you are supposed to. You return a pointer (to an integer) - which by the way is also unsafe because the value being pointed at is located on the stack - i.e. you return the address of a local stack-based variable.

/JOlesen
 
If you return std::string class object, it IS NOT &quot;the address of&quot; only. STL class string object is MORE than &quot;address of&quot;, and return statement has a copy (constructor) semantics. So, you may return any local std::string variable: caller gets his copy incarnation of it and it's his right to call destructor (of this copy) appropriately. But C-style &quot;string&quot; == (char*) or (const char*) is true pointer (only), it's wrong to locate (killed by return) locales via returned value. May be these two distinct string terms are the cause of the trouble?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top