struct padding对C struct序列化的影响(保存到文件) [英] struct padding influence in C struct serialization ( saving to file )

查看:133
本文介绍了struct padding对C struct序列化的影响(保存到文件)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在C语言中具有以下结构:

I have the following structs in C:

typedef struct sUser {
    char name[nameSize];
    char nickname[nicknameSize];
    char mail[mailSize];
    char address[addressSize];
    char password[passwordSize];
    int totalPoints;
    PlacesHistory history;
    DynamicArray requests;
}User;

typedef struct sPlacesHistory {
    HistoryElement array[HistorySize];
    int occupied;
    int last;
}PlacesHistory;

和功能:

void serializeUser( User * user, FILE * fp ) {
    fwrite( user, nameSize + nicknameSize + mailSize + addressSize + passwordSize + sizeof( int ) + sizeof( PlacesHistory ), 1, fp );
    serializeDynamicArray( user -> requests, fp );
}

User * loadUser( FILE * fp ) {
    User * user = malloc( sizeof( User ) );
    fread( user, nameSize + nicknameSize + mailSize + addressSize + passwordSize + sizeof( int ) + sizeof( PlacesHistory ), 1, fp );
    user -> requests = loadDynamicArray( fp );

    return user;
}

当我加载结构User并打印该用户(从文件加载)时,placesHistory的字段"last"的值为255或-1,具体取决于PlacesHistory结构的字段的顺序.但是我保存的用户的成员为-1.

When I load the struct User, and I print that user (loaded from file), the field "last" of placesHistory has the value of 255 or -1, depending on the order of the fields of the PlacesHistory structure. But The User I saved had -1 on that member.

所以当我得到255时,这显然是错误的. 我怀疑这与结构填充有关.

So when i get 255, it is obviously wrong.. I suspect this has to do about struct padding.

如何以这样的方式执行此操作,使得结构中的字段顺序无关紧要?
还是我需要遵循哪些标准才能使事情正常进行?
我是否需要一次重写/读取一个成员? (出于效率考虑,我希望避免这种情况)
我需要先序列化到数组而不是文件吗? (我希望不是..,因为这意味着由于数组位置不正确,这意味着要事先知道我所有结构的大小,这意味着要为每个不简单的结构创建一个函数来知道它的大小,需要进行额外的工作)

How can I do this in such a way that the order of fields in the structure doesn't matter?
Or which criteria do I need to follow to make things work right?
Do I need to fwrite/fread one member at a time? ( I would like to avoid this for efficiency matters )
Do I need to serialize to an array first instead of a file? (I hope not .. because this implicates to know the size of all my structures beforehand because of the mallocated array- which means extra work creating a function for every non simple structure to know it's size)

注意:*大小是定义的常量
注意2:DynamicArray是指向另一个结构的指针.

Note: *Size are defined constants
Note2: DynamicArray is a pointer to another structure.

推荐答案

是的,它可能与totalPointshistory前面的填充有关.

Yes, it probably has to do with padding in front of either totalPoints or history.

您可以只写出sizeof(User) - sizeof(DynamicArray)并以相同的方式读回.当然,这仅在您的结构定义和编译器不变的情况下才兼容.如果您不需要程序的一个版本的序列化数据与另一版本兼容,则上述方法应该可以工作.

You can just write out sizeof(User) - sizeof(DynamicArray) and read back in the same. Of course this will only be compatible as long as your struct definitions and compiler don't change. If you don't need serialized data from one version of your program to be compatible with another version, then the above should work.

这篇关于struct padding对C struct序列化的影响(保存到文件)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆