I am trying to do an exercise that is done with system calls, and you need to allocate memory for struct *. My code is:
myStruct * entry = (myStruct *)mmap(0, SIZEOF(myStruct), PROT_READ|PROT_WRITE, MAP_ANONYMOUS, -1, 0);
To clarify, I cannot use malloc()
, but I can use mmap()
. I had no problems with this on Windows in Netbeans, now, although I compile and run from the command line in Ubuntu, I get a “Segmentation Error” every time I try to access it.
Is there a reason why it will work on one and not on the other, and is mmap()
valid way to allocate memory this way? My concern was that I was going to allocate large chunks of memory for each mmap()
call initially, now I just can't get it to work.
Also, the error returned by my mmap is 22 - Invalid Argument (I did some troubleshooting when writing the question to check for the error in the above code). The address is 0, the custom function SIZEOF()
works in other mmap arguments, I use MAP_ANONYMOUS
, so the fd
and offset
parameters should be -1 and 0, respectively.
Is there something wrong with the PROT_READ|PROT_WRITE
?
source share