c - 无法使用 mmap 从结构中读取指针

标签 c pointers struct malloc mmap

我正在尝试使用 mmap 在文件上存储图形,因此我的读写速度更快,但我无法读取使用 malloc 创建的字段结构字段(并且我无法使它们是一个数组)

问题是我无法从文件中读回提交的 map[i].nodes->vertexKey
(我认为是因为它是使用 malloc 创建的)

我的代码是:

#include <stdlib.h>
#include <stdio.h>
#include <stdint.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/mman.h>
#include <unistd.h>
#include <string.h>
#define COUNT 10
#define FILESIZE (  COUNT * sizeof(struct vertex))
struct node{
    int vertexKey ;
    struct node *nextNode;
};
struct vertex {
    int vertexKey;
    struct node *nodes;
};
int readMmap(){
    const char *filepath = "/tmp/mmapped.bin";
    int fd = open(filepath, O_RDWR  , (mode_t)0600);
    if (fd == -1)
    {
        perror("Error opening file for writing");
        exit(EXIT_FAILURE);
    }
    struct stat fileInfo = {0};    
    if (fstat(fd, &fileInfo) == -1)
    {
        perror("Error getting the file size");
        exit(EXIT_FAILURE);
    }
    if (fileInfo.st_size == 0)
    {
        fprintf(stderr, "Error: File is empty, nothing to do\n");
        exit(EXIT_FAILURE);
    }
    printf("File size is %ji\n", (intmax_t)fileInfo.st_size);
    struct vertex *map = mmap(0, FILESIZE , PROT_READ, MAP_SHARED, fd, 0);
    if (map == MAP_FAILED)
    {
        close(fd);
        perror("Error mmapping the file");
        exit(EXIT_FAILURE);
    }
    for (off_t i = 0; i < COUNT; i++)
    {
        printf("%d  |", map[i].vertexKey );
        // i can't read map[i].nodes->vertexKey
        printf("%d  \n", map[i].nodes->vertexKey );
        printf("\n" );
    }
    // Don't forget to free the mmapped memory
    if (munmap(map, fileInfo.st_size) == -1)
    {
        close(fd);
        perror("Error un-mmapping the file");
        exit(EXIT_FAILURE);
    }   
    // Un-mmaping doesn't close the file, so we still need to do that.
    close(fd);
    return 0;
}

int writeMmap(){
    const char *filepath = "/tmp/mmapped.bin";
    int fd = open(filepath, O_RDWR | O_CREAT | O_TRUNC, (mode_t)0600);
    if (fd == -1){
        perror("Error opening file for writing");
        exit(EXIT_FAILURE);
    }
    if (lseek(fd, FILESIZE-1, SEEK_SET) == -1){
        close(fd);
        perror("Error calling lseek() to 'stretch' the file");
        exit(EXIT_FAILURE);
    }
    if (write(fd, "", 1) == -1){
        close(fd);
        perror("Error writing last byte of the file");
        exit(EXIT_FAILURE);
    }
    struct vertex *map = 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);
    }
    for (size_t i = 0; i < COUNT; i++){
        struct vertex ss ;
        ss.vertexKey=i;
        struct node *n1 = (struct node*)malloc(sizeof(struct node));
        n1->nextNode =NULL ;
        n1->vertexKey=i*10 ;
        ss.nodes = n1 ;
        map[i] = ss;
    }
    // Write it now to disk
    if (msync(map, 100, MS_SYNC) == -1)
    {
        perror("Could not sync the file to disk");
    }
    // Don't forget to free the mmapped memory
    if (munmap(map, 100) == -1)
    {
        close(fd);
        perror("Error un-mmapping the file");
        exit(EXIT_FAILURE);
    }
    // Un-mmaping doesn't close the file, so we still need to do that.
    close(fd);
    return 0;
}

最佳答案

这到底需要多快?使用内存镜像作为持久格式是一种有问题的做法 - 您需要它在更大的方案中取得相当大的胜利,这样它才有值(value),如果它可能的话。

如果您想要数据的持久表示,那么该表示需要是独立的。指针本身不受支持,但您可以使用对象表(实际上是数组)的索引来代替它们。如果索引是隐式的那就更好了,但这对您来说可能还不够。对于含糊其词,我深表歉意,但在我提出任何具体细节之前,我需要更好地了解您的数据的特征。

关于c - 无法使用 mmap 从结构中读取指针,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33828616/

相关文章:

c - 如何使 stdin 上的 getc() 成为二进制文件

检查空指针在 C 中不起作用(给出段错误)

go - 以编程方式填充 golang 结构

c - 从类型 int 分配给类型 char[] 时类型不兼容

c - 在c中的结构内声明数组

c - 读取C函数

c - 从复合 C 结构中提取变量名和偏移量

c - 在 C 中设置 shell 的 PATH 和 HOME 环境变量

c - 使用 scanf 定义 malloc 数组大小并初始化

pointers - 将指针传递给 Golang 中的 map