简体   繁体   English

[C++]即使自第一次声明以来从未分配过变量,该值也已更改

[英][C++]The value has changed even though a variable has never been assigned since the first declaration

Envs环境

g++ (Ubuntu 7.5.0-3ubuntu1~18.04) 7.5.0 g++ (Ubuntu 7.5.0-3ubuntu1~18.04) 7.5.0

Background背景

I practice implementing DFS.我练习实施 DFS。
I tried to solve this problem .我试图解决这个问题

Code代码

sorry for dirty code抱歉脏代码

#include<iostream>
using namespace std;

int dfs(int params[4], int i, int arr[3], int sum) {
    cout << "i: " << i << endl;
    cout << "params[0]: " << params[0] << endl << endl;
    if (i == 4) {
        if (sum == 7) {
            cout << params[0];
            for (int x = 1; x < 4; x++) {
                if (arr[x]) {
                    cout << '+';
                } else {
                    cout << '-';
                }
                cout << params[x];
            }
            cout << "=7" << endl;
            exit(0);
        }
        return 0;
    }
    arr[i] = 1;
    dfs(params, i + 1, arr, sum + params[i]);
    arr[i] = 0;
    dfs(params, i + 1, arr, sum - params[i]);
}

int main() {
    string num; cin >> num;
    int A = num[0] - '0';
    int B = num[1] - '0';
    int C = num[2] - '0';
    int D = num[3] - '0';
    int params[4] = {A, B, C, D};
    int arr[3];
    dfs(params, 1, arr, A);
}

What happened发生了什么

The params[0] is declared as the first number of input. params[0] 被声明为输入的第一个数字。
And I never changed the value.而且我从来没有改变过这个值。
But when I get the value, sometimes it changes.但是当我得到值时,有时它会改变。

Input and Output输入和输出

input输入

1222

output(This is no problem)输出(这没问题)

$ ./c
1222
i: 1
params[0]: 1

i: 2
params[0]: 1

i: 3
params[0]: 1

i: 4
params[0]: 1

1+2+2+2=7

input2输入2

0290

output2输出2

$ ./c
0290
i: 1
params[0]: 0

i: 2
params[0]: 0

i: 3
params[0]: 0

i: 4
params[0]: 1

i: 4
params[0]: 0

i: 3
params[0]: 0

i: 4
params[0]: 1

i: 4
params[0]: 0

i: 2
params[0]: 0

i: 3
params[0]: 0

i: 4
params[0]: 1

1-2+9+0=7

input3输入3

3242

output3输出3

3242
i: 1
params[0]: 3

i: 2
params[0]: 3

i: 3
params[0]: 3

i: 4
params[0]: 1

i: 4
params[0]: 0

0+2+4-2=7

The value of params[0] should always stay. params[0] 的值应始终保持不变。
Even if I change the type int to const int , the outputs didn't change.即使我将类型int更改为const int ,输出也没有改变。

I want to know我想知道

Why this happened?为什么会这样?

The array arr has only 3 elements, so arr[3] is out-of-range and no read and write to there are allowed.数组arr只有 3 个元素,因此arr[3]超出范围并且不允许对其进行读取和写入。 It seems params is happened to be placed just after arr and writing to arr[3] is breaking data in params .似乎params恰好放在arr之后,写入arr[3]会破坏params数据。

To easily fix this, allocate one more element for arr .要轻松解决此问题,请为arr再分配一个元素。

Better fix is to fix range of i not to cause this out-of-range access.更好的解决方法是修复i范围,以免造成这种超出范围的访问。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 字符常量数组为空,即使已为其赋值[C ++] - Character Constant Array is empty, even though values has been assigned to it [C++] 变量具有值而不被赋予C ++ - Variable has value without being assigned C++ 即使变量第一次被赋值,C++ 程序也要求我输入两次 - C++ Program requires me to input twice even though the variable gets assigned the first time 即使运算符已重载,也没有运算符=与这些操作数匹配c ++ - No operator = match these operands c++ even though said operator has been overloaded 从未使用C / C ++默认值/分配给变量的值 - C/C++ default value/value assigned to variable is never used 赋值后清除 C++ 中固定字符数组的内容 - Clearing contents of a fixed char array in C++ after value has been assigned 为什么我会收到此错误? “[变量]没有命名类型”在 C++ 中用于 std::string 数组,即使它已包含在同一个 scope 中 - Why do I get this error? “[variable] does not name a type” in C++ for std::string array even though it has been included and is in the same scope 即使已为其分配了非零指针,指针仍返回零 - pointer returns zero even though it has been assigned a non zero pointer 为什么Windows无法识别我的C ++应用程序已更改注册表项,即使注册表和UI显示更新? - Why is Windows is not recognizing that my C++ app has changed a registry key, even though the registry and UI show the update? 如何确定c ++中的全局变量是否发生了变化? - How to find out if a global variable has changed in c++?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM