BSW example

#include <iostream.h>

#include <windows.h>

 

#define A_LOT 256

int *

setup_values(void)

{

    int values[A_LOT];    //Bug: This should be 'static', not automatic

    for (int i=0; i < A_LOT; i++) {

        values[i] = i;

    }

    return(values);       //Bug: returning a value which vanishes as we return

}

int

main(int, char **)

{

    int *values;

    values = setup_values();

    for (int i=0; i < A_LOT; i++) {

        //BSR + BSW: updating values which are no longer alive on the stack

        values[i] *= i;

        cerr << "element #" << i << " is " << values[i] << '\n';

    }

    return(0);

}

(C) Copyright IBM Corporation 1992, 2010.