0

When this code was executed, the library dmalloc somehow determined that there was an out of bounds memory access. As it allocated 1023 elements and attempted to access 1024th element. (Array index is 0-based).

#include "dmalloc.h"
int main(){

    char *ch = malloc(1023);
    ch[1023] = 0x00;
    return 0;
}

How can it know?

TheCodeArtist
  • 21,479
  • 4
  • 69
  • 130
drlexa
  • 131
  • 1
  • 2
  • 5
  • 1
    This is covered in the dmalloc documentation that is on line at http://dmalloc.com/docs/latest/online/dmalloc_7.html#SEC9 with additional information at http://dmalloc.com/docs/latest/online/dmalloc_17.html. – Richard Chambers Sep 06 '12 at 12:35

2 Answers2

1

When using the dmalloc library, it actually allocates more than you request. It keeps one area before and one after the memory it returns to you. These areas are filled with special values that are then checked when you free the memory. If those values are not correct, then you clearly have modified memory out of bounds.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
0

The easiest way is to use sentinels, which are simply blocks of memory that is filled-in with a known pattern by dmalloc. It can then check if that pattern has been destroyed, and flag an error.

unwind
  • 391,730
  • 64
  • 469
  • 606