Electric Fence

Can you spot the error in the following code (other than the fact that it uses "new" directly)? What's wrong with new?

    const int SIZE = 16;
    int* ia = new int[SIZE];
    for (unsigned int i = 0; i <= SIZE; i++) {
        ia[i] = 1;
    }

Well, ElectricFence can!

ElectricFence, or "efence", is a nifty little link-in replacement for malloc() that has a nifty implementation allowing bugs like the above to be automatically caught at run-time.

The problem with bad pointers is that they probably point at data near where they should've pointed. If they point off into the blue, you'll get a core dump with or without efence. However, if they point nearer to your own data (such as one sizeof(int) beyond, as in the above example), the machine will probably keep running in a blissfully undefined state.

Available at your local apt-get mirror or http://www.perens.com/FreeSoftware/

Note: This only catches errors at run-time, which may be too late. If you've got some money to burn check out RationalPurify 'n' friends.


Or on Windows just call _heapchk at some regular interval to get exactly the same result. -- PhilippeDetournay


EditText of this page (last edited October 19, 2008) or FindPage with title or text search