Thread: string vs dynamic char array

  1. #16
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Ah, of course. Using resize() & size() are correct.

    Was I at least right about needing to manually add a '\0' to the vector?
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

  2. #17
    Registered User
    Join Date
    Dec 2007
    Posts
    932
    Cpjust: vec.push_back( '\0' ); // I can't remember if this is required, but it doesn't hurt.

    Apparrently its working without the '\0'.
    At least i dont have garbage at the end and nothing missing either.

    And by the way why would you need to allocate space in a vector?

    Arent vectors like strings?

    "Actually you could say that a string is a vector<char> A container of chars, or an advanced array of chars. "
    Last edited by Ducky; 11-03-2009 at 10:38 PM.
    Using Windows 10 with Code Blocks and MingW.

  3. #18
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Yes, but only if you use the vector's functions to add things to it. If you pass a pointer to the first element of the vector to a function, it won't be calling the vector's functions, it'll just write to it like it's a raw array.
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

  4. #19
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Apparrently its working without the '\0'.
    At least i dont have garbage at the end and nothing missing either.
    If things like the following work:
    Code:
    string s = &v[0];
    then this can only be because of pure (un)luck. If you are going to treat it as a C-style string, it has to be null-terminated. If you copy from the string to the vector, then string.end() or string.size() won't include the null-terminator (which doesn't have to be there).
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  5. #20
    Registered User
    Join Date
    Dec 2007
    Posts
    932
    Thank you, anon.
    You sure know how to recognize different types of trees from quite a long way away.

    I will '\0' terminate it then.
    Using Windows 10 with Code Blocks and MingW.

  6. #21
    Registered User
    Join Date
    Dec 2007
    Posts
    932
    And if i concatenate a vector element to a string like this, do i need to '\0' terminate it too?

    Code:
    int main()
    {
        string  str1 =  "Hello ";
        string  str2 =  "World";
        vector <char> vec( str2.begin(), str2.end());
    
        str1 += &vec[0];
    
        cout  << str1 << endl;
    
        return 0;
    }
    Using Windows 10 with Code Blocks and MingW.

  7. #22
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    Quote Originally Posted by Ducky View Post
    And if i concatenate a vector element to a string like this, do i need to '\0' terminate it too?

    Code:
    int main()
    {
        string  str1 =  "Hello ";
        string  str2 =  "World";
        vector <char> vec( str2.begin(), str2.end());
    
        str1 += &vec[0];
    
        cout  << str1 << endl;
    
        return 0;
    }
    Yes, because with the code you have, there is no guarantee that &vec[0] is null terminated.
    bit∙hub [bit-huhb] n. A source and destination for information.

  8. #23
    Registered User
    Join Date
    Dec 2007
    Posts
    932
    Thank you Bithub!

    Can it be done simpler than this?

    Code:
     
    str1 += &vec[0];
    
    str1 += '\0';
    Last edited by Ducky; 11-04-2009 at 12:26 PM.
    Using Windows 10 with Code Blocks and MingW.

  9. #24
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    Quote Originally Posted by Ducky View Post
    Thank you Bithub!

    Is it ok like this too?

    Code:
     str1 += '\0';
    You need to null terminate the vector before you add it to the string, not after. A push_back('\0') on the vector before adding it to the string should do the trick.
    bit∙hub [bit-huhb] n. A source and destination for information.

  10. #25
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    That is a contrived example, though. Normally you wouldn't need to add the '\0' to the vector if you do it right.

    Earlier there was a discussion about pushing back a null character in cpjust's example (post #6). Pushing it back there is not required and doesn't do anything unless you use the vector as a C-style string later on, but you would normally just use c_str() for that.

    When that code is changed to use resize, that actually adds 100 null characters anyway, so filling the vector with data from the old function would only cause a problem if all 100 characters were filled with string data. Then you might get garbage characters at the end. One solution is to resize to 100 (or whatever) and only pass 99 characters to your old function. Another is to assign the vector to the string.
    Code:
    std::string str = "Hello World";
    std::vector<char> vec1( str.begin(), str.end() );
    // You would want push_back('\0') here if using the vector as a char*
    // but that's not usually necessary, use c_str() on the string instead
    Code:
    std::vector<char> vec2( 100 );  // Initialize with 100 null characters
    old_c_function( &vec[0], vec.size() );
    
    // Now put it back into the string.
    str.assign(vec.begin(), vec.end());
    // No null needed, we used assign.
    Code:
    std::vector<char> vec2( 100 );  // Initialize with 100 null characters
    old_c_function( &vec[0], vec.size()-1 );
    
    // This is a bit more dangerous, I prefer assign.
    str = &vec[0];
    // No null needed, only filled size()-1 characters, and the last character was initialized to null.

  11. #26
    Registered User
    Join Date
    Dec 2007
    Posts
    932
    Thanks Daved!

    You are revealing details about vectors that doesnt really explained anywhere. Interesting.
    Using Windows 10 with Code Blocks and MingW.

  12. #27
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    std::string does not require null termination. The question of how to properly "null terminate" the vector is moot.

    Convert vector of char to string:

    Code:
    std::string foo( vec.begin(), vec.end() );
    Convert string to vector of char:

    Code:
    std::vector< char > foo( str.begin(), str.end() );
    And yes, when you call c_str() on the resulting string, it will be null terminated automatically. std::string is nothing like a C-string.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  13. #28
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    If you just wanted a C string for reading, you'd use string::c_str(). The reason for using a vector is to pass it to a C function for writing into a C string, and for that you'd need to add a '\0' char to the end (assuming it's going to append instead of just overwriting the string).
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Unable to compare string with 'getter' returned string.
    By Swerve in forum C++ Programming
    Replies: 2
    Last Post: 10-30-2009, 05:56 PM
  2. Replies: 8
    Last Post: 04-25-2008, 02:45 PM
  3. Personal Program that is making me go wtf?
    By Submeg in forum C Programming
    Replies: 20
    Last Post: 06-27-2006, 12:13 AM
  4. Character arrays
    By PsychoBrat in forum C++ Programming
    Replies: 7
    Last Post: 06-21-2002, 12:02 PM
  5. How do you search & sort an array?
    By sketchit in forum C Programming
    Replies: 30
    Last Post: 11-03-2001, 05:26 PM