【发布时间】:2020-03-01 21:33:56
【问题描述】:
我有一个名为 dim 的 32 位整数,我正在使用 new 保留一个无符号字符数组,即 dim * dim * dim 大。 Visual Studio 说我应该在执行此操作之前将dim 转换为 64 位整数,但我觉得这并不重要。
unsigned char* cells = new unsigned char[dim * dim * dim];
C26451: Arithmetic overflow: Using operator '*' on a 4 byte value and then casting the result to a 8 byte value. Cast the value to the wider type before calling operator '*' to avoid overflow (io.2).
dim 不会大于 1000。
【问题讨论】:
-
如果 dim 可以是 1.000,那么 dim * dim * dim 可以是 1.000.000.000,这至少超出了
int的较小实现。 -
@Yunnosch 警告告诉我们
int是 4 个字节,就像在现代 Windows 上一样。 -
@AsteroidsWithWings True.
标签: c++