code_junkie

Convert std::string to const char* or char*

Nov 12th, 2011
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.75 KB | None | 0 0
  1. std::string str;
  2. const char * c = str.c_str();
  3.  
  4. std::string str;
  5. char * writable = new char[str.size() + 1];
  6. std::copy(str.begin(), str.end(), writable);
  7. writable[str.size()] = ''; // don't forget the terminating 0
  8.  
  9. // don't forget to free the string after finished using it
  10. delete[] writable;
  11.  
  12. std::string str;
  13. boost::scoped_array<char> writable(new char[str.size() + 1]);
  14. std::copy(str.begin(), str.end(), writable.get());
  15. writable[str.size()] = ''; // don't forget the terminating 0
  16.  
  17. // get the char* using writable.get()
  18.  
  19. // memory is automatically freed if the smart pointer goes
  20. // out of scope
  21.  
  22. std::string str;
  23. std::vector<char> writable(str.size() + 1);
  24. std::copy(str.begin(), str.end(), writable.begin());
  25.  
  26. // get the char* using &writable[0] or &*writable.begin()
  27.  
  28. std::string x = "hello";
  29.  
  30. // USING STACK WHERE MAXIMUM SIZE OF x IS KNOWN TO BE COMPILE-TIME CONSTANT "N"
  31. char y[N + 1];
  32. strcpy(y, x.c_str());
  33.  
  34. // USING THE STACK TO HANDLE x OF UNKNOWN LENGTH
  35. char* y = alloca(x.size() + 1);
  36. strcpy(y, x.c_str());
  37.  
  38. // USING THE STACK TO HANDLE x OF UNKNOWN LENGTH (NON-STANDARD GCC EXTENSION)
  39. char y[x.size() + 1];
  40. strcpy(y, x.c_str());
  41.  
  42. // USING new/delete HEAP MEMORY, MANUAL DEALLOC, NO INHERENT EXCEPTION SAFETY
  43. char* y = new char[x.size() + 1];
  44. strcpy(y, x.c_str());
  45. // use y...
  46. delete[] y; // make sure no break, return, throw or branching bypasses this
  47.  
  48. // USING new/delete HEAP MEMORY, SMART POINTER DEALLOCATION, EXCEPTION SAFE
  49. see boost shared_array usage in Johannes Schaub's answer
  50.  
  51. // USING malloc/free HEAP MEMORY, MANUAL DEALLOC, NO INHERENT EXCEPTION SAFETY
  52. char* y = strdup(x.c_str());
  53. // use y...
  54. free(y);
  55.  
  56. ...unfinished...
  57.  
  58. class DeepString
  59. {
  60. DeepString(const DeepString& other);
  61. DeepString& operator=(const DeepString& other);
  62. char* internal_;
  63.  
  64. public:
  65. explicit DeepString( const string& toCopy):
  66. internal_(new char[toCopy.size()+1])
  67. {
  68. strcpy(internal_,toCopy.c_str());
  69. }
  70. ~DeepString() { delete[] internal_; }
  71. char* str() const { return internal_; }
  72. const char* c_str() const { return internal_; }
  73. };
  74.  
  75. void aFunctionAPI(char* input);
  76.  
  77. // other stuff
  78.  
  79. aFunctionAPI("Foo"); //this call is not safe. if the function modified the
  80. //literal string the program will crash
  81. std::string myFoo("Foo");
  82. aFunctionAPI(myFoo.c_str()); //this is not compiling
  83. aFunctionAPI(const_cast<char*>(myFoo.c_str())); //this is not safe std::string
  84. //implement reference counting and
  85. //it may change the value of other
  86. //strings as well.
  87. DeepString myDeepFoo(myFoo);
  88. aFunctionAPI(myFoo.str()); //this is fine
Add Comment
Please, Sign In to add comment