Thread: string::size_type vs. int

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

    string::size_type vs. int

    The C++ std::string class provides string::size_type as an integer datatype large enough to represent any possible string size.

    The maximum value of an int is 2,147,483,647.

    Meaning that if a line or page wont hold more than 2,147,483,647 characters
    - which in normal cases never do - then you dont have to bother declaring string::size_type, you can just declare an int, which is much more simple.

    Am i right or am i right?
    Using Windows 10 with Code Blocks and MingW.

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    In theory, you don't have any knowledge about string::size_type, it could be a 16-bit integer, a 32-bit integer or a 64-bit integer [or a floating point variable for all you know].

    The value is most likely compatible with an int, but the CORRECT thing to do is to always use the string::size_type. That is guaranteed to ALWAYS work. [Just be aware that if you change the STL implementation that you use for a particular software setup, you WILL need to recompile ALL of your code, or bad things may happen - but that's true even if you don't use string::size_type].

    --
    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.

  3. #3
    Registered User
    Join Date
    Dec 2007
    Posts
    930
    Hmm i see. Thanks Matsp!
    Using Windows 10 with Code Blocks and MingW.

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    By the way, it's most likely an UNSIGNED integer, since it's very unlikely that the size of a string is negative.

    Mixing unsigned and signed variables will sometimes cause a problem [usually when you are outside the range of either type, e.g. large unsigned numbers become negative, or negative numbers become large positive when translated from one to the other].

    --
    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.

  5. #5
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by matsp View Post
    In theory, you don't have any knowledge about string::size_type, it could be a 16-bit integer, a 32-bit integer or a 64-bit integer [or a floating point variable for all you know].
    It certainly won't be floating point. A size_type is guaranteed to be an unsigned integral type.

  6. #6
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by Ducky View Post
    The C++ std::string class provides string::size_type as an integer datatype large enough to represent any possible string size.

    The maximum value of an int is 2,147,483,647.

    Meaning that if a line or page wont hold more than 2,147,483,647 characters
    - which in normal cases never do - then you dont have to bother declaring string::size_type, you can just declare an int, which is much more simple.

    Am i right or am i right?
    Since you certainly won't have a negative number of characters, a signed number is kind of pointless since you're wasting half the range.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  2. Replies: 2
    Last Post: 03-24-2006, 08:36 PM
  3. getting a headache
    By sreetvert83 in forum C++ Programming
    Replies: 41
    Last Post: 09-30-2005, 05:20 AM
  4. Quack! It doesn't work! >.<
    By *Michelle* in forum C++ Programming
    Replies: 8
    Last Post: 03-02-2003, 12:26 AM
  5. easy if you know how to use functions...
    By Unregistered in forum C Programming
    Replies: 7
    Last Post: 01-31-2002, 07:34 AM