I’m trying to create an array of structs as a sort of rudimentary cache.
Given a void* pointer to a mmap, does mmap provide any affordances for indexing into it? I think conceptually a mmap is simply providing a block of memory, but then I’m a bit confused as to what I can do with it. Can I just think of it as a malloc?
void * mptr = mmap(NULL, 1024*1024, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0);
Thanks for any clarification here.
Yes, you can think of it as a malloc, but you must deallocate it with munmap(mptr,1024*1024) rather than free(mptr).
If you want to index into it, cast it to another type, for example char:
Then you can index into it using cptr[10], for example.