简体   繁体   English

c++ 中的数组初始化?

[英]Array initialization in c++?

int main()
{
    clrscr();
    int x[3];
    int n;
    cout<<"Enter the array size= ";
    cin>>n;
    cout<<"Enter the elements for array= ";
    for(int i=0;i<n;i++)
    {
        cin>>x[i] ;
    }
    for(i=0;i<n;i++)
    {
        cout<<"x["<<i<<"]="<<x[i]<<"\n";
    }
    getch();
    return 0;
}

When m trying the same logic in c# then I got the right output as if I enter the size of array more than I initialize it gives the exception.当我在 c# 中尝试相同的逻辑时,我得到了正确的 output,就好像我输入的数组大小超过了我初始化它的大小一样。 But in c++ m not getting any type of error neither on compilation time nor at run time.但是在 c++ m 中,无论是在编译时还是在运行时,都没有出现任何类型的错误。 But according to rule it should be give some error at run time in output if i give array size more than I initialize.但是根据规则,如果我给出的数组大小超过了我的初始化,它应该在 output 的运行时给出一些错误。 And one thing more why it determine the 09 as two numbrs not single as 90 it shows it 0 and 9 at differect index as in output.还有一件事,为什么它将 09 确定为两个数字而不是单个数字 90,它在 output 中的不同索引处显示 0 和 9。

输出

在此处输入图像描述

If you have an array:如果你有一个数组:

char array[3];

And you try to write into an element that doesn't exist:你尝试写入一个不存在的元素:

array[15] = '!';

Then this is an error.那么这一个错误。 However, a compiler is not required by the C++ Standard to diagnose this error, and most do not.但是,C++ 标准不需要编译器来诊断此错误,而且大多数都不需要。 This is because it would require computation every time you accessed the array to determine whether you were within the array bounds.这是因为每次访问数组时都需要计算以确定您是否在数组范围内。

Instead, it is up to the programmer to ensure that he/she uses the array correctly , by writing these bounds checks his- or herself.相反,由程序员自己来确保他/她正确使用数组,通过编写这些边界检查他或她自己。 Then the checks are only performed when the programmer has deemed them necessary, and there are no wasted computations.然后仅在程序员认为必要时才执行检查,并且不会浪费计算。

So:所以:

std::cin >> n;

if (n > 3)
   throw std::runtime_error("OMG not enough space in my array!");

C++ and in-turn C are not required to throw an exception when you over-run an array. C++ 和 C 不需要在超出数组时引发异常。 Arrays are simply memory blocks, and when you over-run them, the only time you will encounter an error is when you have exceeded the bounds of what the operating system run-time allows you to access as a user in memory. Arrays 只是 memory 块,当您过度运行它们时,您唯一会遇到错误的情况是您超出了操作系统运行时允许您以用户身份访问 ZCD69B4957F098CD818D7BF3E26 的范围。 For instance, if you over-ran your array, and wrote over the values for the return pointer for your function, and then when the function returns, it tries to place in the instruction pointer register a bogus return address, the OS itself will throw a segmentation fault because you tried to execute code beyond a memory address you were allowed.例如,如果你超出了你的数组,并重写了 function 的返回指针的值,然后当 function 返回时,它会尝试在指令指针寄存器中放置一个虚假的返回地址,操作系统本身会抛出分段错误,因为您尝试执行超出允许的 memory 地址的代码。 Segmentation faults and bus error messages though are OS-specific, not specific to C or C++ (ie, you'd get the same errors with assembly).分段错误和总线错误消息虽然是特定于操作系统的,而不是特定于 C 或 C++ (即,您会在组装时遇到相同的错误)。

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

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