top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

Is it valid to address one element beyond the end of an array?

+5 votes
1,072 views

Can we increase the size during run time? What if, we exceed the size of array elements?

posted Dec 21, 2013 by Prachi Agarwal

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

2 Answers

0 votes

No for an array you can not go beyond the size of array as you may be entering into restricted area and may result in segmentation fault.

Only C99 allows to have the size of address at runtime, following code is perfectly valid with C99 but not with standard C

int main(int argc, char **argv)
{
    size_t size;
    cin >> size;
    int array[size];
    for(size_t i = 0; i < size; i++)
    {
        array[i] = i;
        cout << i << endl;
    }

    return 0;
}

If you want to increase the size of array at runtime using malloc/calloc (or new in case of C++) use the pointer instead (pointer can be used in a same way as array with the dynamic allocation).

answer Dec 21, 2013 by Luv Kumar
0 votes

Yes you can extend the size at run time, but if you will try to point an element outside of the array , it will show unexpected behavior and sometimes Index Outside error..

answer Jan 13, 2014 by Atul Mishra
Similar Questions
+4 votes

You have two arrays A1 and A2. Delete all element from A1 which are already in A2 and return new array.

+7 votes

Given an array of random numbers, Push all the zero’s of a given array to the end of the array.

+2 votes

1,1,2,2,2,6,6,6,7,7,7,7,7,7,7,8,8,9,9,9

Example:
Input = 1 Output=0 (First index of 1).
Input = 2 Output=2 (First index of 2).
Input = 6 Output= 5 (First index of 6).
Input = 7 Output= 8 (First index of 7).
Input = 8 Output=15 (First index of 8).
Input = 9 Output=17 (First index of 9).

+4 votes

Write a program to make AVL tree buy arranging element in array ?

...