Why does this work? (returning automatic string)

P

Phlip

Bryan said:
std::string foo()
{
char buf[16];
strcpy(buf, "this is a test.");
return std::string(buf);

}
it appears to me that foo should return garbage. however, "this is a test"
is printed correctly.
what am i missing here?

Google for "copy constructor". Then run that code thru the debugger and
trace inside the return statement to see one in action.

(Introductory C texts warn if you return buf, a character array, you get
undefined behavior; that's probably what you accidentally expected here.)
 
B

Bryan Bullard

#include <cstring>
#include <iostream>
#include <string>

std::string foo()
{
char buf[16];

strcpy(buf, "this is a test.");

return std::string(buf);

}

int main()
{
std::cout << foo();

return 0;
}

it appears to me that foo should return garbage. however, "this is a test"
is printed correctly.
what am i missing here?

thanks in advance,
bryan
 
A

Aggro

Bryan said:
strcpy(buf, "this is a test.");

Here we copy 'this is a test\0' string to buf. (Note that the string
will be null terminated.)
return std::string(buf);

And here we return the null terminated string.
}

int main()
{
std::cout << foo();

And here we print it.

Everything looks fine to me. Why shouldn't this work?
 
J

Jeff Schwab

Aggro said:
Here we copy 'this is a test\0' string to buf. (Note that the string
will be null terminated.)



And here we return the null terminated string.

No, "we" don't. We return a std::string object, which happens to be
constructed from a null-terminated sequence of characters.
And here we print it.

Here we print a copy of the std::string returned by the function.

A std::string object isn't an array, although it may contain one. The
local object in the function is destroyed when the function returns, as
the OP expects. The value is copied to the caller's space, though, and
the copy can be printed, assigned, or whatever else one can do with a
string. This is the same thing that happens for other copy-by-value
types, e.g. int's.
 
K

Karl Heinz Buchegger

Bryan said:
#include <cstring>
#include <iostream>
#include <string>

std::string foo()
{
char buf[16];

strcpy(buf, "this is a test.");

return std::string(buf);

This creates a std::string object.
Since the return type of this function specifies
std::string (and not a reference or a pointer) a
*copy* of that object is passed back to main.
}

int main()
{
std::cout << foo();

and here the std::string object returned from foo
is used for doing the output. Since the return value
of foo isn't used anywhere else, the temporary object
returned by foo is destroyed after the statement has
executed.
 
B

Bryan Bullard

thanks for your post.

you make a point that the string is null terminated. however, i don't be
that should make a difference.

char buf[16]; // is automatic storage
strcpy(buf, "..."); // copies the literal into the automatic storage.
return std::string(buf); // constructes std::string based on automatic
storage.

does std::string create its own static copy? otherwise, it would seem to me
to be invalid.

-bryan
 
B

Bryan Bullard

thanks for the clarification.
Bryan said:
std::string foo()
{
char buf[16];
strcpy(buf, "this is a test.");
return std::string(buf);

}
it appears to me that foo should return garbage. however, "this is a test"
is printed correctly.
what am i missing here?

Google for "copy constructor". Then run that code thru the debugger and
trace inside the return statement to see one in action.

(Introductory C texts warn if you return buf, a character array, you get
undefined behavior; that's probably what you accidentally expected here.)
 
A

Andre Kostur

thanks for your post.

you make a point that the string is null terminated. however, i don't
be that should make a difference.

char buf[16]; // is automatic storage
strcpy(buf, "..."); // copies the literal into the automatic storage.
return std::string(buf); // constructes std::string based on
automatic storage.

does std::string create its own static copy? otherwise, it would seem
to me to be invalid.

Yes, std::string takes its own copy of the array into its own storage.

So your function will construct a string from the array, copy that string
into some temporary variable (to carry the return-by-value for your
function), which is then output via cout. (Some copies may be skipped due
to RVO).
 
B

Bryan Bullard

my question was answered, thanks.
thanks for your post.

you make a point that the string is null terminated. however, i don't be
that should make a difference.

char buf[16]; // is automatic storage
strcpy(buf, "..."); // copies the literal into the automatic storage.
return std::string(buf); // constructes std::string based on automatic
storage.

does std::string create its own static copy? otherwise, it would seem to me
to be invalid.

-bryan

Here we copy 'this is a test\0' string to buf. (Note that the string
will be null terminated.)


And here we return the null terminated string.


And here we print it.

Everything looks fine to me. Why shouldn't this work?
 
K

Karl Heinz Buchegger

Bryan said:
thanks for your post.

you make a point that the string is null terminated. however, i don't be
that should make a difference.

char buf[16]; // is automatic storage
strcpy(buf, "..."); // copies the literal into the automatic storage.
return std::string(buf); // constructes std::string based on automatic
storage.

does std::string create its own static copy?

Yes it does.

And please: Don't top post. Put your reply beneath the text
you are replying to.
 

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
474,159
Messages
2,570,881
Members
47,418
Latest member
NoellaXku

Latest Threads

Top