size_t vs int warning

前端 未结 5 1083
小鲜肉
小鲜肉 2020-12-14 15:08

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

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

warning C

5条回答
  •  天命终不由人
    2020-12-14 15:39

    is this safe to ignore this warning or should I make all my loop variable of type 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.

    Technically, std::vector::size returns std::vector::size_type, though.

    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)

提交回复
热议问题