I'm trying to use mmap for the first time to store a tree object with a lot of data in it. The tree class basically contains a pointer to the root of class Node, and each Node instance has an array of pointers to it's children. I think mmap is doing what it is supposed to, because I can access the tree's constant members, but when I try to access the pointer to the root I get a segfault.
Here is how a create a tree with a root node:
int main(int argc, char *argv[])
{
Tree *map;
...
map = (Tree*)mmap(0, FILESIZE, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
if (map == MAP_FAILED) {
close(fd);
perror("Error mmapping the file");
exit(EXIT_FAILURE);
}
Node* root = new Node("data");
map->set_root(root);
...
}
Here is how I access the Tree:
int main(int argc, char *argv[])
{
int i;
int fd;
Tree *map;
fd = open(FILEPATH, O_RDONLY);
if (fd == -1) {
perror("Error opening file for reading");
exit(EXIT_FAILURE);
}
map = (Tree*)mmap(0, FILESIZE, PROT_READ, MAP_SHARED, fd, 0);
if (map == MAP_FAILED) {
close(fd);
perror("Error mmapping the file");
exit(EXIT_FAILURE);
}
Node* root = map->root();
cout << root->data();
...
The output of root->data() provides a segfault. Can anyone give me a hint to where I'm wrong? Please say if I'm not making my problem clear.
Thanks in advance.
Mads