Construct a std::string from char*

This is a discussion on Construct a std::string from char* within the C++ Programming forums, part of the General Programming Boards category; still modify the first character of the string?? Why should it? buf won't change automatically when _M_p changes. If you ...

  1. #16
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,145
    still modify the first character of the string??
    Why should it?

    buf won't change automatically when _M_p changes.

    If you have
    Code:
    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
    Location
    Henderson, NV
    Posts
    875
    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:
    Code:
    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
    Posts
    8,892
    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!
    CornedBee

    "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

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 13
    Last Post: 12-14-2007, 02: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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21