indexoutofboundsexception - Why does non-existant array position allow compiling -
indexoutofboundsexception - Why does non-existant array position allow compiling -
     #include <stdlib.h> #include <stdio.h> #include <ctime>      // time functions #include <math.h>     // pow() #include <iostream> using namespace std;  int main() {        int jiff=50;       int sizes[jiff]; //array of size jiff      sizes[49]=50;  //existing, should  lastly position     sizes[55]=4;    //should not exist      printf("it %d\n", sizes[49]);     printf("it %d",sizes[55]); }    
output:
it 50 4    why doesn't code crash? sizes[55] should out of bounds , should crash @ run-time. gives?
edit: nevermind, crashes now, or prints out obscurely big numbers. new question: why not misbehaving earlier? first 5 runs flawless array position till 60
accessing memory not allocated causes undefined behavior. sometimes, might print whatever happens sitting @ memory address , might give segmentation fault. see this question list of mutual undefined behaviors
 arrays indexoutofboundsexception 
 
  
Comments
Post a Comment