Hacker News new | past | comments | ask | show | jobs | submit login
Memory locality (intersec.com)
4 points by fruneau on Feb 22, 2014 | hide | past | favorite | 1 comment



Not to detract from the interesting difference between pointers and unbounded arrays in a struct, but get_at() is surely broken. It looks like the condition uses the wrong inequality:

int get_at(const struct foo_t *foo, int pos) {    return foo->len >= pos ? -1 : foo->data[pos]; }

!(foo->len >= pos) = (pos >= foo->len) => foo->data[pos] = foo->data[foo->len + x] where x >= 0, which is undefined.

It seems the author got caught up trying to be tricky, and the fix has the bonus of reading more naturally:

return (pos < foo->len) ? foo->data[pos] : -1;

It is late though. Maybe my brain isn't in gear...




Join us for AI Startup School this June 16-17 in San Francisco!

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

Search: