Thread: string vs dynamic char array

  1. #1
    Registered User
    Join Date
    Dec 2007
    Posts
    930

    string vs dynamic char array

    We can store data dynamically in a string just like in a dynamic char array, right?

    So "string str;" is almost the same as "char * ch = new char[64];"
    only much better because we dont have to define the max size?

    If the answer is yes i wonder why people continue to complicate their lives by using
    dynamic char arrays when they can use strings as well?
    Last edited by Ducky; 11-02-2009 at 11:54 AM.
    Using Windows 10 with Code Blocks and MingW.

  2. #2
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    Yes.

    Yes.

    Certain functions still require char arrays.
    Mainframe assembler programmer by trade. C coder when I can.

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> Certain functions still require char arrays.
    Even then the string is usually better with a simple conversion done before working with the other functions.

    It might make sense to use a non-dynamic char array instead of the string class in some cases, although those cases are somewhat rare.

  4. #4
    i've lost my mind
    Join Date
    Jun 2008
    Posts
    26
    Use strings, and if you need an old school string for any reason you can do this:

    string lolwat = "hai therreee";
    // ... lolwat.c_str();

    Of course, don't forget to distinguish between C and C++ when reading random source online, which is probably where you confusion comes from. Most people carelessly mix the two languages, and in C there is no string class because classes don't exist in C. But there still are libs and ways to write functions to manage a 'dynamic' arrays in C.
    Last edited by gltiich; 11-02-2009 at 10:58 AM.

  5. #5
    Registered User
    Join Date
    Dec 2007
    Posts
    930
    Thats awesome and crystal clear, strings all the way from now on!

    Thanks a lot everyone!
    Using Windows 10 with Code Blocks and MingW.

  6. #6
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    If you need to pass a char* to a function instead of a const char*, you can use a vector<char> like this:
    Code:
    std::string str = "Hello World";
    std::vector<char> vec( str.begin(), str.end() );
    vec.push_back( '\0' );  // I can't remember if this is required, but it doesn't hurt.
    vec.reserve( 100 );  // Make extra room for the string to be written into.
    
    old_c_function( &vec[0], vec.capacity() );
    
    // Now put it back into the string.
    str = &vec[0];
    "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

  7. #7
    Registered User
    Join Date
    Dec 2007
    Posts
    930
    Thank you Cpjust, i think that's just what i was looking for!

    Using a vector element as a string
    Using Windows 10 with Code Blocks and MingW.

  8. #8
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Note that this part is not quite correct:
    Code:
    old_c_function( &vec[0], vec.capacity() );
    It should be:
    Code:
    old_c_function( &vec[0], vec.size() );
    The internal data in the vector will not be accurate if you use capacity, because capacity only tells you how much space was allocated, but the size tells you how many actual chars are available to be modified.

  9. #9
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    Quote Originally Posted by Daved View Post
    Note that this part is not quite correct:
    Code:
    old_c_function( &vec[0], vec.capacity() );
    It should be:
    Code:
    old_c_function( &vec[0], vec.size() );
    The internal data in the vector will not be accurate if you use capacity, because capacity only tells you how much space was allocated, but the size tells you how many actual chars are available to be modified.
    No, capacity is correct.
    bit∙hub [bit-huhb] n. A source and destination for information.

  10. #10
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by bithub View Post
    No, capacity is correct.
    Correct in the sense that it might pass the integer value you were expecting, but incorrect in the sense that any characters written to beyond the value of size() are surely undefined behaviour.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  11. #11
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by bithub View Post
    No, capacity is correct.
    I don't think so, because although the characters from vec[0] to vec[capacity-1] may be writable, the vector itself doesn't consider them to be part of the current value. They are just available space.

    The vector tracks the size of its contents, so it isn't really correct to go behind vector's back and write into the space beyond end(), even if that space is reserved. It will have no way of knowing that you did that.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  12. #12
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by bithub
    No, capacity is correct.
    In practice for vectors of objects of built-in types (or maybe POD types in general), it probably works okay. However, semantically and more likely as an issue for non-POD objects, it is not correct.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  13. #13
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    My comment was just for the context of the posted code. In that case, capacity() is correct and size() is incorrect (since size() would return 0, and thus fail for the given example). I realize that after the function call, the vector is in an undefined state (and that size() will probably still return zero after the function call), but in the posted code, this was irrelevant.
    bit∙hub [bit-huhb] n. A source and destination for information.

  14. #14
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    That does not imply that capacity() is correct. Rather, that implies that reserve() is incorrect, and resize() should be used instead.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  15. #15
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    I misread the original code and thought it used resize instead of reserve.

    Using reserve and capacity is wrong in the sense that the vector's internal variables will be out of sync with the data.

    You should use resize and size instead as laserlight said. Sorry for the confusion.

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