Is it a good habit to use "string::size_type" as an index, rather than int

This is a discussion on Is it a good habit to use "string::size_type" as an index, rather than int within the C++ Programming forums, part of the General Programming Boards category; Is it a good habit to use "string::size_type" as an index, rather than int? Fro, eg Code: string::size_type index; //not ...

  1. #1
    Registered User
    Join Date
    Apr 2007
    Posts
    284

    Is it a good habit to use "string::size_type" as an index, rather than int

    Is it a good habit to use "string::size_type" as an index, rather than int?
    Fro, eg
    Code:
    string::size_type index; //not "int index"
    cout<<str[index];

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,319
    Yes, if you're using it as an index into a string.

  3. #3
    Registered User
    Join Date
    Apr 2007
    Posts
    284
    What's the catch to use int?

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,319
    It will be very rare that it actually matters in practice. The potential problem is that int might not hold all the possible values that can be used as indexes in the string. The size_type typedef is meant to be used because it guarantees that it won't be too big or too small for the indexes in the string.

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by meili100 View Post
    What's the catch to use int?
    The biggest concern is if your string is _REALLY_ big, the signedness of int halfes the size that the string could be. [not to mention that 64-bit machines may have 32-bit int, and 64-bit sizetype - allowing strings to be longer than what can be represented in a 32-bit integer]

    The problem with comparing a signed integer with an unsigned quantity is that you need to make sure you do it right way around - the compiler will most likely warn about unsigned and signed being compared. A signed value has half the positive range (because one bit is the sign) compared to an unsigned value, but also, if you convert a LARGE unsigned to a signed value, it will become negative, which if it's truly an unsigned quantity (such as the length of a string), it is obviously not smaller than the largest positive value that a signed integer can hold, but a negative value produced by such a conversion is smaller! This could potentially lead to all sorts of confusing and strange results, not to mention strange bugs.

    Now, most people have strings that are a few dozen to a few kilobytes in size - I was debugging a problem with a collegues (commercial) software at work the other day. It was caused by a string being read from a file, where the string was bigger than what the software could cope with - a bit more than 1 kb strings were allowed, but the file being read had MUCH larger strings. This was not OUR software, but it was reading one of OUR files.

    But the signed/unsigned problem is unlikely to happen until strings are at a very minimum 32K long, and on modern OS's and machines, more likely to happen at 2G in length - that's a MASSIVE string.

    --
    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
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    Basically, it's good to avoid compiler warnings.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 03-10-2008, 11:57 AM
  2. Debug Error Really Quick Question
    By GCNDoug in forum C Programming
    Replies: 1
    Last Post: 04-23-2007, 12:05 PM
  3. Function to check memory left from malloc and free?
    By Lechx in forum C Programming
    Replies: 4
    Last Post: 04-24-2006, 05:45 AM
  4. Need help understanding info in a header file
    By hicpics in forum C Programming
    Replies: 8
    Last Post: 12-02-2005, 11:36 AM
  5. Switch/case Problems (long code in post)
    By Wraithan in forum C++ Programming
    Replies: 2
    Last Post: 12-01-2005, 05:40 PM

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