Thread: Construct a std::string from char*

  1. #16
    Registered User
    Join Date
    Dec 2006
    still modify the first character of the string??
    Why should it?

    buf won't change automatically when _M_p changes.

    If you have
    int a = 5;
    int b = a;
    a = 10;
    b doesn't become 10. Same thing there.

    And how would you update _M_length?

    And even if you can make it work, this is only for this particular version of GCC. Another implementation or even another version of GCC can do it totally differently.

    If you are so concerned about performance that you can't afford the copying, it would be much easier to roll your own string class. Much safer, too.

  2. #17
    Registered User jeffcobb's Avatar
    Join Date
    Dec 2009
    Henderson, NV
    Just to avoid the copy when passing a string to a function, generally we create everything as a std::string and then set up functions to take it by reference:
    void doSomethingWithString(std::string &str_ref)
    Not sure that addresses your other issues but this is how we avoid making a copy of a string when passing to a function...
    C/C++ Environment: GNU CC/Emacs
    Make system: CMake
    Debuggers: Valgrind/GDB

  3. #18
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    That doesn't help when calling into external C APIs though.

    I stand corrected about C++03's guarantees, although I believe that implementations should fix defects in C++03 even if their fix only made it into C++0x. That said, I'm not aware of a single implementation where std::string is *not* continuous. I'm not even aware of any where c_str() does not simply point to the string's primary data.
    All the buzzt!

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 13
    Last Post: 12-14-2007, 03:34 PM
  2. std::string: Has my compiler gone nuts??
    By Andruu75 in forum C++ Programming
    Replies: 9
    Last Post: 09-28-2007, 04:02 AM
  3. Debugging help
    By cuddlez.ini in forum C++ Programming
    Replies: 3
    Last Post: 10-24-2004, 07:08 PM
  4. DLL and std::string woes!
    By Magos in forum C++ Programming
    Replies: 7
    Last Post: 09-08-2004, 12:34 PM
  5. returning std::string
    By Unregistered in forum C++ Programming
    Replies: 3
    Last Post: 09-24-2001, 08:31 PM