On Mar 18, 2009, at 9:33 PM, Alvin ONeal wrote:

> Can someone please tell me what the best practice / correct way of  
> doing this is? I want the error I'm getting to go away.
>
> char* myCString = "My C string";
>
>
> I think I've used one of these before to avoid the message... but it  
> just seems a cludgy way to do things.
> // This
> char * myCString = string("My C string").c_str()

I know that your problem is already solved, but I wanted to point out  
another problem with this approach. The char* returned by c_str() is  
only guaranteed to point to valid data for the life of the std::string  
that it came from. In this case, the std::string is deleted at the end  
of the line, so your myCString variable will be pointing to garbage.  
The odd thing is that it may appear to work at first, since the memory  
won't necessarily be overwritten immediately, but eventually it will  
be, and you will have a nasty problem on your hands.

This bit Byron and myself on an embedded project at work a few months  
back, entirely my fault of course. :)

--Dave
--------------------
BYU Unix Users Group 
http://uug.byu.edu/ 

The opinions expressed in this message are the responsibility of their
author.  They are not endorsed by BYU, the BYU CS Department or BYU-UUG. 
___________________________________________________________________
List Info (unsubscribe here): http://uug.byu.edu/mailman/listinfo/uug-list

Reply via email to