维持指针C中的数组指向两个相关类型 [英] Maintaining an array of pointers in C that points to two related types
问题描述
我有一个结构如下图
struct things
{
BOOL_T is_copy; /* is false */
handle1 h1;
handle2 h2;
int n;
void * memory;
};
有时我做的的事情
对象的副本在下面的结构
Sometimes I make a copy of objects of things
in below structure
struct copy_of_things
{
BOOL_T is_copy; /* is true */
handle1 h1; /* I don't need h2 and n here */
void * memory; /* copied from things object and
things object allocates new memory */
int another_member;
};
我也有结构的经理的数组保存所有的的事情
和 copy_of_things
结构住在我程序(称之为结构的东西* things_array [SIZE_OF_ARRAY];
)。我不能管理的,因为设计要求2个数组(数组是类似于哈希值)。为了实现这一目标,我发这个阵列的类型为的事*
,改变了 copy_of_things
的类型如下
Also I have an array of structure in a manager that keeps all the things
and copy_of_things
structure living in my program(call it struct things *things_array[SIZE_OF_ARRAY];
). I can not manage 2 arrays because of design requirements(Arrays are similar to hash). To enable this, I made the type of this array as thing *
and changed the type of copy_of_things
as below
struct copy_of_things
{
struct things ths;
int another_member;
};
现在我可以读我的数组元素的 is_copy
成员,并决定是否间preT为的事情
或 copy_of_things
。
Now I can read is_copy
member of my array elements and decide whether to interpret it as things
or copy_of_things
.
我觉得这不仅是低效
在内存方面,但丑
寻找。
I feel this is not only inefficient
in terms of memory but ugly
looking.
解决方案2
我还打算使用类型的数组是结构类型(is_copy)和工会
的
struct things {
BOOL_T is_copy;
union {
struct { /* is_copy = false */
handle1 h1;
handle2 h2;
int n;
void * memory;
} t;
struct { /* is_copy = true */
handle1 h1;
void * memory;
int another_member;
} c;
};
不过,在检讨,我发现这样的设计也难看。
But while reviewing I found this design also ugly.
解决方案3 我打算让 BOOL_T is_copy;
既是结构的第一个成员,并保持类型的数组 BOOL_T
。读 BOOL_T的内容之后
我可以去参考我的指针的东西或copy_of_things。我不知道如果这是一个很好的解决方案,并提供了一个良好定义的行为(在优化的更高层次)为同一地址PTED不同类型间$ P $。
Solution 3 I plan to keep BOOL_T is_copy;
as first member of both structure and keep array of type BOOL_T
. After reading the content of BOOL_T
I can de-reference my pointer to things or copy_of_things. I am not sure if this is a good solution and provides a well defined behaviour (at higher level of optimization) as same address is interpreted as different types.
问
是否有我的问题更好的解决方案是在任何平台上移植。
Question Is there a better solution for my problem that is portable on any platform.
修改
谢谢你的答案。所以,有两个建议的替代。
EDIT
Thank you for the answers. So there are two suggested alternatives.
- 使用工会:该方法的缺点是,它需要复制更多的内存。在我的情况下
的sizeof copy_of_things
比sizeof的事情
显著较小。一个解决办法是页头刚好足够的字节,其中实际对象可驻留。 - 使用一个共同的结构,使之既
copy_of_things的第一个成员
和的事情
。在这里,我最终会去引用相同的内存位置有2种类型(结构和通用结构物或结构copy_of_things)。我不知道,严格别名规则都不会咬我。 - 还有一个解决方案可以保留两个结构的第一个成员为
字符is_copy; / * \\ 0,如果不是副本,非零否则
并访问指针只能作为的char *
或的东西*
或copy_of_things *
。
- Use Unions: Downside of the approach is, it requires more memory for copies. In my case the
sizeof copy_of_things
is significantly smaller thansizeof things
. One workaround would be alloc just enough bytes in which actual object can reside. - Use a common struct and make it first member of both
copy_of_things
andthings
. Here I would end up de-referencing same memory location with 2 types (struct common and struct things or struct copy_of_things). I am not sure that strict aliasing rule won't bite me. - One more solution can be keep first member of both structs as
char is_copy; /* \0 if not a copy, non zero otherwise
and access the pointer only aschar *
orthings *
orcopy_of_things *
.
还是开放的问题是在很多地方使用
我见过的解决方案2。是的严格别名规则的安全吗?有没有更好的解决我的问题,因为code将在各种编译器编译。反向映射阵列的大小是大的,所以我避免使用联合或增加反向映射的大小的溶液。事情号(及复印件)较少,因此它是好的,以增加新的数据成员那里。
Still open question
I have seen solution 2 used at many places. Is it strict aliasing rule safe? Is there a better solution to my problems as the code would be compiled on a variety of compilers. Size of reverse mapping array is large so I am avoiding to use a union or a solution that increases the size of reverse mapping. Number of things (and copy) are less, so it is okay to add new data member there.
推荐答案
我有很多好的想法为我的问题。但似乎我无法非常有效地记录我的问题的所有细节。
I got many good ideas for my problem. But it seems I was unable to record all the details of my question very effectively.
下面是我想出了一个最终的解决方案:
Here is the final solution I came up with:
[1]编写包含的是通过反向映射访问的成员一个共同的结构。
struct common {
handle1 h1;
void * memory;
};
[2]现在拥有这个成员在这两个结构。
struct things
{
handle2 h2;
int n;
struct common cmn;
};
struct copy_of_things
{
BOOL_T is_copy; /* is true */
int another_member;
struct common cmn;
};
[3]更改反向映射到 结构体共同*
这增加了很轻微的非可读性我的code,但满足了我的所有其他要求。
This adds very slight non-readability to my code, but meets all other requirements for me.
这篇关于维持指针C中的数组指向两个相关类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!