简体   繁体   English

将size_t转换为整数(c ++)

[英]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. 我一直在尝试创建一个for循环,它将根据网络数据包的长度进行迭代。 In the API there exists a variable (size_t) by event.packet->dataLength. 在API中,event.packet-> dataLength存在变量(size_t)。 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. 我想从0迭代到event.packet-> dataLength - 7每次迭代时我增加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. 我尝试将size_t转换为unsigned int并使用它进行算术但不幸的是它没有用。 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. 虽然每次我做这样的事情或尝试我的转换时,我<#part都是一个巨大的数字。 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. 他们在API的教程中给出了一个printf语句,它使用“%u”来打印实际数字,但是当我将它转换为unsigned int时,它仍然是不正确的。 I'm not sure where to go from here. 我不知道从哪里开始。 Any help would be greatly appreciated :) 任何帮助将不胜感激 :)

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. 在C ++ 03中没有size_t格式说明符,你必须转换为最大的无符号整数类型并打印它。 (The format specifier for size_t in C++0x is %zu ). (C ++ 0x中size_t的格式说明符是%zu )。 However, you shouldn't be using printf anyway: 但是,你不应该使用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为负时会发生什么?)

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. 保持签名更容易。

Is dataLength >= 7? 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的结果为负,如果将其解释为无符号,则结果为非常大的整数。

Since event.packet->dataLength returns an unsigned type size_t : 由于event.packet->dataLength返回无符号类型size_t

1) Use size_t as the index variable type. 1)使用size_t作为索引变量类型。

2) Insure math does not underflow. 2)确保数学不会下溢。 @beldaz . @beldaz Rather than subtract 7 from event.packet->dataLength , add 7 to i . 而不是从event.packet->dataLength减去7,而是向i添加7。

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

Use size_t for i. 使用size_t为i。

For printf, if you don't have C99, only C90, cast to unsigned long, or unsigned long long. 对于printf,如果你没有C99,只有C90,强制转换为无符号长整数或无符号长整数。 Eg: 例如:

for (size_t i = 0; i < 10; ++i)
        //printf("%llu\n", (unsigned long long)i);
        printf("%lu\n", (unsigned long)i);

Otherwise use %zu 否则使用%zu

You should first check if event.packet->dataLength < 7 . 您应该首先检查event.packet->dataLength < 7 Now if it's less then 7 you get values less than 0 used as unsigned: eg 0 = 0x00000000; 现在,如果它小于7,则得到小于0的值作为无符号使用:例如0 = 0x00000000; -1 = 0 - 1 = 0xFFFFFFFF. -1 = 0 - 1 = 0xFFFFFFFF。

Again, the check: 再次,检查:

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

"every time I do something like this or attempt at my conversions the i < # part is a huge number." “每当我做这样的事情或尝试我的转换时,我<#part就是一个巨大的数字。”

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. 一种解决方法是使用实​​践中足够大的有符号整数类型,标准库为此提供ptrdiff_t Like, 喜欢,

#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. 一个更麻烦的解决方法是将整个内容嵌入到if ,检查大小是否至少为7。

Cheers & hth., 干杯&hth。,

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM