C结构的大小排列 [英] C struct size alignment

查看:205
本文介绍了C结构的大小排列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望有一个C结构的大小为16的倍数字节(16B / 32B / 48B / ..)。 不要紧它到达它的大小,它仅需要多个16B的。 我怎么能强制编译器这样做呢? 谢谢你。

I want the size of a C struct to be multiple of 16 bytes (16B/32B/48B/..). It does not matter which size it gets to, it only needs to be multiple of 16B. How could I enforce the compiler to do that? Thanks.

推荐答案

C结构的大小将取决于结构,它们的类型的成员,其中有多少人有。实在是没有标准的方法来强制编译器,使结构出现一些大小的倍数。有些编译器提供了编译,将允许您设置然而对齐边界那真是一个不同的事情。并且可能有一些将有这样的设置,或提供这样的杂

The size of a C struct will depend on the members of the struct, their types and how many of them there are. There is really no standard way to force the compiler to make structs to be a multiple of some size. Some compilers provide a pragma that will allow you to set the alignment boundary however that is really a different thing. And there may be some that would have such a setting or provide such a pragma.

然而,如果你坚持这个方法是做结构的内存分配和强制内存分配围捕到下一个16字节大小。

However if you insist on this one method would be to do memory allocation of the struct and to force the memory allocation to round up to the next 16 byte size.

所以,如果你有这样的结构。

So if you had a struct like this.

struct _simpleStruct {
   int iValueA;
   int iValueB;
};

然后,你可以不喜欢以下。

Then you could do something like the following.

{
    struct _simpleStruct *pStruct = 0;
    pStruct = malloc ((sizeof(*pStruct)/16 + 1)*16);
    // use the pStruct for whatever
    free(pStruct);
}

这是什么会做的是推动规模到下一个16字节大小,只要你担心。然而什么是内存分配器确实可能会或可能不会给你一个块实际上是大小。该内存块实际上可能比你的要求更大。

What this would do is to push the size up to the next 16 byte size so far as you were concerned. However what the memory allocator does may or may not be to give you a block that is actually that size. The block of memory may actually be larger than your request.

如果您打算做一些特别的东西与此,比如让说,你会写这个结构到一个文件,你想知道块的大小,那么你必须做的malloc的使用相同的计算(),而不是通过sizeof()运算来计算结构的大小

If you are going to do something special with this, for instance lets say that you are going to write this struct to a file and you want to know the block size then you would have to do the same calculation used in the malloc() rather than using the sizeof() operator to calculate the size of the struct.

因此​​,接下来的事情将是使用宏,如编写自己的sizeof()操作符。

So the next thing would be to write your own sizeof() operator using a macro such as.

#define SIZEOF16(x) ((sizeof(x)/16 + 1) * 16)

据我所知,从一个指针拉动分配块的大小没有可靠的方法。通常的指针将具有所使用的存储器堆的管理功能,将含有各种存储器管理信息,如哪些实际上可能比所请求的内存量越大分配的块大小的存储器分配块。然而格式该块和它所在的位置相对于提供的服务将依赖于C编译器的运行时的实际内存地址。

As far as I know there is no dependable method for pulling the size of an allocated block from a pointer. Normally a pointer will have a memory allocation block that is used by the memory heap management functions that will contain various memory management information such as the allocated block size which may actually be larger than the requested amount of memory. However the format for this block and where it is located relative to the actual memory address provided will depend on the C compiler's run time.

这篇关于C结构的大小排列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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