Thread: 'unsinged' integer usage

  1. #1
    Registered User
    Join Date
    Apr 2003
    Posts
    9

    Question 'unsinged' integer usage

    I am currently learning C++ at ITT, and I have been using their class book as well as other books to learn more about the langauge. In doing so, I have come across some inconsistencies in what the authors think is important.
    My class book by Walter Savitch makes no real references to 'unsigned' variables. Another book by Jesse Liberty uses 'unsigned' variables in just about all of his examples. It doesn't save any memory by declaring an integer 'unsigned', so why bother?

    Thanks.

  2. #2
    jasondoucette.com JasonD's Avatar
    Join Date
    Mar 2003
    Posts
    278
    Generally people use signed integers unless they are sure that they do not need negative numbers. The memory usage is the same. And which you choose to use is totally up to you. You just have to make sure the values your variables intend to hold are within range of the type you choose them to be. It's that simple, really.

  3. #3
    Registered User axon's Avatar
    Join Date
    Feb 2003
    Posts
    2,572
    Firstly, if you're using "Absolute C++" by Savitch then I am sure he mentions it when talking about vectors preview. I don't have the book on me right now so I can't reference a page number. I believe that his explanation was that some function like size() for example return an unsigned int, not just type int. This value should be normaly converted to int when it needs to, but some compilers might not do it, so to be sure you could use unsigne int for safety, for example in a for loop:
    Code:
    for ( unsigned int i = 0; i < someVector.size(); i++)
            cout << someVector[i];
    I'll give you the page number later,

    hope this helps,

    axon

    some entropy with that sink? entropysink.com

    there are two cardinal sins from which all others spring: Impatience and Laziness. - franz kafka

  4. #4
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    "It doesn't save any memory by declaring an integer 'unsigned', so why bother?"

    C++ is a strongly typed language, which means if you don't assign the proper type to a variable or send the proper type to a function, you get an error. If you declare a variable as unsigned, that means you require a positive number to be assigned to that variable, and if somehow that variable gets assigned a negative number, then you want it to cause an error immediately, rather than propagating the error throughout the rest of your code.

    Based on Jesse Liberty's book Learn C++ in 21 days, he is incompetent, so be very suspicious of anything he says.
    Last edited by 7stud; 04-26-2003 at 05:38 PM.

  5. #5
    Registered User
    Join Date
    Apr 2003
    Posts
    9

    Thumbs up

    OK, starting to make sense.
    For general learning purposes, and for learning "correct" coding habits, is going through the hassle of declaring:

    unsigned short int myAge = 5

    a long way to go to put a simple 5 in a basic variable? I suppose an age can't be a negative number, but it seems like overkill.

    Ironically, yes the Jesse Liberty book in question is the C++ in 21 Days. I wasn't sure if this guy was a quack or not, because he does SOO many other programming books.

    The Walter Savitch book in question is Problem Solving with C++. It's one of the only useful books they've used so far at ITT. It's been pretty useful so far, but I was looking for more examples to mess with.

    Thanks.

  6. #6
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    "I suppose an age can't be a negative number, but it seems like overkill."

    There's nothing forcing you to make myAge so strongly typed. You can declare it like this:

    int myAge;

  7. #7
    Registered User
    Join Date
    Jan 2003
    Posts
    311
    unsigned int's were popular in C, particularly where 16 bit int's were common. In modern C and particularly C++ the main reason to choose unsigned over signed is to have well-defined behavior for >> and for overflow. When using stl containers and your own containers the proper and correct way of handleing the return value of size() is to use vector<>::size_type, and to declare your own simmilar typedef's.
    Code:
    template<class T>
    void printvec(const std::vector<T> &v, std::ostream &os=std::cout) {
        os << '{';
        if(v.size() > 0) {
            typedef typename std::vector<T>::size_type st; 
            const st last = v.size()-1;
            for(st i=0;i<last;++i) {
                os << v[i] << ", ";
            }
            os << v[last];
        }
        os << '}';
    }
    Now if size begins returning unsigned long long, you are set. Anytime somthing returns size_t or anything else defined with a typedef, use that type. For your own objects, if there is any reason to change something provide a typedef. For age, use an int.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. memory issue
    By t014y in forum C Programming
    Replies: 2
    Last Post: 02-21-2009, 12:37 AM
  2. Link List math
    By t014y in forum C Programming
    Replies: 17
    Last Post: 02-20-2009, 06:55 PM
  3. Looking for constructive criticism
    By wd_kendrick in forum C Programming
    Replies: 16
    Last Post: 05-28-2008, 09:42 AM
  4. No Match For Operator+ ???????
    By Paul22000 in forum C++ Programming
    Replies: 24
    Last Post: 05-14-2008, 10:53 AM
  5. load gif into program
    By willc0de4food in forum Windows Programming
    Replies: 14
    Last Post: 01-11-2006, 10:43 AM