This is my minimal reproducible example.
struct Cell
{
};
void initializeCells(unsigned int width, unsigned int height, unsigned int size, Cell* outcell)
{
outcell = new Cell[(width / size) * (height / size)];
}
int main(int argc, char* argv[])
{
Cell* cells = nullptr;
initializeCells(1200, 600, 10, cells);
}
I am getting a warning in line outcell = new Cell[(width / size) * (height / size)]; saying
Warning 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).
How can I cast the value to a wider type like the warning is suggesting? Also something else that's weird is that the warning sometimes disappears for a few minutes and comes back again and is inconsistent. I am using visual studio 2019 and the inbuilt compiler.
size_tand hover my mouse over it, it saysunsigned long long. Isn't that a complete overkill to store anintof 1200 and 600?new[]operator is called.size_tyou'll likely just be using 3 64-bit registers rather than 3 32-bit registers; and there's nothing wrong with passingunsigned inttypes - they'll be silently promoted tosize_t.