C++ size_t vs int 警告

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6578048/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 20:23:25  来源:igfitidea点击:

size_t vs int warning

c++visual-c++stl

提问by Avinash

I am getting following warning always for following type of code.

对于以下类型的代码,我总是收到以下警告。

std::vector v;
for ( int i = 0; i < v.size(); i++) {
}

warning C4267: 'initializing' : conversion from 'size_t' to 'int', possible loss of data

warning C4267: 'initializing' : conversion from 'size_t' to 'int', possible loss of data

I understand that size()returns size_t, just wanted to know is this safe to ignore this warning or should I make all my loop variable of type size_t

我知道size()返回size_t,只是想知道忽略此警告是否安全,或者我应该将所有循环变量设为类型size_t

回答by Ben Voigt

If you might need to hold more than INT_MAXitems in your vector, use size_t. In most cases, it doesn't really matter, but I use size_tjust to make the warning go away.

如果您可能需要INT_MAX在向量中保存多个项目,请使用size_t. 在大多数情况下,这并不重要,但我size_t只是为了让警告消失。

Better yet, use iterators:

更好的是,使用迭代器:

for( auto it = v.begin(); it != v.end(); ++it )

(If your compiler doesn't support C++11, use std::vector<whatever>::iteratorin place of auto)

(如果您的编译器不支持 C++11,请使用std::vector<whatever>::iterator代替auto

C++11 also makes choosing the best index type easier (in case you use the index in some computation, not just for subscripting v):

C++11 还使选择最佳索引类型变得更容易(以防您在某些计算中使用索引,而不仅仅是用于下标v):

for( decltype(v.size()) i = 0; i < v.size(); ++i )

回答by Alok Save

What is size_t?
size_tcorresponds to the integral data type returned by the language operator sizeofand is defined in the header file (among others) as an unsigned integral type.

什么是size_t
size_t对应于语言运算符返回的整数数据类型,sizeof并在头文件(以及其他文件)中定义为unsigned integral type.

Is it okay to cast size_tto int?
You could use a cast if you are sure that size is never going to be > than INT_MAX.

可以投射size_tint吗?
如果您确定 size 永远不会 > than ,则可以使用演员表INT_MAX

If you are trying to write a portable code, it is notsafe because,

如果你想写一个可移植的代码,它是不是安全的,因为,

size_tin 64 bit Unixis 64 bits
size_tin 64 bit Windowsis 32 bits

size_t64 bit Unix64 bits
size_t64 bit Windows32 bits

So if you port your code from Unix to WIndows and if above are the enviornments you will lose data.

因此,如果您将代码从 Unix 移植到 WINdows,并且如果以上是环境,您将丢失数据。

Suggested Answer

建议答案

Given the caveat, the suggestion is to make iof unsigned integral typeor even better use it as type size_t.

鉴于警告,建议是使iunsigned integral type甚至更好的使用它作为类型size_t

回答by Billy ONeal

is this safe to ignore this warning or should I make all my loop variable of type size_t

忽略此警告是否安全,或者我应该将所有类型的循环变量设为 size_t

No. You are opening yourself up to a class of integer overflow attacks. If the vector size is greater than MAX_INT(and an attacker has a way of making that happen), your loop will run forever, causing a denial of service possibility.

不。您正在接受一类整数溢出攻击。如果向量大小大于MAX_INT(并且攻击者有办法做到这一点),您的循环将永远运行,从而导致拒绝服务的可能性。

Technically, std::vector::sizereturns std::vector::size_type, though.

不过,从技术上讲,std::vector::size返回std::vector::size_type

You should use the right signedness for your loop counter variables. (Really, for most uses, you want unsigned integers rather than signed integers for loops anyway)

您应该为循环计数器变量使用正确的符号。(实际上,对于大多数用途,无论如何您都需要无符号整数而不是有符号整数用于循环)

回答by tylerl

The problem is that you're mixing two different data types. On some architectures, size_tis a 32-bit integer, on others it's 64-bit. Your code should properly handle both.

问题是你混合了两种不同的数据类型。在某些架构上,size_t是 32 位整数,在其他架构上是 64 位。您的代码应该正确处理两者。

since size()returns a size_t(not int), then that should be the datatype you compare it against.

因为size()返回一个size_t不是 int),那么这应该是你比较它的数据类型。

std::vector v;
for ( size_t i = 0; i < v.size(); i++) {
}

回答by Jon

Here's an alternate view from Bjarne Stroustrup: http://www.stroustrup.com/bs_faq2.html#simple-program

这是 Bjarne Stroustrup 的另一种观点:http: //www.stroustrup.com/bs_faq2.html#simple-program

for (int i = 0; i<v.size(); ++i) cout << v[i] << '\n';

Yes, I know that I could declare i to be a vector::size_type rather than plain int to quiet warnings from some hyper-suspicious compilers, but in this case,I consider that too pedantic and distracting.

是的,我知道我可以声明 i 是一个 vector::size_type 而不是普通的 int 来避免一些超级可疑的编译器发出安静的警告,但在这种情况下,我认为这太迂腐和分散注意力了。

It's a trade-off. If you're worried that v.size() could go above 2,147,483,647, use size_t. If you're using i inside your loop for more than just looking inside the vector, and you're concerned about subtle signed/unsigned related bugs, use int. In my experience, the latter issue is more prevalent than the former. Your experience may differ.

这是一种权衡。如果您担心 v.size() 可能超过 2,147,483,647,请使用 size_t。如果您在循环中使用 i 不仅仅是查看向量内部,并且您担心微妙的有符号/无符号相关错误,请使用 int。根据我的经验,后一个问题比前一个问题更普遍。您的体验可能有所不同。

Also see Why is size_t unsigned?.

另请参阅为什么 size_t 未签名?.