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.
我一直在尝试建立一个for循环,它将基于网络数据包的长度进行迭代。在API中存在一个变量(size_t) by event.packet->dataLength。我想从0迭代到事件。包->dataLength - 7每次迭代时增加i 10,但是我有很多麻烦。
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:
我寻找解决方案,但一直找不到任何有用的东西。我尝试将size_t转换为无符号int并进行算术运算,但不幸的是它不起作用。基本上我想要的是:
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 :)
尽管每次我做这样的事情或尝试转换I < #部分都是一个很大的数字。他们在一个API的教程中给出了printf语句,它使用“%u”来打印实际的数字,但是当我将它转换为无符号整数时,它仍然是不正确的。我不知道从这里到哪里去。如有任何帮助,我们将不胜感激。
7 个解决方案
#1
4
Why don't you change the type of i
?
你为什么不改变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:
在c++ 03中没有size_t格式说明符,您必须将它转换为最大的无符号整数类型并打印出来。(在c++ 0x中,size_t的格式说明符是%zu)。然而,你不应该使用printf:
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?)
记住,实际的循环逻辑可能有缺陷。(正如genpfault所指出的,当dataLength - 7为负数时,会发生什么情况?)
#2
2
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.
一旦您开始使用带有可能为负的值的无符号算术,并使用比较运算符如<,您就有麻烦了。更容易保持东西签名。
#3
1
Is dataLength >= 7? If the result of dataLength-7 is negative, if you interpret it as unsigned, the result is a very large integer.
dataLength > = 7吗?如果dataLength-7的结果是负数,如果您将其解释为无符号,则结果是一个非常大的整数。
#4
0
Use size_t for i.
对我使用size_t。
For printf, if you don't have C99, only C90, cast to unsigned long, or unsigned long long. E.g.:
对于printf,如果没有C99,只有C90,则被转换为无符号long,或无符号long。例如:
for (size_t i = 0; i < 10; ++i)
//printf("%llu\n", (unsigned long long)i);
printf("%lu\n", (unsigned long)i);
Otherwise use %zu
否则使用%祖茂堂
#5
0
You should first check if event.packet->dataLength < 7
. Now if it's less then 7 you get values less than 0 used as unsigned: e.g. 0 = 0x00000000; -1 = 0 - 1 = 0xFFFFFFFF.
您应该首先检查事件是否发生。包- > dataLength < 7。现在如果它小于7就会得到小于0的值作为无符号:例如,0 = 0x00000000;-1 = 0 -1 = 0xffffff。
Again, the check:
再一次,检查:
if (event.packet->dataLength < 7) {
...
} else {
for (size_t i = 0; i < event.packet->dataLength - 7; i+=10) { }
}
#6
0
"every time I do something like this or attempt at my conversions the i < # part is a huge number."
“每次我做这样的事情或尝试转换I < #部分都是一个很大的数字。”
That indicates that original packet length is less than 7 (you're subtracting 7).
这表明原始数据包长度小于7(减去7)。
One fix is to use an in-practice-large-enough signed integer type, and the standard library provides ptrdiff_t
for that purpose. Like,
一种修复方法是使用实际中足够大的带符号整数类型,标准库为此提供ptrdiff_t。就像,
#include <stdlib.h> // Not sure, but I think it was this one.
typedef ptrdiff_t Size;
typedef Size Index;
void foo()
{
// ...
for( Index i = 0; i < Size( event.packet->dataLength ) - 7; i += 10 )
{
// ...
}
}
A more cumbersome workaround is to embed the whole thing in an if
that checks that the size is at least 7.
一个更麻烦的解决方案是,如果检查的大小至少为7,那么将整个东西嵌入到an中。
Cheers & hth.,
欢呼声& hth。
#7
0
Since event.packet->dataLength
returns an unsigned type size_t
:
因为事件。>dataLength返回一个无符号类型size_t:
1) Use size_t
as the index variable type.
1)使用size_t作为索引变量类型。
2) Insure math does not underflow. @beldaz. Rather than subtract 7 from event.packet->dataLength
, add 7 to i
.
2)确保数学不欠流。@beldaz。而不是从事件中减去7。数据包->dataLength,添加7给i。
// for (int i = 0; i < event.packet->dataLength - 7; i+=10) { }
for (size_t i = 0; i + 7 < event.packet->dataLength; i += 10) { }
#1
4
Why don't you change the type of i
?
你为什么不改变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:
在c++ 03中没有size_t格式说明符,您必须将它转换为最大的无符号整数类型并打印出来。(在c++ 0x中,size_t的格式说明符是%zu)。然而,你不应该使用printf:
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?)
记住,实际的循环逻辑可能有缺陷。(正如genpfault所指出的,当dataLength - 7为负数时,会发生什么情况?)
#2
2
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.
一旦您开始使用带有可能为负的值的无符号算术,并使用比较运算符如<,您就有麻烦了。更容易保持东西签名。
#3
1
Is dataLength >= 7? If the result of dataLength-7 is negative, if you interpret it as unsigned, the result is a very large integer.
dataLength > = 7吗?如果dataLength-7的结果是负数,如果您将其解释为无符号,则结果是一个非常大的整数。
#4
0
Use size_t for i.
对我使用size_t。
For printf, if you don't have C99, only C90, cast to unsigned long, or unsigned long long. E.g.:
对于printf,如果没有C99,只有C90,则被转换为无符号long,或无符号long。例如:
for (size_t i = 0; i < 10; ++i)
//printf("%llu\n", (unsigned long long)i);
printf("%lu\n", (unsigned long)i);
Otherwise use %zu
否则使用%祖茂堂
#5
0
You should first check if event.packet->dataLength < 7
. Now if it's less then 7 you get values less than 0 used as unsigned: e.g. 0 = 0x00000000; -1 = 0 - 1 = 0xFFFFFFFF.
您应该首先检查事件是否发生。包- > dataLength < 7。现在如果它小于7就会得到小于0的值作为无符号:例如,0 = 0x00000000;-1 = 0 -1 = 0xffffff。
Again, the check:
再一次,检查:
if (event.packet->dataLength < 7) {
...
} else {
for (size_t i = 0; i < event.packet->dataLength - 7; i+=10) { }
}
#6
0
"every time I do something like this or attempt at my conversions the i < # part is a huge number."
“每次我做这样的事情或尝试转换I < #部分都是一个很大的数字。”
That indicates that original packet length is less than 7 (you're subtracting 7).
这表明原始数据包长度小于7(减去7)。
One fix is to use an in-practice-large-enough signed integer type, and the standard library provides ptrdiff_t
for that purpose. Like,
一种修复方法是使用实际中足够大的带符号整数类型,标准库为此提供ptrdiff_t。就像,
#include <stdlib.h> // Not sure, but I think it was this one.
typedef ptrdiff_t Size;
typedef Size Index;
void foo()
{
// ...
for( Index i = 0; i < Size( event.packet->dataLength ) - 7; i += 10 )
{
// ...
}
}
A more cumbersome workaround is to embed the whole thing in an if
that checks that the size is at least 7.
一个更麻烦的解决方案是,如果检查的大小至少为7,那么将整个东西嵌入到an中。
Cheers & hth.,
欢呼声& hth。
#7
0
Since event.packet->dataLength
returns an unsigned type size_t
:
因为事件。>dataLength返回一个无符号类型size_t:
1) Use size_t
as the index variable type.
1)使用size_t作为索引变量类型。
2) Insure math does not underflow. @beldaz. Rather than subtract 7 from event.packet->dataLength
, add 7 to i
.
2)确保数学不欠流。@beldaz。而不是从事件中减去7。数据包->dataLength,添加7给i。
// for (int i = 0; i < event.packet->dataLength - 7; i+=10) { }
for (size_t i = 0; i + 7 < event.packet->dataLength; i += 10) { }