3

GetTypeName is std::string, the following code

printf("%#x\n", proto->GetTypeName().c_str());
printf("%s\n", proto->GetTypeName().c_str());
const char *res = proto->GetTypeName().c_str();
printf("%#x\n",res);
printf("%s\n",res);

produces this output:

0x90ef78
ValidTypeName
0x90ef78
ю■ю■ю■ю■ю■ю■ю■ю■ю■ю■ю■ю■ю■ю■ю■ю■ю■ю■ю■ю■ю■ю■ю■ю■ю■ю■ю■ю■←ЬЬQщZ

addresses are always the same; the following code (lines are exchanges)

const char *res = proto->GetTypeName().c_str();
printf("%#x\n",res);
printf("%s\n",res);
printf("%#x\n", proto->GetTypeName().c_str());
printf("%s\n", proto->GetTypeName().c_str());

produces this output, addresses are always different:

0x57ef78
  Y
0x580850
ValidTypeName

What am I doing wrong?

strlen(res)

returns invalid size, so I can't even strcpy.

Majid Laissi
  • 19,188
  • 19
  • 68
  • 105
Dimannn
  • 73
  • 5

1 Answers1

5

YourGetTypeName function is returning an std::string and you are calling c_str to get a pointer to the internal data in that string.

As it's a temporary the std::string you return will be deleted at the end of the statement

const char *res = proto->GetTypeName().c_str();

But you still have res pointing to the now deleted data.

Edit: Change your code to something like :-

const std::string& res = proto->GetTypeName(); 

and call .c_str() on that string in the printf like this :-

printf("%#x\n",res.c_str());
printf("%s\n",res.c_str());

Assigning a temporary to a reference extends the lifetime of that temporary to be the same as the lifetime of the reference...

Better still, just use std::string and iostream for printing and stop messing about with low level pointers when unnecessary :)

jcoder
  • 29,554
  • 19
  • 87
  • 130
  • 1
    @amaurea: probably, but it has one drawback: http://stackoverflow.com/a/134777/166749 – Fred Foo Oct 31 '12 at 13:29
  • With move semantics, why not just store the returned string in an actual string? Then someone looking at the code would not have to know the signature of GetTypeName to validate the lifetime of the string... – Yakk - Adam Nevraumont Oct 31 '12 at 13:59