简体   繁体   English

在一定长度后从数组打印出时缺少 integer

[英]Missing integer when printing out from array after certain length

I have the following code:我有以下代码:

#include <iostream>

using namespace std;

int main()
{
    int length = 0;
    int arrA[length];

    cout << "Enter the length : ";
    cin >> length;

    cout << "Enter " << length << " integers for array : ";

    for(int i = 0; i < length; i++)
    {
        cin >> arrA[i];
    }

    cout << "Array A : ";
    for(int i = 0; i < length; i++)
    {
        cout << arrA[i] << " ";
    }
    cout << endl;
}

The above require the user to input the length of array followed by the integers that will be store in array.以上要求用户输入数组的长度,然后输入将存储在数组中的整数。 It work but the printing of the array value is incorrect when I input length 8 and above.它可以工作,但是当我输入长度为 8 及以上时,数组值的打印不正确。

Working在职的

Enter the length : 7
Enter 7 integers for array : 7 6 5 4 3 2 1
Array A : 7 6 5 4 3 2 1

Not working不工作

Enter the length : 8
Enter 8 integers for array : 8 7 6 5 4 3 2 1
Array A : 8

Could this be due to memory issue or something?这可能是由于 memory 问题还是什么?

int length = 0;
int arrA[length];

This creates an array of size zero这将创建一个大小为零的数组

cout << "Enter the length : ";
cin >> length;

This sets the length variable to a value entered by the user but doesn't change the size of arrA which has already been created .这会将length变量设置为用户输入的值,但不会更改已创建arrA的大小。

You obviously think that when the length variable changes that the array arrA will change as well, but this is not true.您显然认为当length变量更改时,数组arrA也会更改,但事实并非如此。

Because you are accessing elements of an array which has zero size the behaviour of your program is undefined.因为您正在访问大小为零的数组的元素,所以程序的行为是未定义的。

In addition this whole approach is incorrect because此外,这整个方法是不正确的,因为

int length;
...
int arrA[length];

is not legal C++.不合法 C++。 Because length is a variable this is a variable length array or VLA.因为length是一个变量,所以这是一个可变长度数组或 VLA。 VLAs are legal in C but are not legal in C++. VLA 在 C 中是合法的,但在 C++ 中是不合法的。

The best way to write this code in C++ is to use a vector.在 C++ 中编写此代码的最佳方法是使用向量。 A vector is the C++ improvement on a C array.向量是对 C 数组的 C++ 改进。

#include <iostream>
#include <vector>

using namespace std;

int main()
{
    int length;

    cout << "Enter the length : ";
    cin >> length;

    vector<int> arrA(length);
    ...
int length = 0;
int arrA[length];

This is not valid C++.这不是有效的 C++。 What you may want to do is:您可能想要做的是:

int *arrA = new int[length];

after cin >> length .cin >> length之后。 You will have to free the pointer later with delete[] arrA;稍后您将不得不使用delete[] arrA;

It is not due to a memory issue.这不是由于memory 问题造成的。 But because variable length arrays are not part of the C++ standard.但是因为可变长度 arrays 不是 C++ 标准的一部分。 length is a variable length array. length是一个可变长度数组。 Read more about variable length arrays阅读有关可变长度 arrays 的更多信息
Here 这里

Here is the code if you do not want to use a vector .如果您不想使用vector ,这里是代码。 I would suggest not to use this for multifarious reasons.我建议不要出于多种原因使用它。 But go ahead and knock yourself out.但是 go 领先并击倒自己。

#include <iostream>

using namespace std;

int main()
{
    int length = 0;

    int *arrA = new int [length];

    cout << "Enter the length : ";
    cin >> length;
   
    cout << "Enter " << length << " integers for array : ";

    for (int i = 0; i < length; i++)
    {
        cin >> arrA[i];
        
    }
   

    cout << "Array A : ";
    for (int i = 0; i < length; i++)
    {
        cout << arrA[i] << " ";
    }
    
    delete[] arrA;
    cout << endl;
    return 0;
}

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM