string vs char[50]

This is a discussion on string vs char[50] within the C++ Programming forums, part of the General Programming Boards category; While curising the C string tutorial on this site, I found a set of variables resembling my struct. Here is ...

  1. #1
    SRS
    SRS is offline
    3rd Week In C++ SRS's Avatar
    Join Date
    Nov 2007
    Posts
    22

    string vs char[50]

    While curising the C string tutorial on this site, I found a set of variables resembling my struct. Here is what I found:
    Code:
      char name[50];
      char lastname[50];
      char fullname[100];
    ...and here is my struct:
    Code:
    struct contacts
    {
        string firstName;
        string lastName;
        string companyName;
        string homeAddress;
        int id_number;
        int age;
    };
    ...and here are my questions:
    1: Should I use string or the char arrays for what I am doing (storing names)
    2: Why or why not? -memory use advantages, etc anything that may apply

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,799
    Yes, they are very adaptable, they can shrink or grow to accommodate whatever size data they need to so they only use enough memory to do so. Management of this memory is automatic so you don't really have to think about it too much. There may be a bit of a learning curve getting used to them and understanding the more useful of the member functions available.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  3. #3
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,181
    from what i have read, the general rule of thumb is to use C++ strings (std::string) instead of C strings (char arrays) unless you know what you are doing, since C++ strings are better encapsulated, and is easier and safer to manipulate. The only drawback I would imagine would be that it's a bit less efficient compared to C strings, but most of the time, efficiency doesn't practically matter in string manipulation codes.

  4. #4
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Always use std::string instead of char arrays.
    Always use std::vector<T> instead of arrays of T.

    Arrays can't grow or shrink (unless you use new & delete [], but that can lead to bugs if you don't do it carefully).

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    There is one exception: If you want to store the data structure as a binary format in a file [1] you are better off with fixed size arrays than any C++ object type, because you can then store the entire struct with one write call, rather than as a bunch of small writes of individual strings and other "pieces".

    [1] Yes, I'm fully aware that there are all kinds of problems with portability if a struct is stored in a binary file.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  6. #6
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by matsp View Post
    There is one exception: If you want to store the data structure as a binary format in a file [1] you are better off with fixed size arrays than any C++ object type, because you can then store the entire struct with one write call, rather than as a bunch of small writes of individual strings and other "pieces".
    I don't agree. You can write a whole file to a vector in one pass too.

  7. #7
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by cpjust View Post
    I don't agree. You can write a whole file to a vector in one pass too.
    You mean write a vector to a file in one pass? Perhaps, but does that work also if you have a struct of strings inside the vector? Without help from member functions or similar?

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  8. #8
    int x = *((int *) NULL); Cactus_Hugger's Avatar
    Join Date
    Jul 2003
    Location
    Banks of the River Styx
    Posts
    902
    You mean write a vector to a file in one pass? Perhaps, but does that work also if you have a struct of strings inside the vector? Without help from member functions or similar?
    No, it wouldn't work with string members. (Or if it did, it's certainly not portable.)
    Regardless, even with char arrays - if they represent strings, then you have to make sure that what's past the terminating null is zeroed out, or you'll be writing old string values, old RAM values, possibly sensitive and certainly not needed information to disk.
    long time; /* know C? */
    Unprecedented performance: Nothing ever ran this slow before.
    Any sufficiently advanced bug is indistinguishable from a feature.
    Real Programmers confuse Halloween and Christmas, because dec 25 == oct 31.
    The best way to accelerate an IBM is at 9.8 m/s/s.
    recursion (re - cur' - zhun) n. 1. (see recursion)

  9. #9
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by matsp View Post
    You mean write a vector to a file in one pass? Perhaps, but does that work also if you have a struct of strings inside the vector? Without help from member functions or similar?
    No, I mean using vector<unsigned char> to store binary file data, or vector<char> to store a text file.

  10. #10
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Cactus_Hugger View Post
    No, it wouldn't work with string members. (Or if it did, it's certainly not portable.)
    Regardless, even with char arrays - if they represent strings, then you have to make sure that what's past the terminating null is zeroed out, or you'll be writing old string values, old RAM values, possibly sensitive and certainly not needed information to disk.
    True - If I write code that stores structs to files, I generally use memset() to fill with zero for this exact reason [and it's also "handy" to have all values zero anyways!]

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ ini file reader problems
    By guitarist809 in forum C++ Programming
    Replies: 7
    Last Post: 09-04-2008, 06:02 AM
  2. Replies: 8
    Last Post: 04-25-2008, 02:45 PM
  3. Message class ** Need help befor 12am tonight**
    By TransformedBG in forum C++ Programming
    Replies: 1
    Last Post: 11-29-2006, 10:03 PM
  4. Classes inheretance problem...
    By NANO in forum C++ Programming
    Replies: 12
    Last Post: 12-09-2002, 02:23 PM
  5. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 06:49 PM

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