简体   繁体   English

通过读取用户的值创建10个整数的输入数组

[英]Create an input array of 10 ints by reading values from the user

I am trying the following code : 我正在尝试以下代码:

int main() {
    int x[10] ;
    int a[] = {1,2,3,4,5} ;
    int n ;
    int b[n] ;
    //int c[] ; gives compilation error

    cout<<sizeof(x)<<endl ; //prints 40
    cout<<sizeof(a)<<endl ; //prints 20
    cout<<sizeof(b)<<endl ; //prints 4
}

Now my question is what exactly happens when I define b . 现在我的问题是当我定义b时到底发生了什么。 I have tried to read answers to similar questions but I did not get a satisfactory reply. 我试图阅读类似问题的答案,但我没有得到满意的答复。 Because arrays are created statically, so there size must be given at the time of declaring them. 因为数组是静态创建的,所以在声明它们时必须给出大小。 Then why is declaration of b valid. 那么为什么b声明有效。 Does the sizeof(b) indicate that this just treated as a int pointer sizeof(b)是否表明这只是作为int pointer处理

what exactly happens 究竟发生了什么

 int n ; int b[n] ; 

Using the value of uninitialized integer (except the narrow character type) has undefined behaviour ( UB ). 使用未初始化的整数值(窄字符类型除外)具有未定义的行为( UB )。 Furthermore, since n is not a compile time constant, but is used as a size of an array, the program is ill-formed according to the C++ standard (this means that the compiler is allowed to refuse to compile the program, and is required to at least show a diagnostic message). 此外,由于n不是编译时常量,而是用作数组的大小,程序根据C ++标准是不正确的(这意味着允许编译器拒绝编译程序,并且是必需的至少显示诊断信息)。

However, if your compiler supports variable length arrays ( VLA , a language extension), then exactly what happens depends on how the compiler has implemented VLA (and of course, how it happens to deal with the UB that you introduced). 但是,如果您的编译器支持可变长度数组( VLA ,语言扩展),那么究竟发生了什么取决于编译器如何实现VLA(当然,它是如何处理您引入的UB的)。

Then why is declaration of b valid. 那么为什么b声明有效。

It is in fact, not valid in standard C++. 这是在事实上,在标准C ++ 无效

Does the sizeof(b) indicate that this just treated as a int pointer sizeof(b)是否表明这只是作为int指针处理

No. Since the behaviour is undefined, the output indicates nothing. 不会。由于行为未定义,输出不显示任何内容。 Even if the size of int pointer happens to be 4 on your system, the output happens to be the same only by chance. 即使你的系统上int指针的大小恰好是4,输出也只是偶然相同。

In C++, variable length arrays are not allowed, arrays have to be fixed size, if you want to allocate size dynamically you may want to use malloc and allocate memory mannually. 在C ++中,不允许使用可变长度数组,数组必须是固定大小的,如果要动态分配大小,可能需要使用malloc并手动分配内存。 However, the default value of int serves the purpose here. 但是, int的默认值用于此目的。 I won't recommend this practice though as it may yield undefined behaviour. 我不会推荐这种做法,因为它可能会产生未定义的行为。 Read these links Array[n] vs Array[10] - Initializing array with variable vs real number and Why aren't variable-length arrays part of the C++ standard? 阅读这些链接Array [n] vs Array [10] - 初始化具有变量与实数的数组, 为什么不是C ++标准的可变长度数组? they have some good explanations. 他们有一些很好的解释。

First, int b[n] is not a valid C++ code, unless n is a constant expression. 首先, int b[n]不是有效的C ++代码,除非n是常量表达式。 You get away with it only because you use GCC, and it is permitted as an extension ( https://gcc.gnu.org/onlinedocs/gcc/Variable-Length.html#Variable-Length ). 你只是因为使用GCC而逃脱它,并且允许它作为扩展( https://gcc.gnu.org/onlinedocs/gcc/Variable-Length.html#Variable-Length )。

Second, to check what is going on, let us see this: 其次,要检查发生了什么,让我们看看:

My program var-arr.cpp: 我的程序var-arr.cpp:

#include <iostream>

using namespace std;

int main() {
    int m=0, n=5, k;
    int a[m], b[n], c[k];
    cout
    << "sizeof(a)=" << sizeof(a)
    << " sizeof(b)=" << sizeof(b)
    << " sizeof(c)=" << sizeof(c)
    << endl;
}

Here is what happens when I compile and run it ( [~/CPP] is the prompt) 这是我编译运行时发生的事情( [~/CPP]是提示)

[~/CPP] g++ -o var-arr var-arr.cpp
[~/CPP] ./var-arr
sizeof(a)=0 sizeof(b)=20 sizeof(c)=0

Some time later 一段时间之后

[~/CPP] ./var-arr
Segmentation fault (core dumped)

What does it mean? 这是什么意思? It means that when you write int n; 这意味着当你写int n; within a function, n remains uninitialized. 在函数内, n仍然未初始化。 So when you write int a[n]; 所以当你写int a[n]; after it, the length of a can be ANYTHING AT ALL. 之后,长度a可以任何东西。 It also can be too big to allocate a . 它也可以是过大,分配a

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

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