jeudi 26 février 2015

C++ enforcing a standard: size_t


Simple question, Would it be good for me to force myself to start using size_t (or unsigned longs?) in places where I would normally use ints when dealing with arrays or other large datastructures?


Say you have a vector pointer:



auto myVectorPtr = myVector;


Unknown to you, the size of this vector is larger than:



std::numeric_limits<int>::max();


and you have a loop:



for(int i = 0; i < myVectorPtr->size(); ++i)


wouldn't it be preferable to use



for(size_t i = 0; i < myVectorPtr->size(); ++i)


to avoid running into overflows?


I guess my question really is, are there any side effects of using size_t (or unsigned longs?) in arithmetic and other common operations. Is there anything I need to watch out for if I started using size_t (or unsigned longs?) instead of the classic int.




Aucun commentaire:

Enregistrer un commentaire