Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Converting a size_t into an integer (c++)

I've been trying to make a for loop that will iterate based off of the length of a network packet. In the API there exists a variable (size_t) by event.packet->dataLength. I want to iterate from 0 to event.packet->dataLength - 7 increasing i by 10 each time it iterates but I am having a world of trouble.

I looked for solutions but have been unable to find anything useful. I tried converting the size_t to an unsigned int and doing the arithmetic with that but unfortunately it didn't work. Basically all I want is this:

for (int i = 0; i < event.packet->dataLength - 7; i+=10) { }

Though every time I do something like this or attempt at my conversions the i < # part is a huge number. They gave a printf statement in a tutorial for the API which used "%u" to print the actual number however when I convert it to an unsigned int it is still incorrect. I'm not sure where to go from here. Any help would be greatly appreciated :)

like image 826
JeanOTF Avatar asked Jan 13 '11 22:01

JeanOTF


People also ask

Are size_t and int the same?

If we consider the standard, both are integers of size 16 bits. On a typical 64-bit system, the size_t will be 64-bit, but unsigned int will be 32 bit. So we cannot use them interchangeably. One standard recommendation is that the size_t be at most as big as an unsigned long.

Can you assign int to size_t?

If size_t is narrower than int (that is, int can represent all values of size_t ), then foo is promoted to int in the expression foo + 1 . The only way this could overflow is if INT_MAX == SIZE_MAX . Theoretically that is possible, e.g. 16-bit int and 15-bit size_t .

Can size_t be a double?

Converting to double will not cause an overflow, but it could result in a loss of precision for a very large size_t value. Again, it doesn't make a lot of sense to convert a size_t to a double ; you're still better off keeping the value in a size_t variable.

What is size_t type in C?

size_t type is a base unsigned integer type of C/C++ language. It is the type of the result returned by sizeof operator. The type's size is chosen so that it can store the maximum size of a theoretically possible array of any type. On a 32-bit system size_t will take 32 bits, on a 64-bit one 64 bits.


2 Answers

Why don't you change the type of i?

for (size_t i = 0; i < event.packet->dataLength - 7; i+=10) { }

Try to keep the types of all variables used together the same type; casts should be avoided.

There is no format specifier for size_t in C++03, you have to cast to the largest unsigned integer type you can and print that. (The format specifier for size_t in C++0x is %zu). However, you shouldn't be using printf anyway:

std::cout << i; // print i, even if it's a size_t

While streams may be more verbose, they're more type safe and don't require you to memorize anything.

Keep in mind your actual loop logic may be flawed. (What happens, as genpfault notes, when dataLength - 7 is negative?)

like image 58
GManNickG Avatar answered Oct 04 '22 00:10

GManNickG


Do everything with signed arithmetic. Try:

for (int i = 0; i < int(event.packet->dataLength) - 7; i+=10) { }

Once you start using unsigned arithmetic with values that may be negative, and using comparison operators like <, you're in trouble. Much easier to keep things signed.

like image 20
David Thornley Avatar answered Oct 03 '22 23:10

David Thornley