string vs dynamic char array

This is a discussion on string vs dynamic char array within the C++ Programming forums, part of the General Programming Boards category; We can store data dynamically in a string just like in a dynamic char array, right? So "string str;" is ...

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

    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 10:54 AM.
    Compiler MSVC++ 2013 with Code::Blocks.

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

    Yes.

    Certain functions still require char arrays.
    Mac and Windows cross platform programmer. Ruby lover.

    Quote of the Day
    12/20: Mario F.:I never was, am not, and never will be, one to shut up in the face of something I think is fundamentally wrong.

    Amen brother!

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    >> 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 09:58 AM.

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

    Thanks a lot everyone!
    Compiler MSVC++ 2013 with Code::Blocks.

  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
    790
    Thank you Cpjust, i think that's just what i was looking for!

    Using a vector element as a string
    Compiler MSVC++ 2013 with Code::Blocks.

  8. #8
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    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,246
    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,302
    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
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,239
    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
    21,660
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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,246
    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
    21,660
    That does not imply that capacity() is correct. Rather, that implies that reserve() is incorrect, and resize() should be used instead.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  15. #15
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    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.

Page 1 of 2 12 LastLast
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, 04:26 PM

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