unsigned int usage

This is a discussion on unsigned int usage within the C++ Programming forums, part of the General Programming Boards category; In Stroustrup book we read: Originally Posted by Bjarne Stroustrup Using an unsigned instead of an int to gain one ...

  1. #1
    System Novice siavoshkc's Avatar
    Join Date
    Jan 2006
    Location
    Tehran
    Posts
    1,231

    unsigned int usage

    In Stroustrup book we read:
    Quote Originally Posted by Bjarne Stroustrup
    Using an unsigned instead of an int to gain one more bit to represent positive integers is
    almost never a good idea. Attempts to ensure that some values are positive by declaring
    variables unsigned will typically be defeated by the implicit conversion rules (C.6.1, C.6.2.1).
    But here we see
    Quote Originally Posted by Bjarne Stroustrup
    Arrays are not self-describing because the number of elements of an array is not guaranteed to
    be stored with the array. This implies that to traverse an array that does not contain a
    terminator the way character strings do, we must somehow supply the number of elements.
    For example:
    Code:
    void fp (char v[], unsigned int size)
    {
        for (int i=0; i<size; i++) use(v[i] );
        const int N =7;
        char v2[N];
        for (int i=0; i<N; i++) use(v2[i] );
    }
    Why he used unsigned int for "size" parameter? BTW what is the type of indexer? It should be as large as a pointer I think as it can access whole memory.
    If &vec[0] is the lowest address accessible and &vec[n] is the highest address accessible which is &vec[0] + (n*sizeof(typeof(vec[0]))) then if type is char, n should be able to hold the highest address - lowest address much like a pointer.
    Last edited by siavoshkc; 07-22-2010 at 04:23 PM.
    Learn C++ (C++ Books, C Books, FAQ, Forum Search)
    Code painter latest version on sourceforge DOWNLOAD NOW!
    Download FSB Data Integrity Tester.
    Siavosh K C

  2. #2
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,673
    There was a flame war about it on CLC++ not too long ago - here's a choice posting (the entire thread is bit bloated):
    http://groups.google.com/group/comp....9587534a34b221

    gg

  3. #3
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    1,625
    unsigned int is used instead of an int because:
    It can contain a bigger number ( Max_signed_int * 2 )
    It hints the user that a non-negative value is expected

    None is better than the other! Each has its own area of expertise. The programmer just chooses the best for his/her current project.

    For instance:
    Code:
    int mult(int a, int b) { return a*b; } // For common numbers
    
    unsigned int mult(unsigned int a, unsigned int b) { return a*b; } // For big physical numbers
    Devoted my life to programming...

  4. #4
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    That said, the example code by Stroustrup here is bad because it mixes signed and unsigned ints. You can use unsigneds or leave them be, whatever your preference is, but be very careful when mixing signeds and unsigneds, or very subtle bugs can occur. Also, the compiler warnings are annoying.
    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

  5. #5
    System Novice siavoshkc's Avatar
    Join Date
    Jan 2006
    Location
    Tehran
    Posts
    1,231
    Sipher, did you read the Bjarne quotes and Codeplug link?
    Learn C++ (C++ Books, C Books, FAQ, Forum Search)
    Code painter latest version on sourceforge DOWNLOAD NOW!
    Download FSB Data Integrity Tester.
    Siavosh K C

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Producer/Consumer - problems creating threads
    By hansel13 in forum C Programming
    Replies: 47
    Last Post: 08-20-2010, 02:02 PM
  2. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  3. Need Help Please
    By YouShallNotPass in forum C++ Programming
    Replies: 3
    Last Post: 08-22-2006, 11:22 AM
  4. Converted from Dev-C++ 4 to Dev-C++ 5
    By Wraithan in forum C++ Programming
    Replies: 8
    Last Post: 12-03-2005, 06:45 AM
  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