-3

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?
  2. how value of variable k become 7?
  3. why size of old and new array is same though new array has 1 more element?

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 ;

   }
πάντα ῥεῖ
  • 1
  • 13
  • 116
  • 190
ashasa892
  • 3
  • 3
  • 1
    1) Why didn't you try stepping through your code, line-by-line, with a debugger? 2) `arr[j]`, when `j == n`is undefined behavior (due to indexing out of bounds, of the array). hence, figuring out why some particular behavior is observed, is pointless. 3) Consider learning from a [good C++ book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). – Algirdas Preidžius Jun 20 '19 at 15:40
  • 1
    Use `std::vector` if you intend changing the array size. – πάντα ῥεῖ Jun 20 '19 at 15:41
  • Unrelated: If your compiler is up to date you may be able to use `std::size` rather than `sizeof(arr)/sizeof(arr[0])` to determine the size of an array. – user4581301 Jun 20 '19 at 16:09

2 Answers2

2

Classic C Memory Question!

When you declare arr as an array it has a fixed length ... thus a fixed amount of space in memory. 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.

This explains why k is being set to 7. k must be stored adjacent to the end of arr in memory. Thus, when you shift arr[n-1] to arr[n], k is being set to 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.

c.abate
  • 402
  • 2
  • 11
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.

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

To make a long story short, handling arrays in C++ (and C) is fraught with danger and you have to be very careful. In any current and/or production code, you should use a std::vector instead.

Chris Long
  • 1,299
  • 7
  • 15