Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

Not sure which is the first dimension but for an array of 10 if(arr[100]) returns false

This is taking the first pointer and offsetting it by the length of 100 pointers. There are only 10 pointers in the array so it is going into other data.

If your pointers are 64 bit and your numbers are 32 bit, even if that is referencing the memory mapped data of the sub arrays it is going twice as far as the end of the array and returning 0 purely by chance of some other memory you're dealing with by accident.

A better way would be to keep things simple and bounds check that x is less than width and y is less than height while using the single buffer method.

The point here isn't to be too harsh with criticism, it's just that it's easy to see something as more trivial than it really is.



Thats fun too.

    function foo(x,y,num){
      for(i=y; i<y+3 && i<10; i++){
        for(w=x; w<x+3 && w<10; w++){
          if(grid[i*10+w] == num) return true
        }
      }
      return false;
    }
https://jsfiddle.net/gaby_de_wilde/ycx159kg/1/




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: