演员结构问题 [英] cast struct question

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

问题描述

大家好,


这个问题与界面设计有关。假设我有一个翻译

工具。它可以转换类型为general的结构。其他类型然后

做一些处理。


示例:


typedef struct

{

int data1;

int data2;

} general;


typedef struct

{

int data3;

} specific_a;


例如,翻译看起来像这样:


void translate1(一般*输入)

{

specific_a * new_specific_a = malloc(..... );

new_specific_a-> data3 = input-> data1 * input-> data2;


/ *在这里使用new_specific_a一段时间* /

}


假设有另一种类型:

typedef struct

{

int data4;

int data5;

} specific_b;


在这种特殊情况下,另一个翻译如下: br />

void translate2(一般*输入)

{

specific_b * new_specific_b = malloc(....);

new_specific_b-> data4 = input-> data1;

new_specific_b-> data5 = input-> data2;


/ *使用new_specific_b进行一些处理* /


}


因为其他接口是固定的,我需要输入specific_a和specific_a。在

" translate1"方法和specific_b在translate2中。


我在translate2方法中看到了一个优化,即:

void translate2(一般*输入)

{

specific_b * new_specific_b =(specific_b *)输入;

}


(注意:我使用的是结构specific_b和specific_a仅用于阅读)。


我不想生成新消息,而是要转换结构(因为在

这个特定情况下的数据项完全匹配)。这是允许的吗?在其他

字中,返回调用new_specific_b-> data4返回data1和

new_specific_b-> data5返回data2或者是否存在一些编译器问题

请记住?


Grz Milux

解决方案

Milux写道在06/11/07 13:01,:


大家好,


这个问题与界面设计有关。假设我有一个翻译

工具。它可以转换类型为general的结构。其他类型然后

做一些处理。


示例:


typedef struct

{

int data1;

int data2;

} general;


typedef struct

{

int data3;

} specific_a;


例如,翻译看起来像这样:


void translate1(一般*输入)

{

specific_a * new_specific_a = malloc(..... );

new_specific_a-> data3 = input-> data1 * input-> data2;


/ *在这里使用new_specific_a一段时间* /


}


假设有另一种类型:

typedef struct

{

int data4;

int data5;

} specific_b;


在这个特定情况下另一个翻译看起来像:


void translate2(一般*输入)

{

specific_b * new_specific_b = malloc(....) ;

new_specific_b-> data4 = input-> data1;

new_specific_b-> data5 = input-> data2;


/ *使用new_specific_b进行一些处理* /


}


因为其他接口是固定的,我需要输入specific_a;在

" translate1"方法和specific_b在translate2中。


我在translate2方法中看到了一个优化,即:

void translate2(一般*输入)

{

specific_b * new_specific_b =(specific_b *)输入;

}


(注意:我使用的是结构specific_b和specific_a仅用于阅读)。


我不想生成新消息,而是要转换结构(因为在

这个特定情况下的数据项完全匹配)。这是允许的吗?在其他

字中,返回调用new_specific_b-> data4返回data1和

new_specific_b-> data5返回data2或者是否存在一些编译器问题

请记住?



在某些非常具体的情况下(这里不适用
),语言需要打字类型

上班。大多数编译器使其在那些特殊情况下工作的方式具有使其成为

的副作用,即使在像你这样的情况下也是如此,严格来说这就是

不保证。你正走出语言保证的

范围之外,但它会造成麻烦的机会很小。


...除了麻烦的机会随着时间的推移接近
100%。这两种结构类型是完美的

今天匹配,但是当它们被维护,改进和扩展时会发生什么?

程序?你只需要巧合,就可以把你的希望寄托在一起,实际上是
。有人,有一天会改变其中一个或另一个两个同卵双胞胎的
。结构,突然之间

东西将停止工作。


你可能认为你可以通过

来解决这个问题在这两个结构上放置了很大的评论块,警告

程序员改变其中一个他必须做出的改变另一个改变。从战斗中获取 -

伤痕累累的老兵:从长远来看,这不会起作用。

编译器不强制执行你在评论中所说的内容,
和错误*将*发生。此外,阅读评论的程序员会诅咒你的名字:你将违反安全实施的重要原则



应该只是一个权威的

描述每种数据类型,功能,界面,或

其他东西在该计划中。唯一一个 - 因为

一旦有两个,就会有分歧。


你正在建立一个纸牌屋

空气仍然存在,即使你肯定知道

飓风最终会来临。建立你的信用卡房子

如果你坚持 - 但我不会给你一个

保险单。


-
Er*********@sun.com


6月11日下午6点,Eric Sosman< Eric.Sos ... @ sun.comwrote:

$ b $ m Milux在06/11/07 13:01写道:


我想要转换结构而不是生成新消息(因为在

这个特定情况下数据项完全匹配)。这是允许的吗?在其他

字中,返回调用new_specific_b-> data4返回data1和

new_specific_b-> data5返回data2或者是否存在一些编译器问题

请记住?



在某些非常具体的情况下(这里不适用
),语言需要打字类型

上班。大多数编译器使其在那些特殊情况下工作的方式具有使其成为

的副作用,即使在像你这样的情况下也是如此,严格来说这就是

不保证。你正在超出语言保证的界限范围之外,但它会造成麻烦的机会很小。



为什么这不保证行为?是否允许编译器生成具有不同内存占用量的
struct {int,int}?如果是这样,请问你能否b / b详细说明编译器在这种情况下的自由度?


我一直认为struct保证了元素的顺序。

这准确吗?


...除了遇到麻烦的机会

100%随着时间的推移过去了。这两种结构类型是完美的

今天匹配,但是当它们被维护,改进和扩展时会发生什么?

程序?你只需要巧合,就可以把你的希望寄托在一起,实际上是
。有人,有一天会改变其中一个或另一个两个同卵双胞胎的
。结构,突然之间

的东西将停止工作。



OP没有提供足够的背景来得出这个结论。

许多结构永远不会改变它们的定义。


Richard Urich在06/11/07 15:44写道:


6月11日,6:下午00点,Eric Sosman< Eric.Sos ... @ sun.comwrote:


>> Milux写于06/11/07 13:01, :


>>>而不是生成新消息,我想要转换结构(因为在
这个特定情况下数据项匹配究竟)。这是允许的吗?换句话说,返回调用new_specific_b-> data4返回data1和
new_specific_b-> data5返回data2或者是否有一些编译器问题需要记住?


在某些非常具体的情况下(这里不适用),语言要求打字类型
才能工作。大多数编译器使其在特殊情况下工作的方式有其副作用,即使在像你这样的情况下也能使它工作,严格来说这是无法保证的。你正在超越语言保证的范围之外,但它会造成麻烦的机会很小。




为什么这不保证行为?是否允许编译器生成具有不同内存占用量的
struct {int,int}?如果是这样,你能不能请b / b
详细说明编译器在这种情况下的自由度?



几个星期前,我们在一个名为这是合法的吗?美学上可以接受吗?通过它阅读

,如果你还有问题,请回来。


我一直认为struct保证了元素的顺序。

这个准确吗?



是的,但这还不足以确保OP的

代码能够正常工作。
< blockquote class =post_quotes>


> ......除了随着时间的推移,麻烦的机会接近100%。这两种结构类型今天是完美的匹配,但是当
程序被维护,改进和扩展时会发生什么呢?你真的把你的希望寄托在巧合之上。有人,有一天会改变两个同卵双胞胎中的一个或另一个。结构,突然之间会停止工作。



OP没有提供足够的背景来得出这个结论。

许多结构永远不会改变它们的定义。



看来你从未见过Major Murphy:

http://en.wikipedia.org/wiki/Edward_A._Murphy%2C_Jr


说真的,如果两件事应该是 - 事实上,

要求 - 相同,他们应该只是

一件事。否则,该计划的正确运作

取决于没有比巧合更坚固的东西。我们这些人* * * * * * * * * * * * * * $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $
Er*********@sun.com

Hi All,

This question has to do with interface design. Suppose I have an translation
tool. It can translates structs from type "general" to other types and then
does some processing.

Example:

typedef struct
{
int data1;
int data2;
}general;

typedef struct
{
int data3;
}specific_a;

For example, the translation looks like this:

void translate1(general * input)
{
specific_a * new_specific_a = malloc (.....);
new_specific_a->data3 = input->data1 * input->data2;

/* use here new_specific_a for a while */
}

Suppose have another type:
typedef struct
{
int data4;
int data5;
}specific_b;

In this specific case another translation looks like:

void translate2(general * input)
{
specific_b * new_specific_b = malloc(....);
new_specific_b->data4 = input->data1;
new_specific_b->data5 = input->data2;

/* do some processing with new_specific_b */

}

Because other interfaces are fixed I need type "specific_a" in the
"translate1" method and "specific_b" in "translate2".

I see an optimalization here in the translate2 method, namely:
void translate2(general * input)
{
specific_b * new_specific_b = (specific_b *) input;
}

(Note: I use the structures specific_b and specific_a only for reading).

Instead of generating the new message, I want to cast the struct (because in
this specific case the data items match exactly). Is this allowed?? In other
words, returns a call to new_specific_b->data4 return data1 and
new_specific_b->data5 return data2 or are there some compiler issues to take
keep in mind?

Grz Milux

解决方案

Milux wrote On 06/11/07 13:01,:

Hi All,

This question has to do with interface design. Suppose I have an translation
tool. It can translates structs from type "general" to other types and then
does some processing.

Example:

typedef struct
{
int data1;
int data2;
}general;

typedef struct
{
int data3;
}specific_a;

For example, the translation looks like this:

void translate1(general * input)
{
specific_a * new_specific_a = malloc (.....);
new_specific_a->data3 = input->data1 * input->data2;

/* use here new_specific_a for a while */
}

Suppose have another type:
typedef struct
{
int data4;
int data5;
}specific_b;

In this specific case another translation looks like:

void translate2(general * input)
{
specific_b * new_specific_b = malloc(....);
new_specific_b->data4 = input->data1;
new_specific_b->data5 = input->data2;

/* do some processing with new_specific_b */

}

Because other interfaces are fixed I need type "specific_a" in the
"translate1" method and "specific_b" in "translate2".

I see an optimalization here in the translate2 method, namely:
void translate2(general * input)
{
specific_b * new_specific_b = (specific_b *) input;
}

(Note: I use the structures specific_b and specific_a only for reading).

Instead of generating the new message, I want to cast the struct (because in
this specific case the data items match exactly). Is this allowed?? In other
words, returns a call to new_specific_b->data4 return data1 and
new_specific_b->data5 return data2 or are there some compiler issues to take
keep in mind?

In some very specific circumstances (which don''t
apply here), the language requires the type punning
to work. The way most compilers make it work in those
special circumstances has the side-effect of making it
work even in cases like yours, where strictly speaking
it is not guaranteed. You are stepping outside the
bounds of the language''s guarantees, but the chance
that it will cause trouble is small.

... except that the chance of trouble approaches
100% as time goes by. The two struct types are perfect
matches today, but what will happen to them as the
program is maintained, refined, and extended? You are
pinning your hopes on nothing more than a coincidence,
really. Somebody, someday will change one or the other
of the two "identical twin" structs, and all of a sudden
things will stop working.

You may think that you can solve this problem by
putting great big comment blocks on both structs, warning
the programmer who changes one of them that he must make
the same change in the other. Take it from a battle-
scarred veteran: This does *not* work in the long run.
The compiler does not enforce what you say in comments,
and mistakes *will* happen. Besides, the programmers who
read the comments will curse your name: You will have
violated an important principle of safe implementation
that states there should be exactly one authoritative
description of each data type, function, interface, or
other "thing" in the program. One and only one -- because
as soon as there are two, there can be disagreement.

You are erecting a house of cards on a day when the
air is still, even though you know for certain that
hurricanes will eventually come. Build your card house
if you insist -- but I for one will not sell you an
insurance policy on it.

--
Er*********@sun.com


On Jun 11, 6:00 pm, Eric Sosman <Eric.Sos...@sun.comwrote:

Milux wrote On 06/11/07 13:01,:

Instead of generating the new message, I want to cast the struct (because in
this specific case the data items match exactly). Is this allowed?? In other
words, returns a call to new_specific_b->data4 return data1 and
new_specific_b->data5 return data2 or are there some compiler issues to take
keep in mind?


In some very specific circumstances (which don''t
apply here), the language requires the type punning
to work. The way most compilers make it work in those
special circumstances has the side-effect of making it
work even in cases like yours, where strictly speaking
it is not guaranteed. You are stepping outside the
bounds of the language''s guarantees, but the chance
that it will cause trouble is small.

Why is this not guaranteed behavior? Is a compiler allowed to generate
struct{int,int} with varying memory footprints? If so, can you please
elaborate what freedoms a compiler has in this case?

I had always thought struct guaranteed the order of the elements. Is
this accurate?

... except that the chance of trouble approaches
100% as time goes by. The two struct types are perfect
matches today, but what will happen to them as the
program is maintained, refined, and extended? You are
pinning your hopes on nothing more than a coincidence,
really. Somebody, someday will change one or the other
of the two "identical twin" structs, and all of a sudden
things will stop working.

The OP did not provide sufficient context to make this conclusion.
Many structures never change by their very definition.


Richard Urich wrote On 06/11/07 15:44,:

On Jun 11, 6:00 pm, Eric Sosman <Eric.Sos...@sun.comwrote:

>>Milux wrote On 06/11/07 13:01,:

>>>Instead of generating the new message, I want to cast the struct (because in
this specific case the data items match exactly). Is this allowed?? In other
words, returns a call to new_specific_b->data4 return data1 and
new_specific_b->data5 return data2 or are there some compiler issues to take
keep in mind?


In some very specific circumstances (which don''t
apply here), the language requires the type punning
to work. The way most compilers make it work in those
special circumstances has the side-effect of making it
work even in cases like yours, where strictly speaking
it is not guaranteed. You are stepping outside the
bounds of the language''s guarantees, but the chance
that it will cause trouble is small.



Why is this not guaranteed behavior? Is a compiler allowed to generate
struct{int,int} with varying memory footprints? If so, can you please
elaborate what freedoms a compiler has in this case?

We hashed this out a couple weeks ago, in a thread
titled "Is this legal? Aesthetically acceptable?". Read
through it, and come back if you still have questions.

I had always thought struct guaranteed the order of the elements. Is
this accurate?

Yes, but it''s not enough to ensure that the O.P.''s
code will work.

> ... except that the chance of trouble approaches
100% as time goes by. The two struct types are perfect
matches today, but what will happen to them as the
program is maintained, refined, and extended? You are
pinning your hopes on nothing more than a coincidence,
really. Somebody, someday will change one or the other
of the two "identical twin" structs, and all of a sudden
things will stop working.


The OP did not provide sufficient context to make this conclusion.
Many structures never change by their very definition.

It seems you''ve never met Major Murphy:

http://en.wikipedia.org/wiki/Edward_A._Murphy%2C_Jr.

Seriously, if two things are supposed to be -- in fact,
required to be -- identical, they should probably be just
one thing. Otherwise, the proper operation of the program
rests on nothing sturdier than a coincidence. Those of us
who *have* made the Major''s acquaintance are distrustful
of mere coincidence.

--
Er*********@sun.com


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

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