c++ - Interesting behavior with dynamically allocated array -
c++ - Interesting behavior with dynamically allocated array -
so i'm working dynamically allocated array, , i've set hold 5 elements, should 0-4. made function reserve if necessary, wanted see if getting programme crash expected when assigned value array @ [5]. but, no error, not until [6].
here's code:
int* dynamic_arr; dynamic_arr = new int[5]; for(int = 0; <= 100; i++){ dynamic_arr[i] = i; used++; cout << dynamic_arr[i]<<endl; }
here's output:
0 //i[0] 1 //i[1] 2 //i[2] 3 //i[3] 4 //i[4] 5 //i[5] <-- should out of range
after crashes.
why able assign value portion of array is, well, out of range lack of improve term?
thanks
opsrcftw
edit: appreciate answers guys, thanks. i'll read more before posting quick reply next time, sorry.
accessing beyond end of array produces undefined behavior. not produce crash. can happen.
c++ arrays dynamic
Comments
Post a Comment