Probably an easy one for you but pls check

J

johan.ekman

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
 
R

Ratan

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.
 
R

Roland Pibinger

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)

"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.
and therefore the method
would return a pointer to an object that not exists outside this
method.

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

Don't confuse it with erroneous code like:

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

Best wishes,
Roland Pibinger
 
R

Richard Herring

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

C++ has no concept of a "stack".
But same function will NOT work if one is using like...

[restoring some context here - please learn how to quote what you are
replying to]
char *Temp = MyString();
Here the Temp contains Junk value.
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.
 
T

Tomás

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.


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
 
R

Rolf Magnus

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 ?!?

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.
(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 ?

No.
 
R

Rolf Magnus

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

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.
But same function will NOT work if one is using like...

char *Temp = MyString();

That's right...
Here the Temp contains Junk value.

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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top