[gameprogrammer] Re: Is this valid?
- From: Matthew Weigel <unique@xxxxxxxxxxx>
- To: gameprogrammer@xxxxxxxxxxxxx
- Date: Fri, 25 Jan 2008 00:26:43 -0600
Kevin Jenkins wrote:
std::string s;
s.reserve(strlen("Hello world"));
strcpy(s.c_str(), "Hello world");
That makes me cry in so many ways.
1. The signature for string.c_str() is "const charT* c_str() const" so
assigning it a value is a bad thing - and not valid.
2. Please, never use strcpy() - even if it's with a constant string. Use
strncpy(), and understand its semantics.
3. Why are you mixing C and C++ string manipulation? You are trying to use
your std::string as a char*, why not just use a char*?
4. Alternatively, why not use
std::string s("Hello world");
to assign the string?
--
Matthew Weigel
hacker
unique & idempot.ent
---------------------
To unsubscribe go to http://gameprogrammer.com/mailinglist.html
- Follow-Ups:
- [gameprogrammer] Re: Is this valid?
- From: Olivier Delannoy
- References:
- [gameprogrammer] Is this valid?
- From: Kevin Jenkins
Other related posts:
std::string s;
s.reserve(strlen("Hello world"));
strcpy(s.c_str(), "Hello world");
- [gameprogrammer] Re: Is this valid?
- From: Olivier Delannoy
- [gameprogrammer] Is this valid?
- From: Kevin Jenkins