简体   繁体   English

请解释以下代码的输出

[英]please explain the ouptput of following code

i have been trying to insert a new element into a given array at a given index.我一直在尝试在给定索引处将新元素插入给定数组。 though i am able to insert new element but i have few doubts..虽然我能够插入新元素,但我几乎没有怀疑..

  1. why it got inserted at 7th index instead at 3?为什么它插入第 7 个索引而不是第 3 个?
  2. how value of variable k become 7?变量 k 的值如何变为 7?
  3. why size of old and new array is same though new array has 1 more element?为什么新数组和新数组的大小相同,但新数组还有 1 个元素?

thanks in advance.提前致谢。 any help would be appreciated.任何帮助,将不胜感激。

int main()
{

    int arr[] = {1,2,3,4,5,6,7};
    int n = sizeof(arr)/sizeof(arr[0]);
    int k = 3;

    cout << "elements of old array" << endl;
    for (int i=0; i<n; i++){
        cout << arr[i] << endl;
    }
    cout << "size of old array  " << sizeof(arr) << endl << endl << endl;

    // inserting new element

    for (int j=n; j>k; j--){
        arr[j] = arr[j-1];
    }

    arr[k] = 10;

    cout << "elements of new array  " << endl;
    for (int i=0; i<=n; i++){
        cout << arr[i] << endl;
    }
    cout << "size of new array " << sizeof(arr)<< endl ;

   }

Classic C Memory Question!经典的C内存问题!

When you declare arr as an array it has a fixed length ... thus a fixed amount of space in memory.当您将 arr 声明为数组时,它的长度是固定的……因此内存中的空间量是固定的。 By shifting all the elements of arr to the right, you are not actually increasing the size of arr, you are just writing over whatever memory is directly adjacent to arr.通过将 arr 的所有元素向右移动,您实际上并没有增加 arr 的大小,您只是在写入与 arr 直接相邻的任何内存。

This explains why k is being set to 7. k must be stored adjacent to the end of arr in memory.这解释了为什么 k 被设置为 7。k 必须存储在内存中 arr 的末尾附近。 Thus, when you shift arr[n-1] to arr[n], k is being set to 7.因此,当您将 arr[n-1] 移至 arr[n] 时,k 被设置为 7。

If you want to fix this, I would suggest making a new array of size n + 1, then copying all the elements over with their new indices.如果你想解决这个问题,我建议创建一个大小为 n + 1 的新数组,然后用它们的新索引复制所有元素。

An array has a fixed size once it's created;数组一旦创建就具有固定大小; your array has length 7, and that's it - it's not going to change.您的数组长度为 7,仅此而已 - 它不会改变。

Your code shifts the array along by one element, which involves writing to arr[7] , which means 'store this value in the eighth element of array arr ', and the compiler happily does that - there's no bounds checking for arrays in C++.您的代码将数组移动了一个元素,这涉及写入arr[7] ,这意味着“将此值存储在数组arr的第八个元素中”,编译器很乐意这样做 - 在 C++ 中没有对数组进行边界检查。 However, it does not extend the length of the array, it just writes to the memory where the eighth element would be if the array was eight elements long.但是,它不会扩展数组的长度,它只是写入内存中,如果数组八个元素长,则第八个元素所在的位置。

In your code, it so happens that the memory where the eighth element would be is actually where the variable k is stored, which is why the value of k changes to 7 (I suspect that the variable n has been optimised away, otherwise it would have been n that got changed).在您的代码中,恰好第八个元素所在的内存实际上是存储变量k位置,这就是k的值更改为 7 的原因(我怀疑变量n已被优化掉,否则会已n是得到了改变)。

To make a long story short, handling arrays in C++ (and C) is fraught with danger and you have to be very careful.长话短说,在 C++(和 C)中处理数组充满危险,你必须非常小心。 In any current and/or production code, you should use a std::vector instead.在任何当前和/或生产代码中,您应该使用std::vector代替。

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

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