Ancient CS 61 Content Warning!!!!!1!!!
This is not the current version of the class.
This site was automatically translated from a wiki. The translation may have introduced mistakes (and the content might have been wrong to begin with).

9/8/16: Fundamentals 3: Undefined Behavior and Arena Allocation

Undefined behavior

In lecture, we saw a slightly contrived example of undefined behavior causing a problem. Here are some (slightly tweaked) examples.

Defined

static int array[10] = { ... };
int f1(int index) {
    if (index < 0 || index >= 10)
        abort();
    int x = array[index];
    assert(index >= 0 && index < 10);
    return x;
}

Sometimes-undefined

static int array[10] = { ... };
int f1(int index) {
    if (index < 0 || index >= 11)
        abort();
    int x = array[index];
    assert(index >= 0 && index < 10);
    return x;
}