使用动态元素处理struct padding [英] Dealing with struct padding using a dynamic element

查看:86
本文介绍了使用动态元素处理struct padding的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题:


我有一个需要将数据存储在连续内存中的结构

有一个动态元素是不可能的在编译时定义。

它需要沿4字节边界对齐。这是我的

结构的样子:


START


struct tsBob

{

unsigned int fieldA;

unsigned int fieldB;


unsigned short varLen [0];

}


tsBob * myBob;


myBob =(tsBob *)malloc(sizeof(tsBob)+ sizeof(unsigned short) * 1);


结束


myBob需要额外的3个字节alloc''d它才能对齐。我不想使用%


最后一个问题,


我可以使用按位用什么来确定我需要多少额外字节

和malloc用

其余的函数调用?

Problem:

I have a structure which needs to store its data in contiguous memory
by there is a dynamic element which can''t be defined at compile time.
It needs to be aligned along a 4 byte boundary. This is what my
structure looks like:

START

struct tsBob
{
unsigned int fieldA;
unsigned int fieldB;

unsigned short varLen[0];
}

tsBob* myBob;

myBob = (tsBob*)malloc(sizeof(tsBob) + sizeof(unsigned short) * 1);

END

myBob needs an additional 3 bytes alloc''d to it to be aligned. I
don''t want to use %

Finally the question,

Can I use a bitwise something to determine how many extra bytes I need
and malloc that with the
rest of the function call?

推荐答案

mojumbo写道:
mojumbo wrote:

问题:


我有一个需要存储其数据的结构在连续的内存中

有一个动态元素,在编译时无法定义。

需要沿4字节边界对齐。这是我的

结构的样子:


START


struct tsBob

{

unsigned int fieldA;

unsigned int fieldB;


unsigned short varLen [0];
Problem:

I have a structure which needs to store its data in contiguous memory
by there is a dynamic element which can''t be defined at compile time.
It needs to be aligned along a 4 byte boundary. This is what my
structure looks like:

START

struct tsBob
{
unsigned int fieldA;
unsigned int fieldB;

unsigned short varLen[0];



我不确定这是否合适,你可以考虑给它至少1个元素。

I am not sure this is OK, you might consider giving it at least 1 element.


}
}



;

;


>

tsBob * myBob;


myBob =(tsBob *)malloc(sizeof(tsBob)+ sizeof(unsigned short)* 1);
>
tsBob* myBob;

myBob = (tsBob*)malloc(sizeof(tsBob) + sizeof(unsigned short) * 1);



这是通过在类中正确实现的''operator new''

来实现的,然后你就可以了/>

tsBob * myBob = new(true_varLen_count)myBob;

This is better accomplished by the correctly implemented ''operator new''
in the class itself, and then you just do

tsBob* myBob = new (true_varLen_count) myBob;


>

END


myBob需要额外的3个字节alloc''d它才能对齐。我

不想使用%
>
END

myBob needs an additional 3 bytes alloc''d to it to be aligned. I
don''t want to use %



如果以字节为单位的大小可以被4整除,那么它将会是在

4字节边界上对齐。这是''malloc''的规范,IIRC。

If the size in bytes is divisible by 4, it''s going to be aligned on the
4 byte boundary. That''s the specification of ''malloc'', IIRC.


>

最后一个问题,


我可以使用一个按位来确定我需要多少额外字节

和malloc与

函数调用的其余部分?
>
Finally the question,

Can I use a bitwise something to determine how many extra bytes I need
and malloc that with the
rest of the function call?



" Bitwise something"?不明白你的意思。如果你知道你需要给你的'varLen'数组提供多少元b / b元素,那就做你做的,

但不要倍增'' sizeof(unsigned short)''乘以1,乘以

所需的元素数量......


V

- -

请在通过电子邮件回复时删除资金''A'

我没有回复最热门的回复,请不要问

"Bitwise something"? Not sure what you mean. If you know how many
elements you need to give to your ''varLen'' array, just do what you did,
but don''t multiply the ''sizeof(unsigned short)'' by 1, multiply by the
required number of elements...

V
--
Please remove capital ''A''s when replying by e-mail
I do not respond to top-posted replies, please don''t ask


2008-10-03 16:47,Victor Bazarov写道:
On 2008-10-03 16:47, Victor Bazarov wrote:

mojumbo写道:
mojumbo wrote:

>问题:

我有一个需要将其数据存储在连续内存中的结构
有一个动态元素可以'在编译时定义。
需要沿4字节边界对齐。这就是我的
结构:
>Problem:

I have a structure which needs to store its data in contiguous memory
by there is a dynamic element which can''t be defined at compile time.
It needs to be aligned along a 4 byte boundary. This is what my
structure looks like:



除非你可以使用某种4字节的结构/类型(并且make

确保数组中的第一个元素使用

padding正确对齐而不是unsigned short,你需要注意

分配记忆。也许使用工厂函数(分配

正确的字节数)将是一个好主意。

Unless you can use some kind of struct/type which is 4 bytes (and make
sure that the first element in the array is correctly aligned using
padding) instead of the unsigned shorts you will have to take care when
allocating the memory. Perhaps using a factory-function (which allocates
the correct number of bytes) would be a good idea.


> struct tsBob
{unsigned int fieldA;
unsigned int fieldB;

unsigned short varLen [0];
>struct tsBob
{
unsigned int fieldA;
unsigned int fieldB;

unsigned short varLen[0];



我不确定这是否合适,你可以考虑给它至少1个元素。


I am not sure this is OK, you might consider giving it at least 1 element.



这不行,标准需要一个大于零的数字,但是一些

编译器(一个gcc)接受这个。当我们通过网络发送消息时,我们会在工作中大量使用它,并且我发现它有助于

将零大小的数组作为一种描述方式消息布局。它通常看起来像这样:


struct Msg

{

unsigned nrFoo;

unsigned nrBar;

#if 0

Foo foos [0];

条形条[0];

#endif

};


-

Erik Wikstr ?? m

It''s not OK, the standard requires a number greater than zero, but some
compilers (gcc for one) accepts this. We use this a lot at work when
building messages to be sent over the network, and I find it helpful to
have the zero-sized array as a way of describing the message layout. It
usually looks something like this:

struct Msg
{
unsigned nrFoo;
unsigned nrBar;
#if 0
Foo foos[0];
Bar bars[0];
#endif
};

--
Erik Wikstr??m


感谢上面的评论,但我会澄清:


我在结构中使用了一个重载运算符new

就像你建议的元素数量一样,所以

结构的实际规格确实如此。


struct tsBob

{

unsigned int fieldA;

unsigned int fieldB;


unsigned short varLen [0];


void * operator new(size_t,int aNum)

{return malloc(sizeof(tsBob)+ sizeof(unsigned short)* num;}

};


这正是我想要解决的问题 - aNum可以进入任何

数字(这就是为什么我在我的第一个解释中乘以1)


" bitwise something"意思是 -

我还有另一个浮动aFlts [0]的结构。

我的新看起来像这样:void * operator new(size_t,int aNum){return

sizeof(tsFltStr)+ sizeof(float)* aNum +(aNum& 0x01)? 1:0); }


这很好用32位,只需添加另一个。

另外,我认为零长度数组是C99标准的一部分?
Thanks for the above comments but I will clarify:

I am using an overloaded operator new in the structure which takes the
number of elements just as you suggested, so the actual spec for the
struct does look like this.

struct tsBob
{
unsigned int fieldA;
unsigned int fieldB;

unsigned short varLen[0];

void* operator new(size_t, int aNum)
{ return malloc(sizeof(tsBob) + sizeof(unsigned short)*num; }
};

This is exactly what I''m trying to solve - aNum can come in as any
number (which is why I multiplied by one in my first explanation)

"bitwise something" meaning -
I also have another structure with float aFlts[0].
My new looks like this: void* operator new(size_t, int aNum) { return
sizeof(tsFltStr) + sizeof(float)* aNum + (aNum & 0x01) ? 1:0); }

It''s nice with 32-bits, just add another.
Also, I thought zero length arrays were part of the C99 standard?


这篇关于使用动态元素处理struct padding的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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