I'm pretty sure my mistake is very evident, but I just can't seem to find where the problem is.
I'm learning how to use mmap()
in C, everything looks correct to me, but I get a segmentation fault.
Here is my code:
int n=50;
char * tab = mmap(NULL, n, PROT_READ | PROT_WRITE, MAP_SHARED, -1, 0);
for(int i=0; i<n; i++)
{
tab[i] = 1;
}
Using valgrind
, I get an error saying "Invalid write of size 1" at the line where I do tab[i]=1
, (I have tried replacing 1
by '1'
thinking that maybe a char has a smaller size than an int
, but still get the same error), followed by "Address 0xfffff..ff
is not stack'd, malloc'd, or (recently) free'd".
I have no idea where my mistake is. Can somebody help me find it?