gpt4 book ai didi

c++ - 如何解决 Visual Studio C++ 问题, "Arithmetic overflow: Using operator ' *' on a 4 byte value and then casting the result to a 8 byte value."?

转载 作者:行者123 更新时间:2023-11-30 04:43:54 25 4
gpt4 key购买 nike

我已经标记了警告位置。如果我理解正确,将两个 32 位数字相乘可以得到 64 位,所以我得到了一个错误。

如果 C++ int 是 32 位那么存储 64 位不是也溢出了吗?请解释我做错了什么。警告消息显示在 Visual Studio 2019 中。

转换为更大的数据类型解决了这个问题。为什么?

#include <iostream>
using namespace std;


unsigned int m;
unsigned int n;


// Symbols
int symbols[] = { -1, 0, 1 };
unsigned int symbols_size = sizeof(symbols) / sizeof(symbols[0]);



// Returns false to signify that element exiting in array
bool check_symbol_validity(int val) {
for (unsigned int i = 0; i < symbols_size; ++i) {
if (symbols[i] == val) {
return false;
}
}
return true;
}


int main()
{

cout << "Place enter graph node count: ";
cin >> m;
n = m;


// Just a separator
cout << string(30, '#') << "\n";
cout << "Enter Transition Symbol Between Node i and Node j. \nIf none then enter -1.\n";



// Transition table, take node and symbol, return next node
//int* transition_table = new int[m * symbols_size]; // WARNING
int* transition_table = new int[(int64_t)m * symbols_size]; // OK


// Declare C++ dynamic array
//int* weight_matrix = new int[m * n]; // WARNING
//int* weight_matrix = new int[(long)m * n]; // WARNING
int* weight_matrix = new int[(long long)m * n]; // OK


// Store positional values for direct access rather than using multiplication
int* width_val_arr = new int[m];


// TAKE VALID INPUT
int user_input_value;
for (unsigned int i = 0; i < m; ++i) {
int tmp1 = i * m;
width_val_arr[i] = tmp1;
for (unsigned int j = 0; j < n; j++){
cout << "Enter connection between " << i << " and " << j << "\n";
cin >> user_input_value;
while (check_symbol_validity(user_input_value)) {
cout << "WRONG! ENTER CORRECT VALUE." << "\n";
cout << "Enter connection between " << i << " and " << j << "\n";
cin >> user_input_value;
}
weight_matrix[tmp1 + j] = user_input_value;
}
}


cout << string(30, '#') << "\n";


// SHOW THE WEIGHT MATRIX
for (unsigned int i = 0; i < m; ++i) {
//int tmp1 = i * m;
for (unsigned int j = 0; j < n; j++) {
cout << weight_matrix[width_val_arr[i] + j] << " ";
}
cout << "\n";
}



// Delete the dynamic arrays
delete[] weight_matrix;
delete[] transition_table;
delete[] width_val_arr;
}

警告信息:

"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".

最佳答案

C26451 RESULT_OF_ARITHMETIC_OPERATION_CAST_TO_LARGER_SIZE Using operator

Cast the value to the wider type before calling operator [operator] to avoid overflow

This warning indicates incorrect behavior that results from integral promotion rules and types larger than those in which arithmetic is typically performed. We detect when a narrow type integral value was shifted left, multiplied, added, or subtracted and the result of that arithmetic operation was cast to a wider type value. If the operation overflowed the narrow type value, then data is lost. You can prevent this loss by casting the value to a wider type before the arithmetic operation.

Arithmetic overflow checks in C++ Core Check

关于c++ - 如何解决 Visual Studio C++ 问题, "Arithmetic overflow: Using operator ' *' on a 4 byte value and then casting the result to a 8 byte value."?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57975446/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com