c - How could I Segfault without invalid writes? -


i trying implemention of own malloc , im trying understand how memory works.

i writed don't understand why have segmention fault here :

#include <unistd.h> #include <stdio.h>  int main() {   int i;   char *mem;   void *maxsize;    = 0;   maxsize = sbrk(0);   printf("%p\n", maxsize);   mem = maxsize - 500;   printf("%p\n", mem);   while (i != 100)   {     mem[i] = 1;     i++;     printf("%p\n", &mem[i]);   } } 

when test code valgrind works fine , don't have errors. when running code segfault in first loop.

have idea why ? , know how first free address in process ?

what size of "unitilized data segment"? guess it's pretty small, writing before start of data segment. coud try increase first check:

 ...  sbrk(1000);  ^^^^^^^^^^^  = 0;  maxsize = sbrk(0);  ... 

Comments