Returning String Literals from a function

S

Siam

Hi all...

What's the best way to return some string from a function (without
using the string class). Would the following work:

char* getString( )
{
return "TheString";
}

I've seen this in code in a lot of places, but I would have thought the
string literal in the function would be allocated on the stack, a
pointer to the string will be returned, and the block of memory holding
the string would be deallocated when the function returns... Would it
make a difference if the return type was const char* ?

If this doesn't work, the only other way I can think of is to either
dynamically allocate a new string in the function (in which case it
wouldn't be clear to the user to delete it), or to make them pass a
reference to a string and strcpy "TheString" into it (which I find
annoying)... Is there any other way?
 
T

tragomaskhalos

Siam said:
Hi all...

What's the best way to return some string from a function (without
using the string class). Would the following work:

char* getString( )
{
return "TheString";
}

I've seen this in code in a lot of places, but I would have thought the
string literal in the function would be allocated on the stack, a
pointer to the string will be returned, and the block of memory holding
the string would be deallocated when the function returns... Would it
make a difference if the return type was const char* ?

Don't worry, the code is fine. The string literal itself is stored
globally, the function just returns a pointer to that global location.
Returning const char* would be better though, as the returned string
should not be modified by the caller.
 
F

Frederick Gotham

Siam posted:
Hi all...

What's the best way to return some string from a function (without
using the string class). Would the following work:

char* getString( )
{
return "TheString";
}


Put a "const" in there:

char const *GetStr()
{
return "Hello";
}

If it helps your understanding, you could think of the snippet above as
being equivalent to:

char const str[] = "Hello";

char const *GetStr()
{
return str;
}

You can see that you're simply returning the address of a global object --
so no memory is leaked, and no object is accessed after having being
destroyed.
 
S

Siam

Cheers.. One more question.. Would the following code be alright?

char* getString( )
{
string s = "TheString"; //using string class
return s.c_str();
}

Does c_str() create a global string literal and return its address, or
is this one allocated on the stack?

Cheers :)

Siam
 
V

Victor Bazarov

Siam said:
Cheers.. One more question.. Would the following code be alright?

char* getString( )
{
string s = "TheString"; //using string class
return s.c_str();
}

No, most certainly not.
Does c_str() create a global string literal and return its address, or
is this one allocated on the stack?

Unknown. It's magic. But in your case 's' is still a local object, and
it's destroyed when the fucntion ends. Whatever 'c_str' returns is tied
to that object, so the pointer you get is invalid as soon as the function
returns.

V
 
T

tragomaskhalos

Siam said:
Cheers.. One more question.. Would the following code be alright?

char* getString( )
{
string s = "TheString"; //using string class
return s.c_str();
}
Does c_str() create a global string literal and return its address, or
is this one allocated on the stack?

No you can't do this. In your string class example above the global
literal "TheString" gets copied into some memory managed by the
std::string s , c_str() then returns a pointer to this memory
(technically it might be a copy again, but let's ignore that
complexity), but once you return from the function the std::string s
goes out of scope, and the memory it manages is effectively lost. Hence
any attempt to use the return from this function will likely blow up in
your face.
 
F

Frederick Gotham

Siam posted:
char* getString( )


Stop using "pointer to non-const" to store the address of a string literal --
it's sloppy. The following code demonstrates:

char *PoorlyWrittenFunction()
{
return "I'm a string literal, and am inherently const, "
"even though the C type system doesn't acknowledge"
"it.";
}

int main(void)
{
*PoorlyWrittenFunction() = 'e';
}


The above code compiles without error, but invokes undefined behaviour
nonetheless.
 

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

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,905
Latest member
Kristy_Poole

Latest Threads

Top