关于指针转换的对齐问题 [英] alignment question about pointer conversion

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

问题描述

我想实现一个可以包含任何类型数据的公共列表,

所以我将列表声明为(简称)


--- ------------------------------------

struct list

{

int data_size;

int node_num;

char nodes []; //将是list_node1,list_node2 ......

};

struct list_node

{

int pre;

int next;

char data []; //将包含任何结构

};


---------------------- ---------------

整个列表缓冲区足够大的malloc。列表将

像列表一样管理数据。所有数据都有相同的类型。但是这个类型可以是

任何一个。


使用时,我记得不同的结构到list_node.data,并获得

数据由(strcut xxx *)list_node.data。


似乎list.nodes和list_node.data都有

对齐问题。


如何解决问题?

I want to implement a common list that can cantain any type of data,
so I declare the list as (briefly)

---------------------------------------
struct list
{
int data_size;
int node_num;
char nodes[]; //will be list_node1,list_node2...
};
struct list_node
{
int pre;
int next;
char data[]; //will contain any struct
};

-------------------------------------
The whole list buffer is large enough by malloc. The list will
manage datas like a list. All data has same type. But the type can be
any one.

When using, I memcpy different struct to list_node.data, and get
data by (strcut xxx *) list_node.data.

It seems that the list.nodes and list_node.data will both have
the problem of alignment.

How do I resolve the problem?

推荐答案

" haomiao" < mi ****** @ ustc.eduwrote:

#我想实现一个可以包含任何类型数据的通用列表,

#所以我声明列表为(简要)



#-------------------------- -------------

#struct list

#{

#int data_size;

#int node_num;

#char nodes []; //将是list_node1,list_node2 ......

#};





#struct list_node

#{

#int pre;

#int next;

#char data []; //将包含任何结构

#};



#--------------- ----------------------

#整个列表缓冲区足够大的malloc。列表将

#管理数据,如列表。所有数据都有相同的类型。但是类型可以是

#任何一个。



#使用时,我记得不同的结构到list_node.data,并获取

#data by(strcut xxx *)list_node.data。



#似乎list.nodes和list_node.data都是有

#对齐的问题。


如果你记忆进出,你可以避免对齐问题。


T值; struct list_node * p;

p = list_node_allocator(sizeof p + sizeof value);


memcpy(p-> data,& value,sizeof value) ;


memcpy(& value,p-> data,sizeof value);


-

SM Ryan http://www.rawbw.com/~wyrmwif/

谁领导这些暴民?
"haomiao" <mi******@ustc.eduwrote:
# I want to implement a common list that can cantain any type of data,
# so I declare the list as (briefly)
#
# ---------------------------------------
# struct list
# {
# int data_size;
# int node_num;
# char nodes[]; //will be list_node1,list_node2...
# };
#
#
# struct list_node
# {
# int pre;
# int next;
# char data[]; //will contain any struct
# };
#
# -------------------------------------
# The whole list buffer is large enough by malloc. The list will
# manage datas like a list. All data has same type. But the type can be
# any one.
#
# When using, I memcpy different struct to list_node.data, and get
# data by (strcut xxx *) list_node.data.
#
# It seems that the list.nodes and list_node.data will both have
# the problem of alignment.

If you memcpy in and out, you can avoid alignment issues.

T value; struct list_node *p;
p = list_node_allocator(sizeof p+sizeof value);

memcpy(p->data,&value,sizeof value);

memcpy(&value,p->data,sizeof value);

--
SM Ryan http://www.rawbw.com/~wyrmwif/
Who''s leading this mob?


haomiao< mi ****** @ ustc.eduwrote:
haomiao <mi******@ustc.eduwrote:

我想实现一个可以包含任何类型数据的公共列表,

所以我将列表声明为(简称)
I want to implement a common list that can cantain any type of data,
so I declare the list as (briefly)


------------------------------------ ---

结构列表

{

int data_size;

int node_num;

char nodes []; //将是list_node1,list_node2 ...
---------------------------------------
struct list
{
int data_size;
int node_num;
char nodes[]; //will be list_node1,list_node2...



这是一个指向节点或只是一个内存的指针数组

area因为所有节点都将被存储吗?


您是否明白这只能用C99完成? C89没有b $ b支持灵活的阵列成员。

Is this going to be an array of pointers to nodes or just a memory
area for were all nodes are going to be stored?

And do you understand that this can only done with C99? C89 didn''t
support "flexible array members".


};
};


struct list_node

{

int pre;

int next;
struct list_node
{
int pre;
int next;



通常,链接列表是使用指向

列表前一个和下一个元素的指针实现的...

Typically, linked lists are implemented using pointers to the
previous and next element of the list...


char data []; //将包含任何结构

};
char data[]; //will contain any struct
};


------------------------------ -------

整个列表缓冲区足够大的malloc。列表将

像列表一样管理数据。所有数据都有相同的类型。但任何一种类型都可以是

-------------------------------------
The whole list buffer is large enough by malloc. The list will
manage datas like a list. All data has same type. But the type can be
any one.



如果您要显示一些您正在使用的代码,这将有所帮助

完成所有这些...

It would help if you would show a bit of the code you''re using to
do all this...


使用时,我将不同的结构记忆到list_node.data,并通过(strcut xxx *)list_node.data得到

数据。
When using, I memcpy different struct to list_node.data, and get
data by (strcut xxx *) list_node.data.


似乎list.nodes和list_node.data都有

对齐问题。
It seems that the list.nodes and list_node.data will both have
the problem of alignment.



是的,当然。对于列表结构,如果你有一个指向节点的

指针数组,你可以通过使用一个灵活的结构列表指针数组而不仅仅是一个字符来避免这个问题。数组。

这应该确保没有对齐问题。

Yes, definitely. For the list structure, if you have an array of
pointers to nodes, you could avoid the problem by using a flexible
array of struct list_node pointers instead of just a char array.
That should make sure that there are no alignment problems.


如何解决问题?
How do I resolve the problem?



通过使用memcpy()将数据复制出结构到

一些正确对齐的内存或者使用方法你可以还有

与C89一起使用,即使用void指针代替灵活的

数组并分配内存然后将指针设置为。


问候,Jens

-

\ Jens Thoms Toerring ___ jt@toerring.de

\ __________________________ http://toerring.de


Jens Thoms Toerring写道:
Jens Thoms Toerring wrote:

haomiao< mi ****** @ ustc.eduwrote :
haomiao <mi******@ustc.eduwrote:

我想实现一个可以包含任何类型数据的公共列表,

所以我将列表声明为(简称)
I want to implement a common list that can cantain any type of data,
so I declare the list as (briefly)


------------------------- --------------

结构列表

{

int data_size;

int node_num;

char nodes []; //将是list_node1,list_node2 ...
---------------------------------------
struct list
{
int data_size;
int node_num;
char nodes[]; //will be list_node1,list_node2...



这是一个指向节点或只是一个内存的指针数组

区域因为所有节点都将被存储?


Is this going to be an array of pointers to nodes or just a memory
area for were all nodes are going to be stored?



所有节点都将被存储的存储区域。

A memory area for were all nodes are going to be stored.


>

您是否明白这只能用C99完成? C89没有b $ b支持灵活的阵列成员。
>
And do you understand that this can only done with C99? C89 didn''t
support "flexible array members".



是的,我使用的是C99。

Yes, I am using C99.


>
>

};
};


struct list_node

{

int pre;

int next;
struct list_node
{
int pre;
int next;



通常,链接列表是使用指向

列表上一个和下一个元素的指针实现的...


Typically, linked lists are implemented using pointers to the
previous and next element of the list...



我忘了,列表将在共享内存中,所以pre和next

是元素的相对地址。这里是list.nodes的下标



I forgot, the list will be in the share memory, so pre and next
be the relative address of element. Here they are the subscript
of the list.nodes.


>
>

char data []; //将包含任何结构

};
char data[]; //will contain any struct
};


------------------------------ -------

整个列表缓冲区足够大的malloc。列表将

像列表一样管理数据。所有数据都有相同的类型。但任何一种类型都可以是

-------------------------------------
The whole list buffer is large enough by malloc. The list will
manage datas like a list. All data has same type. But the type can be
any one.



如果您要显示一些您正在使用的代码,这将有所帮助

完成所有这些...


It would help if you would show a bit of the code you''re using to
do all this...



例如,如果结构为

struct s1 {...};

则列表内存为像这样,假设列表就像v0-> v1-> v2 ......


------------------- -

data_size // sizeof(struct s1)

node_num //假设是10

---------- ----------- node0

pre = -1 // -1表示没有预先

next = 2 // node2

struct s1 v0 //使用memcpy(node0-> data,& v0,sizeof(struct

s1)

------- -------------- node1

pre = 2 // node2

next = 3 // node3

struct s1 v2

--------------------- node2

pre = 0 // node0

next = 1 // node1

struct s1 v1

------------------- -

...


wh例如,使用获取第n个元素

--list.c --------------------------- -------

char * get_nth_data(struct list * list,int n){

char * addr;

struct list_node * node;

int node_size = sizeod(struct list_node)+ list-> data_size;

int node_index = 0;

for(int i = 0; i< n; i ++){

addr = list-> nodes + node_index * node_size;

node =(struct list_node *)addr; //有对齐问题

node_index = node-> next;

}

返回节点 - >数据;

}

--using.c --------------------------------- -

struct s1 * v =(struct s1 *)get_nth_data(list,n); //有对齐

问题

.....

---------------- ---------------------------


我希望这个列表框架可以用于任何结构比如s1列表,s2

列表,s3列表。


但是现在它似乎有一些对齐问题。

for example, if a struct as
struct s1{...};
then the list memory is like this, suppose list is like v0->v1->v2...

---------------------
data_size // sizeof(struct s1)
node_num // assume be 10
--------------------- node0
pre=-1 // -1 means no pre
next=2 // node2
struct s1 v0 // by using memcpy(node0->data,&v0,sizeof(struct
s1)
--------------------- node1
pre=2 // node2
next=3 // node3
struct s1 v2
--------------------- node2
pre=0 // node0
next=1 // node1
struct s1 v1
---------------------
...

when using, for example, to get nth element
--list.c----------------------------------
char * get_nth_data(struct list * list,int n){
char * addr;
struct list_node *node;
int node_size = sizeod(struct list_node)+list->data_size;
int node_index = 0;
for (int i=0;i<n;i++){
addr = list->nodes + node_index * node_size;
node = (struct list_node *)addr; // have alignment problem
node_index = node->next;
}
return node->data;
}
--using.c----------------------------------
struct s1 *v = (struct s1 *)get_nth_data(list,n); // have alignment
problem
.....
-------------------------------------------

I want this list frame can be used for any struct like s1 list,s2
list,s3 list.

But now it seems have some alignment problem.


>
>

使用时,我将不同的结构记忆到list_node.data,并通过(strcut xxx *)list_node.data获得

数据。
When using, I memcpy different struct to list_node.data, and get
data by (strcut xxx *) list_node.data.


似乎list.nodes和list_node.data都有

对齐问题。
It seems that the list.nodes and list_node.data will both have
the problem of alignment.



是的,当然。对于列表结构,如果你有一个指向节点的

指针数组,你可以通过使用一个灵活的结构列表指针数组而不仅仅是一个字符来避免这个问题。数组。

这应该确保没有对齐问题。


Yes, definitely. For the list structure, if you have an array of
pointers to nodes, you could avoid the problem by using a flexible
array of struct list_node pointers instead of just a char array.
That should make sure that there are no alignment problems.


如何解决问题?
How do I resolve the problem?



通过使用memcpy()将数据复制出结构到

一些正确对齐的内存或者使用方法你可以还有

与C89一起使用,即使用void指针代替灵活的

数组并分配内存,然后将该指针设置为。


Either by using memcpy() to copy the data out of the structure to
some correctly aligned memory or by using the method you can also
use with C89, i.e. using a void pointer instead of the flexible
array and allocating memory which you then set that pointer to.



为了提高效率,我想直接使用指针而不是memcpy。


在我的环境中,我得到了一个手上很大的shm。

在这个shm中,每个

子区域都有很多像s1 list,s2 list ...这样的列表。

那么,我如何修改我的数据结构以满足对齐

requiement?

For the efficiency,I want to directly use pointer than the memcpy.

And in my environment, I get a large shm before hand.
In this shm, there are many list like s1 list, s2 list ...,in each
subarea.

So, how can I modify my data structure to satisfy the alignment
requiement?


>

问候,Jens

-

\ Jens Thoms Toerring ___ jt @ toerring.de

\ __________________________ http://toerring.de


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

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