+ 3

Regarding array In c++

I made a code in which I had to take the size of an array as users input and it's elements too and print them suppose in the following The value of i is 3(according to users input) In the first loop the condition for j is upto <=i where i is the size of array(this shouldn't happen as i begins from 0) due to which the Compiler asks me to input 4 values for the array(n[0],n[1],n[2] & n[3]) but the size of the array is 3 only how can it store 4 objects plz expl https://code.sololearn.com/cIASxKqZ39O9/?ref=app

26th Nov 2017, 5:08 PM
Saurabh Tiwari
Saurabh Tiwari - avatar
7 odpowiedzi
+ 1
https://stackoverflow.com/a/47550120/8640354 hmmmm....
29th Nov 2017, 10:58 AM
Saurabh Tiwari
Saurabh Tiwari - avatar
+ 3
no no... my question is why it isnt storing upto it's fixed size I mean why the Compiler isn't showing some kind of error
26th Nov 2017, 6:43 PM
Saurabh Tiwari
Saurabh Tiwari - avatar
+ 2
C++ counts array elements from 0 on to n-1 , where n is the size/ length of the array. Ex, i=5 should imply there are 5 elements indexed: 0,1,2,3&4. That is last element index is less than array size... Since you are seeking user input (i) to decide the size of the array, just changing your for loops' condition from '<=i' to '<i' should correct the problem.
26th Nov 2017, 6:40 PM
Anil
Anil - avatar
+ 2
the answer:: Undefined Behavior! Usually, when you encounter undefined behavior, the code/ program may crash, freeze, write to unintended memory OR IN WORSE CASE APPEAR to work correctly. C++ simply specifies what should happen if you access the elements within the bounds of an array. What happens if you go out of bounds is left undefined. It might seem to work today but there is no guarantee that it'll still work the next time. The following may be the reason of C++'s lack of a bound check on array:- 1. Arrays are leftovers from C which were dirt primitive and lacked bounds checking. That aspect was left to the programmer. C++ = (C) ++, right? 2. In C++, bounds-checking is possible on class types. But an array is still the plain old C-compatible one. 3. If you treat your collection of data (uou may read: arrays) sacrosanct, C++ offers the std::vector class template, which has the at() member function which ensures bounds-checking. 4. C offerred faster arrays due to the lack of bounds-checking. So when someone markets C++ as faster and advanced version of C, why should they pile on code/ CPU cycles to implement array bounds-checking thereby making it slower? And yeah, your code evoked an instance of undefined behavior!! Congrats.
26th Nov 2017, 7:08 PM
Anil
Anil - avatar
+ 2
still confused!!!!!
26th Nov 2017, 7:12 PM
Saurabh Tiwari
Saurabh Tiwari - avatar
+ 2
Well, for now, it may suffice to accept that C/ C++ compilers are designed to win the battle over execution time by ensuring efficiency. But this calls for compromises, especially in safety aspect of the program/ execution environment by leaving some unnecessary checks at the disposal of the programmer. This is the main difference between C/C++ and Java. But, as always, speed thrills but kills !
26th Nov 2017, 7:22 PM
Anil
Anil - avatar
+ 1
hahaha😂😂😂😂 great explanation
26th Nov 2017, 7:42 PM
Saurabh Tiwari
Saurabh Tiwari - avatar