简体   繁体   English

__attribute __((packed))替代

[英]__attribute__((packed)) alternative

consider the following structure: 考虑以下结构:

typedef struct __attribute__((packed)) a{
    int a1;
    int b2;
    char cArray[5];
    int c3;
} Mystruct;

Now in my code, Im doing this: 现在在我的代码中,我这样做:

char source[50];
Mystruct mm;
//...
//initialization and other codes
//...
memcpy(&mm,source,sizeof(mm));

I am trying to fill up the structure from a string (from a file to be more specific), and hence I don't want the padding. 我试图从字符串填充结构(从文件更具体),因此我不想填充。 But think packing affects the performance also. 但是认为包装也会影响性能。

So my question is, is there any other way to accomplish what I want? 所以我的问题是,还有其他方法可以实现我想要的吗?
Is it possible to do the same (filling up from a string) with member variables of a class in c++? 是否可以使用c ++中的类的成员变量执行相同的操作(从字符串填充)? If yes, how? 如果有,怎么样?

I'd highly recommend you purchase and read Write Portable Code . 我强烈建议您购买并阅读Write Portable Code You don't want to take this approach. 你不想采取这种方法。 Not only because it isn't portable (think endianess issues), but it does impact performance and atomicity (non-aligned 32-bit access is not atomic on x86 without a LOCK prefix). 不仅因为它不可移植(想想endianess问题),而且确实会影响性能和原子性(非对齐的32位访问在没有LOCK前缀的x86上不是原子的)。 It's much better to marshal/unmarshal‌ ​, even though it's a bit more work up front. 尽管前面的工作要多一点,但是编组/解组也要好得多。

The idea behind marshaling/unmarshaling is to convert your data from one format to another, and them back again. 编组/解组后面的想法是将数据从一种格式转换为另一种格式,然后再将它们转换回来。 This is what frameworks like Google Protocol Buffers , ZeroMQ , and many others do to transfer data in a portable way. 这就是Google Protocol BuffersZeroMQ等许多框架以便携方式传输数据的方式。 You basically have specialized functions that take your "string" data that you spoke of, and you parse it, validate it, and then assign it into your structure. 你基本上有专门的函数来获取你所说的“字符串”数据,然后解析它, 验证它,然后将它分配到你的结构中。 You'll see marshaling and serialization used interchangeably in many contexts. 您会看到编组和序列化在许多情况下可以互换使用。

For instance, in your case, you might have a function with the prototype: 例如,在您的情况下,您可能拥有原型的函数:

int
unmarshal_mystruct(const char *data, MyStruct &m);

Internally, you'd parse the data from data , and populate m . 在内部,您将从data解析data ,并填充m It does get tedious which is why so many people use the frameworks. 它确实变得乏味,这就是为什么这么多人使用框架的原因。

Use serialization/deserialization code. 使用序列化/反序列化代码。 Ie read into byte array, for example, and then convert values into structure. 例如,读入字节数组,然后将值转换为结构。 This is how many frameworks do (ie CORBA etc). 这是多少框架(即CORBA等)。

Drawbacks: more code to write. 缺点:要编写更多代码。

Benefits: better portability, compatibility between LE/BE platforms. 优点:更好的可移植性,LE / BE平台之间的兼容性。 For example, you code may not work on some ARM devices. 例如,您的代码可能无法在某些ARM设备上运行。

Example (struct): 示例(struct):

typedef struct a
{
    int a1;
    int b2;
    char cArray[5];
    int c3;
} Mystruct;

Writing: 写作:

void write_int(FILE *f, int value)
{
    int32_t tmp = htonl(value); // write in BE form
    fwrite(&tmp, 4, 1, f);
}

void a_write(Mystruct *d, FILE *f)
{
    write_int(f, d->a1);
    write_int(f, d->b2);
    fwrite(d->cArray, 5, 1, f);
    write_int(f, d->c3);
}

Reading: 读:

void read_int(FILE *f, int *value)
{
    int32_t tmp;
    fread(&tmp, 4, 1, f);
    *value = ntohl(tmp);
}

void a_read(Mystruct *d, FILE *f)
{
    read_int(f, &d->a1);
    read_int(f, &d->b2);
    fread(d->cArray, 5, 1, f);
    read_int(f, &d->c3);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 是否有__attribute __((packed))的C ++ 11/14替代方案 - Is there a C++11/14 alternative to __attribute__((packed)) portable __attribute __((__ package ___)) - portable __attribute__ ((__packed__)) __attribute __((packed))在只有1个元素的结构上 - __attribute__((packed)) on a struct with only 1 element __attribute __((packed))会影响程序的性能吗? - Can __attribute__((packed)) affect the performance of a program? __attribute__((__packed__)) 和 __attribute__((__packed__)) 有什么区别; 和#pragma pack(1) - what is the difference between __attribute__((__packed__)); and #pragma pack(1) Visual C ++相当于GCC的__attribute __((__ package ___)) - Visual C++ equivalent of GCC's __attribute__ ((__packed__)) __attribute __((__ package___))对嵌套结构的影响是什么? - What's the effect of __attribute__ ((__packed__)) on nested structs? 如何在 MySQL 数据库中使用 __attribute__((packed)) 存储 c++ 结构? - How to store c++ structures with __attribute__((packed)) in MySQL database? clang关于__attribute __((packed)),即使需要打包结构 - clang compaining about __attribute__((packed)) even though the struct needs to be packed 有关__attribute __((__ packed__))的c ++编译错误的其他方法是什么? - What are other methods for a c++ compile error about __attribute__((__packed__))?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM