Skip to main content

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

7
  • 2
    Instead of: char * writable = new char[str.size() + 1]; You can use char writable[str.size() + 1]; Then you don't need to worry about deleting writable or exception handling. Commented Jun 21, 2010 at 9:34
  • 13
    You can't use str.size() unless the size is known at compile time, also it might overflow your stack if the fixed size value is huge. Commented Oct 5, 2012 at 15:32
  • 1
    char* result = strcpy((char*)malloc(str.length()+1), str.c_str()); Commented Jul 12, 2014 at 12:10
  • 10
    @cegprakash strcpy and malloc aren't really the C++ way. Commented Sep 25, 2014 at 9:29
  • 8
    No, but char* dest = new char[str.length() + 1]; std::copy(str.begin(), str.end(), dest) would be more idiomatic C++. strcpy() and malloc() aren't wrong or problematic, but it seems inconsistent to use a C++ string and C library facilities with C++ equivalents in the same block of code. Commented Sep 27, 2014 at 8:38