关于malloc,对齐,...的问题 [英] Question about malloc, alignment, ...

查看:83
本文介绍了关于malloc,对齐,...的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用malloc分配一块内存。

我想用它来存储一个标题。结构

后跟我的应用程序中的结构。如何计算

对齐而不做任何假设

关于我机器上最严格的类型?

谢谢。

I am allocating a block of memory using malloc.
I want to use it to store a "header" structure
followed by structs in my application. How to calculate
the alignment without making any assumption
about the most restrictive type on my machine?
Thanks.

推荐答案

Bertrand Mollinier Toublet

< be ********************* **@enst-bretagne.fr>写在

< bi ************ @ ID-168218.news.uni-berlin.de>:
Bertrand Mollinier Toublet
<be***********************@enst-bretagne.fr> wrote in
<bi************@ID-168218.news.uni-berlin.de>:
Bhalchandra Thatte写道:
Bhalchandra Thatte wrote:
我使用malloc分配一块内存。
我想用它来存储一个标题。结构
后面是我的应用程序中的结构。如何计算对齐而不对我机器上最严格的类型做任何假设?
I am allocating a block of memory using malloc.
I want to use it to store a "header" structure
followed by structs in my application. How to calculate
the alignment without making any assumption
about the most restrictive type on my machine?



没有必要强迫malloc给你右"对齐。
它应该分配适当的内存块以适合任何类型。



There is no need to coerce malloc into giving you the "right" alignment.
It is supposed to allocate blocks of memory suitably aligned to fit any
type.




嗯,也许我完全错了,但是一次又一次地阅读OP的问题

我怀疑他想要计算他的数据结构的

补偿,在这种情况下''sizeof ''

运算符浮现在脑海中。很遗憾没有提供任何代码

由OP。

-

我的意见不是我前雇主的意见。 />



Hmm, maybe I''m totally wrong, but reading the OP''s question
again and again I suspect that he wants to calculate the
offsets of his data-structures, in which case the ''sizeof''
operator comes to mind. Too sad that no code was provided
by the OP.
--
My opinions are not those of my ex-employer.


Bhalchandra Thatte写道:
Bhalchandra Thatte wrote:
我正在使用malloc分配一块内存。
我想用它来存储标题结构
后面是我的应用程序中的结构。如何计算对齐而不对我机器上最严格的类型做出任何假设?
I am allocating a block of memory using malloc.
I want to use it to store a "header" structure
followed by structs in my application. How to calculate
the alignment without making any assumption
about the most restrictive type on my machine?




如果你必须这样做,我能想到的最简单的笨拙方式是保证工作的是b * b,这是一个标题和记录本身的联合,

并分配一堆那些。如果标题的大小和记录的大小大致相同,这并不是特别浪费。这个

方式,你根本不需要计算对齐方式。只需将

元素[0]视为标题,将所有其他元素视为记录。


-

Richard Heathfield : bi****@eton.powernet.co.uk

" Usenet是一个奇怪的地方。 - Dennis M Ritchie,1999年7月29日。

C FAQ: http://www.eskimo.com/~scs/C-faq/top.html

K& R答案,C书等:< a rel =nofollowhref =http://users.powernet.co.uk/etontarget =_ blank> http://users.powernet.co.uk/eton



If you must do it this way, the least clumsy way I can think of that is
guaranteed to work is to make a union of the header and the record itself,
and allocate a bunch of those. This isn''t particularly wasteful if the size
of the header and the size of the record are approximately the same. This
way, you don''t need to calculate the alignment at all. Just treat
element[0] as your header, and all the other elements as records.

--
Richard Heathfield : bi****@eton.powernet.co.uk
"Usenet is a strange place." - Dennis M Ritchie, 29 July 1999.
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
K&R answers, C books, etc: http://users.powernet.co.uk/eton


2003年8月31日08:13:10 -0700, bd ****** @ hotmail.com (Bhalchandra

Thatte)写道:
On 31 Aug 2003 08:13:10 -0700, bd******@hotmail.com (Bhalchandra
Thatte) wrote:
我正在使用malloc分配一块内存。
我想要用它来存储标题结构
后面是我的应用程序中的结构。如何计算对齐而不对我机器上最严格的类型做出任何假设?
谢谢。
I am allocating a block of memory using malloc.
I want to use it to store a "header" structure
followed by structs in my application. How to calculate
the alignment without making any assumption
about the most restrictive type on my machine?
Thanks.



malloc返回的值是保证适用于任何

对象,包括您描述的不同类型的结构。


使用符号_t表示结构类型和_ptr指向struct,我们

从头开始构建。记得在每次调用malloc之后添加错误检查。


仅对于标题,

header_ptr = malloc(sizeof * header_ptr);

将分配足够数量的正确对齐的内存来保存

一个标题结构。


对于一个标题和一个struct1,

header_ptr = malloc(sizeof * header_ptr + sizeof * struct1_ptr);

struct1_ptr =(struct1_t *)(header_ptr + 1);

将为一个头结构和另一个
结构分配足够的空间。但是我们不知道分配给struct1_ptr

的值是否与这个结构正确对齐。


一个可能的解决方案是

n = (sizeof * header_ptr)/(sizeof * struct1_ptr)+1;

header_ptr = malloc(sizeof * header_ptr +

2 *(sizeof * struct1_ptr));

struct1_ptr =(struct1_t *)header_ptr + n;

两个指针都保证正确对齐。


对于k struct1的数组,

n =(sizeof * header_ptr)/(sizeof * struct1_ptr)+1;

header_ptr = malloc(sizeof * header_ptr +

(k +1)* sizeof * struct1_ptr);

struct1_ptr =(struct1_t *)header_ptr + n;

将分配足够的空间并对齐两个指针。


对于两种不同类型的应用程序结构,

n1 =(sizeof * header_ptr)/(sizeof * struct1_ptr)+1;

n2 =(( n1 + 1)*(sizeof * struct1_ptr))/(sizeof * struct2_ptr)+1;

header_ptr = malloc(sizeof * header_ptr +

2 *(大小) of * struct1_ptr)+

2 *(sizeof * struct2_ptr));

struct1_ptr =(struct1_t *)header_ptr + n1;

struct2_ptr = (struct2_t *)header_ptr + n2;


您可以推断出更多种类和数量的结构。


然而,这似乎是一个维护噩梦。你可以通过单独的分配更好地获得
,而不是试图强迫

一个块来保存不同的类型。类似

header_ptr = malloc(sizeof * header_ptr);

struct1_ptr = malloc(k1 * sizeof * struct1_ptr);

struct2_ptr = malloc( k2 * sizeof * struct2_ptr);

<<删除电子邮件的del>>


The value returned by malloc is guaranteed to be suitable for any
object, including the different types of struct you describe.

Using notation _t for struct types and _ptr pointers to struct, we
start from scratch and build up. Remember to add error checking after
each call to malloc.

For just the header,
header_ptr = malloc(sizeof *header_ptr);
will allocate a sufficient quantity of properly aligned memory to hold
one header structure.

For one header and one struct1,
header_ptr = malloc(sizeof *header_ptr + sizeof *struct1_ptr);
struct1_ptr = (struct1_t*)(header_ptr+1);
will allocate enough space for one header structure and one other
structure. But we have no idea if the value assigned to struct1_ptr
is properly aligned this structure.

One possible solution is
n = (sizeof *header_ptr)/(sizeof *struct1_ptr)+1;
header_ptr = malloc(sizeof *header_ptr +
2 * (sizeof *struct1_ptr));
struct1_ptr = (struct1_t*)header_ptr+n;
Both pointers are guaranteed to be aligned properly.

For an array of k struct1,
n = (sizeof *header_ptr)/(sizeof *struct1_ptr)+1;
header_ptr = malloc(sizeof *header_ptr +
(k+1)*sizeof *struct1_ptr);
struct1_ptr = (struct1_t*)header_ptr+n;
will allocate enough space and align both pointers.

For two different types of application structures,
n1 = (sizeof *header_ptr)/(sizeof *struct1_ptr)+1;
n2 = ((n1+1)*(sizeof *struct1_ptr))/(sizeof *struct2_ptr)+1;
header_ptr = malloc(sizeof *header_ptr +
2 * (sizeof *struct1_ptr) +
2 * (sizeof *struct2_ptr));
struct1_ptr = (struct1_t*)header_ptr + n1;
struct2_ptr = (struct2_t*)header_ptr + n2;

You can extrapolate for more variety and quantity of structures .

However, this appears to be a maintenance nightmare. You would be
much better off with separate allocations instead of trying to force
one block to hold disparate types. Something like
header_ptr = malloc(sizeof *header_ptr);
struct1_ptr = malloc(k1 * sizeof *struct1_ptr);
struct2_ptr = malloc(k2 * sizeof *struct2_ptr);
<<Remove the del for email>>


这篇关于关于malloc,对齐,...的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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