简体   繁体   中英

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 . 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. Does the sizeof(b) indicate that this just treated as a 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 ). 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).

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

Then why is declaration of b valid.

It is in fact, not valid in standard C++.

Does the sizeof(b) indicate that this just treated as a int pointer

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.

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. However, the default value of int serves the purpose here. 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? they have some good explanations.

First, int b[n] is not a valid C++ code, unless n is a constant expression. 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 ).

Second, to check what is going on, let us see this:

My program 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] 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; within a function, n remains uninitialized. So when you write int a[n]; after it, the length of a can be ANYTHING AT ALL. It also can be too big to allocate a .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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