I'm trying to use the sbrk
system call to ask for one memory page and divide that page into small blocks, but my code always hits some invalid memory:
void sbrkBlocks() {
int *b = sbrk(0);
if(sbrk(sysconf(_SC_PAGESIZE)) == (void *)-1) {
printf("sbrk failed\n");
return NULL;
}
void *bound =b + sysconf(_SC_PAGESIZE);
while (b + 16 <=bound) {
*b = 1;
b+= 16;
}
}
Like if I get sbrk(0)
at 0x804d000
, the bound after sbrk(one_page_size)
will be 0x8055000
, but the code will get a segmentation fault at 0x804e000
.