safe to return a 'const char *' from a local std::string?

S

Sims

Hi,

if i have a code

const char * GetValue()
{
std::string szVectorValue = ...// get a std::string from the vector
return szVectorValue.c_str();
}

Is the code above safe? I think not because the value 'szVectorValue' will
be released making the const char garbage.

i could do

std::string GetValue()
{
std::string szVectorValue = ...// get a std::string from the vector
return szVectorValue;
}

but the problem is that i am using sprintf( ... )

and it would be simpler/clearer if instead of 'GetValue().c_str()' i could
do 'GetValue()'

Many thanks for your input.

Sims
 
V

void

U¿ytkownik Sims napisa³, On 2004-01-27 12:12:
Hi,

if i have a code

const char * GetValue()
{
std::string szVectorValue = ...// get a std::string from the vector
return szVectorValue.c_str();
}

Is the code above safe? I think not because the value 'szVectorValue' will
be released making the const char garbage.
This code isn't safe, szVectorValue is on stack and when you return from
this function it simply don't exists, so all you get is garbage...
i could do

std::string GetValue()
{
std::string szVectorValue = ...// get a std::string from the vector
return szVectorValue;
}

but the problem is that i am using sprintf( ... )

and it would be simpler/clearer if instead of 'GetValue().c_str()' i could
do 'GetValue()'
If you don't want 'GetValue().c_str()' you could try something like this

const char * GetValue()
{
static std::string szVectorValue ;
szVectorValue = ...// get a std::string from the vector
return szVectorValue.c_str();
}
but I'm not sure it is better solution than 'GetValue().c_str()'.
Best regards
Darek Ostolski
 
S

Sims

std::string GetValue()
If you don't want 'GetValue().c_str()' you could try something like this

const char * GetValue()
{
static std::string szVectorValue ;
szVectorValue = ...// get a std::string from the vector
return szVectorValue.c_str();
}

Thanks.

But that's also on the stack so will also become garbage, won't it?

Sims
 
V

void

U¿ytkownik Sims napisa³, On 2004-01-27 12:25:
Thanks.

But that's also on the stack so will also become garbage, won't it?

Sims

No it's static, so it isn't on stack, and you don't get garbage.
This static string is really bad idea. You have one szVectorValue in
your program, so if szVectorValue need to reallocate memory, you will
have garbage previously returned. This also means that if you call
GetValue two times, previously returned value will be 'magicly'
replaced with last returned value. Is is better idea to return copy of
std::string and call it by 'GetValue().c_str()'.

Best regards
Darek Ostolski
 
A

Andrey Tarasevich

Sims said:
Thanks.

But that's also on the stack so will also become garbage, won't it?
...

No, it's 'static' which means that it is not on stack. However, in
general case writing non-reentrant functions is a bad idea unless you
have a good reason to do it. Personally, I wouldn't consider the above
problem to be a good reason for this type of trick.
 
S

Sims

No, it's 'static' which means that it is not on stack. However, in
general case writing non-reentrant functions is a bad idea unless you
have a good reason to do it. Personally, I wouldn't consider the above
problem to be a good reason for this type of trick.

So do you also suggest i do 'GetValue().c_str()' rather than try to be lazy?

Sims
 
A

Andrey Tarasevich

Sims said:
So do you also suggest i do 'GetValue().c_str()' rather than try to be lazy?
...

Yes. Using C standard library functions with C++ standard library
classes is supposed to be cumbersome, since this is not exactly a
natural combination :)
 
J

Jorge Rivera

Sims said:
i could do

std::string GetValue()
{
std::string szVectorValue = ...// get a std::string from the vector
return szVectorValue;
}

but the problem is that i am using sprintf( ... )


Do you really need to use sprintf??? You can use ostringstream to do
string formatting, and you can forget about your .c_str() problems.

Just a though, I have no idea what you are trying to accomplish.

Jorge L
 

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,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top