Skip to content

Instantly share code, notes, and snippets.

@ayourtch
Created January 1, 2011 21:56
Show Gist options
  • Save ayourtch/762039 to your computer and use it in GitHub Desktop.
Save ayourtch/762039 to your computer and use it in GitHub Desktop.
checking malloc
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
void checkzero(char *p, int size) {
while(--size) {
if(p[size]) {
printf("%p is non-zero at offset %d, the value is 0x%02hhx\n",
p, size, p[size]);
return;
}
}
printf("%p is zero in all %d bytes\n", p, size);
}
void do_round() {
char *ptr;
ptr = malloc(1024);
checkzero(ptr, 1024);
memset(ptr, 0xff, 1024);
free(ptr);
}
int main(int argc, char *argv[]) {
do_round();
do_round();
do_round();
}
-----
$ ./a.out
0x1ed3010 is zero in all 0 bytes
0x1ed3010 is non-zero at offset 1023, the value is 0xff
0x1ed3010 is non-zero at offset 1023, the value is 0xff
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment