DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 4 of 4

Thread: filling a vector (size_t instead of int ?)

  1. #1
    Join Date
    Dec 2004

    vector (size_t instead of int ?)

    //: S15:AllMyRodents.cpp
    #include <iostream>
    #include <vector>
    #include <cstddef> // For size_t
    using namespace std;
    void doRodentThings(vector<Rodent*>& v) {
        for (size_t i = 0; i < v.size(); ++i)
     // Clean up:
        for (int i = 0; i < v.size(); ++i)
            delete v[i];
    In the first for loop, Eckel uses size_t, and in the second one he uses int.

    I've tried int instead of size_t, and it works ok.

    Last edited by pseudo; 03-30-2005 at 11:28 AM.

  2. #2
    Join Date
    Nov 2003
    Well, a trough and through program should use the exact type that size() returns, which is size_t. size_t is an unsigned integral type, so it has two advantages: it can never be negative and it can accomdate a larger number (twice than the signed equivalent). However, C/C++ implicitly convert signed to unsigned and vice versa so the int version works just fine, so long as you don't have more than 2^31 elements in the vector;)
    This isn't such an unlikely scenario btw. It's possible to think of a vector <char> that contains a huge disk file for example. But clealrly, in this example the number of elements is much smaller so either size_t or int should be fine.
    That said, I think it's better to stick to consistent coding standards. If you're into size_t, stick with it. Otherwise, human readers get confused.
    Danny Kalev

  3. #3
    Join Date
    Dec 2003
    I usually use unsigned ints. I tend to avoid senseless renaming of basic types -- the fewer basic types I have to keep up with, the easier code is for me to understand. To me, size_t exists to tell me what the parameter expected means (so when I type foo( the list shows up sensibly) more than to acutally use as a type.

    foo(int,int,int,int, double*) does not mean so much in the quick parameter listing...

  4. #4
    Join Date
    Nov 2003
    I beg to differe. size_t has two major advantages over plain unsigned int. First, it's self-explanatory. You know that it counts bytes or elements, rather than say a serial port address. Secondly, it's portable because 64-bit systems or 16-bit system use different sizes for unsigned int. size_t abstracts these platform dependent sizes. Admittedly, 16 bits systems aren't very popular these days but Solaris and other 64-bit systems define size_t as unsigned long, i.e., 64 bits.
    Danny Kalev

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
HTML5 Development Center
Latest Articles
Questions? Contact us.
Web Development
Latest Tips
Open Source

   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center