Created
January 1, 2011 21:56
-
-
Save ayourtch/762039 to your computer and use it in GitHub Desktop.
checking malloc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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