Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 为什么输出不同,代码中有什么错误?_C++_Arrays_C++98 - Fatal编程技术网

C++ 为什么输出不同,代码中有什么错误?

C++ 为什么输出不同,代码中有什么错误?,c++,arrays,c++98,C++,Arrays,C++98,因此,我一直在尝试获取数组大小及其元素的输入,然后将这些元素显示在屏幕上 阵列大小:7 数组元素:1234567 输出为: 1 2 3 4 5 6 6 守则: #include <iostream> using namespace std; int main () { int n , Arr[n]; cout << "please put the size of the array " ; cin >> n

因此,我一直在尝试获取数组大小及其元素的输入,然后将这些元素显示在屏幕上 阵列大小:7 数组元素:1234567 输出为:

1
2
3
4
5
6
6
守则:

#include <iostream>

using namespace std;

int main () {    
    int n , Arr[n];    
    cout << "please put the size of the array " ;    
    cin >> n;    
    cout << "please enter array's elemets ";    
    for (int k=0; k<n ; k++) {    
        cin >> Arr[k];    
    }    
    for (int i=0;i<n;i++){    
        cout << Arr[i] << endl;    
    }    
}
#包括
使用名称空间std;
int main(){
int n,Arr[n];
cout>n;
cout-Arr[k];
}    

对于(int i=0;i <代码> int ARR[n] < /COD>其中<代码> n< /代码>不是编译时常量是非法C++代码。有些编译器允许它作为扩展(可变长度数组)。 即使使用VLA扩展,代码也是无效的,因为
n
在代码中使用时未初始化

首先是真正的解决方案:

使用
std::vector
(tadaaa):

#包括
#包括
int main(){
int n;
std::载体arr;
std::cout>n;
arr.reserve(n);//可选
std::cout元素;
arr.推回(elem);
}
用于(自动e:arr){

STD::CUT<代码> INTAR[n] < /COD> >代码> N< /代码>不是编译时常量,是非法C++代码。有些编译器允许它作为扩展(可变长度数组)。 即使使用VLA扩展,代码也是无效的,因为
n
在代码中使用时未初始化

首先是真正的解决方案:

使用
std::vector
(tadaaa):

#包括
#包括
int main(){
int n;
std::载体arr;
std::cout>n;
arr.reserve(n);//可选
std::cout元素;
arr.推回(elem);
}
用于(自动e:arr){

std::cout正如许多其他人在注释部分中提到的,另一种方法(如果您想坚持使用C数组)是在堆上动态分配数组

#include <iostream>

using namespace std;

int main () {    
    int n;    
    cout << "please put the size of the array " ;    
    cin >> n; 
    int* Arr = new int[n]; //dynamically allocate an array to hold n int on the heap
    cout << "please enter array's elemets ";    
    for (int k=0; k<n ; k++) {    
        cin >> Arr[k];    
    }    
    for (int i=0;i<n;i++){    
        cout << Arr[i] << endl;    
    }    
    delete [] Arr; //make sure to clean up the heap memories
}
#包括
使用名称空间std;
int main(){
int n;
cout>n;
int*Arr=newint[n];//动态分配一个数组以在堆上保存n int
cout-Arr[k];
}    

对于(inti=0;i,正如许多其他人在注释部分中提到的那样,另一种方法(如果您想坚持使用C数组)是在堆上动态分配数组

#include <iostream>

using namespace std;

int main () {    
    int n;    
    cout << "please put the size of the array " ;    
    cin >> n; 
    int* Arr = new int[n]; //dynamically allocate an array to hold n int on the heap
    cout << "please enter array's elemets ";    
    for (int k=0; k<n ; k++) {    
        cin >> Arr[k];    
    }    
    for (int i=0;i<n;i++){    
        cout << Arr[i] << endl;    
    }    
    delete [] Arr; //make sure to clean up the heap memories
}
#包括
使用名称空间std;
int main(){
int n;
cout>n;
int*Arr=newint[n];//动态分配一个数组以在堆上保存n int
cout-Arr[k];
}    
对于(int i=0;iFrom:

如果没有为对象指定初始值设定项,则该对象为 默认已初始化。当使用自动或 获取动态存储持续时间时,对象具有不确定的 值,并且如果没有对对象执行初始化,则 对象保留一个不确定的值,直到该值被替换为止 ([expr.ass])

因此,在您的代码中:

int n , Arr[n]; 
n
有一个不确定的值,直到它被分配到
cin>>n;

n
与此不确定值(非值-/zero-/default初始化和未分配)一起使用,可能会导致未定义的行为。

来自:

如果没有为对象指定初始值设定项,则该对象为 默认已初始化。当使用自动或 获取动态存储持续时间时,对象具有不确定的 值,并且如果没有对对象执行初始化,则 对象保留一个不确定的值,直到该值被替换为止 ([expr.ass])

因此,在您的代码中:

int n , Arr[n]; 
n
有一个不确定的值,直到它被分配到
cin>>n;



n
与此不确定值(非值-/zero-/default初始化和未分配)一起使用,可能会导致未定义的行为。

int n,Arr[n]是一个bug。实际上,在编译时,在标准的C++中,代码1以上的bug……必须在编译时知道,因为数组的大小在编译时是固定的。在扩展之前编译,<代码> N<代码>仍然需要在声明之前初始化。即使使用VLA扩展, N< /Cord>未被初始化,当使用建议阅读C++图书时,肯定您的学校的图书馆有。文件必须被命名为“IoSo.h”。这是很久没有真正的。<代码> int n,ARR[n]是一个bug。实际上,在编译时,在标准的C++中,代码1以上的bug……必须在编译时知道,因为数组的大小在编译时是固定的。要通过扩展来编译, N在声明之前仍然需要初始化。即使使用VLA扩展, N< /Cord>未初始化,当使用建议阅读C++图书时,肯定您的学校的图书馆有。文件必须被命名为“IoSo.h”。c98中不允许:(任何特定原因您使用C++98?哈哈,旧PC和旧IDE:(@lazycracker,这不是借口。您可以安装一个轻量级IDE。好的,要这样做:)thnx BTW:'))For(auto e:arr)在c98中不允许:(任何特定原因您使用C++98?哈哈,旧PC和旧IDE:(@lazycracker这不是借口。你可以安装一个轻量级的IDE。好的,我要这么做:)thnx BTW:“)是的,这太棒了:)我想过这样使用它:)不,这不是很好。实际上,C++是不应该使用的。在C++中,你不应该使用原始代码>新的< /代码> />代码>删除< /代码>。当你把这个放进一个更大的程序中的函数时,会发生什么,在<代码>新的[] /代码>和<代码>删除[]/Cord>?不,,(我不知道,你能指导我吗?我是初学者)这将是一个内存泄漏,因为与堆栈上的变量/对象不同,您需要调用delete来释放分配的堆内存。如果其间存在异常,您的程序将无法到达delete…行,从而导致内存泄漏。正如bolov所建议的,使用std::vector更安全、更好。希望这不会让您感到困惑。是的这太棒了:)我想到了
#include <iostream>

using namespace std;

int main () {    
    int n;    
    cout << "please put the size of the array " ;    
    cin >> n; 
    int* Arr = new int[n]; //dynamically allocate an array to hold n int on the heap
    cout << "please enter array's elemets ";    
    for (int k=0; k<n ; k++) {    
        cin >> Arr[k];    
    }    
    for (int i=0;i<n;i++){    
        cout << Arr[i] << endl;    
    }    
    delete [] Arr; //make sure to clean up the heap memories
}
unsigned char c;
unsigned char d = c;        // OK, d has an indeterminate value
int e = d;                  // undefined behavior
int n , Arr[n];