Connecting Tech Pros Worldwide Help | Site Map

Probably an easy one for you but pls check

  #1  
Old May 26th, 2006, 11:45 AM
johan.ekman@gmail.com
Guest
 
Posts: n/a
I found a function in our code with a similar principle as:

const char* MyString() const
{
return "Hello!";
}

I believe this function is wrong because "Hello" string would be
allocated locally on the stack ?!? (right) and therefore the method
would return a pointer to an object that not exists outside this
method.

Doesn't that mean that a call such as:

sprintf( MyString() );

might output nonsense ?

Thanks / Cooper

  #2  
Old May 26th, 2006, 11:55 AM
Rolf Magnus
Guest
 
Posts: n/a

re: Probably an easy one for you but pls check


johan.ekman@gmail.com wrote:
[color=blue]
> I found a function in our code with a similar principle as:
>
> const char* MyString() const
> {
> return "Hello!";
> }
>
> I believe this function is wrong because "Hello" string would be
> allocated locally on the stack ?!?[/color]

No. Note that the only "stack" that C++ knows about is the standard class
std::stack. There is no need for a C++ implementation to store local
variables on any kind of stack.
Also, string literals have static storage duration. They exist until the end
of the program execution. When the function is called, a pointer to the
first element of that literal is returned, and that pointer is perfectly
valid.
[color=blue]
> (right) and therefore the method would return a pointer to an object that
> not exists outside this method.
>
> Doesn't that mean that a call such as:
>
> sprintf( MyString() );
>
> might output nonsense ?[/color]

No.

  #3  
Old May 26th, 2006, 02:45 PM
Ratan
Guest
 
Posts: n/a

re: Probably an easy one for you but pls check


This will work fine because the return value is available on the stack.

But same function will NOT work if one is using like...

char *Temp = MyString();
Here the Temp contains Junk value.

  #4  
Old May 26th, 2006, 02:55 PM
Rolf Magnus
Guest
 
Posts: n/a

re: Probably an easy one for you but pls check


Ratan wrote:
[color=blue]
> This will work fine because the return value is available on the stack.[/color]

Wherever it is, the return value is a pointer that is copied on return. You
can just copy the pointer value into another pointer. The data pointed to
will be - as I already said - available for the rest of the program's
execution time.
[color=blue]
> But same function will NOT work if one is using like...
>
> char *Temp = MyString();[/color]

That's right...
[color=blue]
> Here the Temp contains Junk value.[/color]

.... but for another reason. You can't copy the returned pointer value to
Temp, because you ignored the const.


  #5  
Old May 26th, 2006, 02:55 PM
Roland Pibinger
Guest
 
Posts: n/a

re: Probably an easy one for you but pls check


On 26 May 2006 03:44:24 -0700, johan.ekman@gmail.com wrote:[color=blue]
>I found a function in our code with a similar principle as:
>
>const char* MyString() const
>{
> return "Hello!";
>}
>
>I believe this function is wrong because "Hello" string would be
>allocated locally on the stack ?!? (right)[/color]

"Hello!" is a string literal which is neither created on the stack nor
allocated on the heap. It is stored in a global area of your program.
[color=blue]
>and therefore the method
>would return a pointer to an object that not exists outside this
>method.[/color]

"Hello!" exists outside of MyString(). The function returns a pointer
to a 'global' sting literal.
[color=blue]
>Doesn't that mean that a call such as:
>sprintf( MyString() );
>might output nonsense ?[/color]

Don't confuse it with erroneous code like:

const char* MyString() const {
char a[] = "Hello!";
return a; // error!!
}

Best wishes,
Roland Pibinger
  #6  
Old May 26th, 2006, 03:45 PM
Richard Herring
Guest
 
Posts: n/a

re: Probably an easy one for you but pls check


In message <1148648621.505799.16380@g10g2000cwb.googlegroups. com>, Ratan
<ra10kumar@gmail.com> writes[color=blue]
>This will work fine because the return value is available on the stack.[/color]

C++ has no concept of a "stack".[color=blue]
>
>But same function will NOT work if one is using like...[/color]

[restoring some context here - please learn how to quote what you are
replying to]
[color=blue][color=green][color=darkred]
>>>const char* MyString() const
>>>{
>>> return "Hello!";
>>>}[/color][/color][/color]
[color=blue]
>
> char *Temp = MyString();
>Here the Temp contains Junk value.
>[/color]
Wrong. Putting aside the "const" error, what MyString() returns is a
copy of a temporary pointer to a static null-terminated byte sequence
initialised to "hello!\0". The fact that the temporary pointer used
within the function has vanished makes no difference: what it pointed to
is static and still there.

If you correct Temp's type to const char *, it will contain a valid
pointer to that sequence.

--
Richard Herring
  #7  
Old May 26th, 2006, 04:35 PM
Tomás
Guest
 
Posts: n/a

re: Probably an easy one for you but pls check


> const char* MyString() const[color=blue]
> {
> return "Hello!";
> }
>
> I believe this function is wrong because "Hello" string would be
> allocated locally on the stack ?!? (right) and therefore the method
> would return a pointer to an object that not exists outside this
> method.[/color]


Here's how string literals work under the hood:


char const string_literal_1[] = "Hello!";
/* This is a global variable */


const char *SomeClass::MyString() const
{
return string_literal_1;
}


Now you can see why the string is valid for the entire program.

-Tomás
Closed Thread


Similar Threads
Thread Thread Starter Forum Replies Last Post
Seperate words cmrhema answers 5 July 21st, 2007 04:40 AM
Python syntax in Lisp and Scheme mike420@ziplip.com answers 699 July 18th, 2005 05:41 AM