只是对一些结构的好奇心 [英] Just curiosity about some constructs

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

问题描述

嗨再次!


我仍​​然对一些C构造/关键字的原因感到好奇...


第一个是关于静态函数。限制

只是在特定源文件中可见的函数是什么原因?不是吗?b $ b足以不给出原型(可见性)?


注册和变量变量怎么样?当时编译器是否b / b $ b不够聪明,无法使用寄存器内变量进行优化?为什么有人会建议编译器不要通过使变量

波动来优化?


最后一个问题!这是关于switch语句的。声明似乎对我来说是完全不同于其他人的。让我用一个

的例子解释一下。一段时间(条件)将执行

while()之后的语句,如果有人想在循环中有更多的指令要执行

,那么{}应该使用。这也适用于if / else,

执行/循环,但不适用于交换机。一个案例不需要任何{}到

执行多条指令,而且,必须给出制动器

使一个案件被执行,否则以下所有

非制动案例陈述将被执行。为什么不像{}和自动刹车那样开关?$ / b $

它可能没用,但不是好奇...... 。


-

Sensei< se ****** @ mac.com>


计算机的不人道性的一部分是,一旦它完全符合程序并且工作顺利,它就是完全诚实的。 (Isaac Asimov)

Hi again!

I have still curiosity about the reason of some C constructs/keywords...

The first is about static functions. What was the reason of restricting
a function to be visible just in a specific source file? Wasn''t it
sufficient not to be given a prototype (for visibility)?

What about register and volatile variables? Was at that time a compiler
not smart enough to optimize with in-register variables? And why would
someone suggest the compiler not to optimze by making a variable
volatile?

Last question! This is about the switch statement. The statement seems
to me to be completely different from others. Let me explain with an
example. A while(condition) will execute the statement after the
while(), and if someone wants to have more instructions to be executed
in the loop, then { } should be used. This is true also for if/else,
do/loop, but not with the switch. A case does not require any { } to
execute more than one instruction, moreover, a brake must be given to
make a single case being executed, otherwise all the following
non-brake case statements will be executed. Why wasn''t the switch like
the others with { } and automatic brake?

It''s probably useless, but not for curiosity...

--
Sensei <se******@mac.com>

Part of the inhumanity of the computer is that, once it is competently
programmed and working smoothly, it is completely honest. (Isaac Asimov)

推荐答案




Sensei写于01/31/06 10 :01,:


Sensei wrote On 01/31/06 10:01,:
你好了!

我仍然对一些C构造/关键字的原因感到好奇......

第一个是关于静态函数。将某个函数限制为仅在特定源文件中可见的原因是什么?难道不足以不给出原型(可见度)吗?


否;你混淆了范围和联系。如果没有

静态,则函数标识符具有外部链接,

意味着无论使用多少翻译单元,该名称在整个

计划中只能引用一件事。如果

你在一个文件中有'void foo(void){...}'',而在另一个文件中你有写'double foo(char * ptr){。 ..}'',标识符在两者中都有外部链接

,两者使用冲突。使用static,

标识符具有内部链接,意思是它确实

与其他翻译单位中的'foo''(外部或内部)的任何使用都没有冲突。


典型的用法是翻译单元(模块)



外部链接定义一些函数(和/或变量),所以代码在其他翻译单位可以

参考他们。同时,帮助者函数(和/或

变量)被声明为静态,因此其他模块不能使用
引用它们,因此它们的名称不会与标识符冲突

用于其他模块。把这个

意义上的静态想象为私有。

寄存器和易变变量怎么样?当时编译器还不够智能,无法使用寄存器内变量进行优化?为什么有人建议编译器不要通过使变量变得易变来优化?


今天的编译器在优化方面比过去的b $ b b更好。这部分是因为该州的艺术进步,但主要是因为运行

编译器的机器比过去更大更快。 br />
现代编译器在执行

优化时所做的计算会花费太多时间和内存

使它们在64KB的机器上可行内存和一个250亿美元的CPU。 `register''是一个有用的提示,资源紧张

编译器;现在它基本上没用了。


'volatile''是另一回事,并且仍然相关。

它告诉编译器它是不安全的优化

对变量的访问,通常是因为变量可以因编译器知识之外的原因而改变
或因为

使访问有一些副作用编译器

无法知道。一个常见的例子是使用内存连接到I / O控制寄存器的位置,硬件

时钟等等。你可能会写一个延迟循环,比如


而(* hardware_timer< target_time)

;


.. ..你会,呃,失望如果编译器转为

这相当于


if(* hardware_timer< target_time)

while(1)<通过优化冗余来实现
;


....获取价值。

同样,您可以通过将

系列命令代码写入特殊位置来控制I / O设备:

* control_word = SEEK_TO_CYLINDER + cyl_num;

* control_word = START_WRITING;


....如果你再次感到失望编译器决定放弃第一个作业,理由是

值会被第二个作出立即覆盖。

最后一个问题!这是关于switch语句的。这句话似乎对我来说与其他人完全不同。让我用一个
例子来解释。一段时间(条件)将执行
while()之后的语句,如果有人想在循环中有更多的指令要执行,那么应该使用{}。这也适用于if / else,
执行/循环,但不适用于交换机。一个案例不需要任何{}来执行多个指令,而且,必须给出一个制动器来执行单个案例,否则以下所有的非制动案例语句将被执行。为什么不像其他{}和自动刹车那样开关?
Hi again!

I have still curiosity about the reason of some C constructs/keywords...

The first is about static functions. What was the reason of restricting
a function to be visible just in a specific source file? Wasn''t it
sufficient not to be given a prototype (for visibility)?
No; you are confusing "scope" and "linkage." Without
`static'', a function identifier has "external linkage," which
means that the name can refer to only one thing in the entire
program, no matter how many translation units are used. If
you have `void foo(void) {...}'' in one file and in another
you write `double foo(char *ptr) {...}'', the identifier has
external linkage in both and the two uses clash. With `static'',
the identifier has "internal linkage," meaning that it does
not clash with any uses of `foo'' (external or internal) in
other translation units.

A typical use is for a translation unit ("module") to
define some number of functions (and/or variables) with
external linkage, so code in other translation units can
refer to them. Meanwhile, "helper" functions (and/or
variables) are declared `static'' so other modules cannot
refer to them and so their names won''t clash with identifiers
used in those other modules. Think of `static'' in this
sense as meaning "private."
What about register and volatile variables? Was at that time a compiler
not smart enough to optimize with in-register variables? And why would
someone suggest the compiler not to optimze by making a variable
volatile?
Compilers today are much better at optimizing than those
of the past. This is partly because of advances in the state
of the art, but mostly because the machines that run the
compilers are much larger and faster than those of the past.
The computations a modern compiler makes while performing its
optimizations would have taken far too much time and memory
to make them feasible on machines with 64KB of memory and a
250KHz CPU. `register'' was a helpful hint to resource-strapped
compilers; nowadays it is largely useless.

`volatile'' is a different matter, and is still relevant.
It tells the compiler that it is not safe to optimize the
accesses to a variable, usually because the variable can
change for reasons outside the compiler''s knowledge or because
the act of making the access has some side-effect the compiler
cannot know about. One common example is the use of memory
locations that connect to I/O control registers, hardware
clocks, and so on. You might write a delay loop like

while (*hardware_timer < target_time)
;

.... and you''d be, er, "disappointed" if the compiler turned
this into the equivalent of

if (*hardware_timer < target_time)
while (1)
;

.... by optimizing away the "redundant" fetches of the value.
Similarly, you might control an I/O device by writing a
series of command codes to a special location:

*control_word = SEEK_TO_CYLINDER + cyl_num;
*control_word = START_WRITING;

.... and you''d be disappointed again if the compiler decided
to discard the first assignment on the grounds that the
value would be immediately overwritten by the second.
Last question! This is about the switch statement. The statement seems
to me to be completely different from others. Let me explain with an
example. A while(condition) will execute the statement after the
while(), and if someone wants to have more instructions to be executed
in the loop, then { } should be used. This is true also for if/else,
do/loop, but not with the switch. A case does not require any { } to
execute more than one instruction, moreover, a brake must be given to
make a single case being executed, otherwise all the following
non-brake case statements will be executed. Why wasn''t the switch like
the others with { } and automatic brake?




开关中的案例标签就是这样的:标签

代码中的单个点,而不是区域的标识符

代码。这样可以很容易地将几个值发送到

相同的代码片段:


开关(字符){

case' ''':

case''\t'':

case''\ n'':

case''\\ \\ f'':

case''\ r'':

process_white_space();

break;


case''。'':

case'','':

case'':'':

process_punctuation();

休息;


...

}


许多语言都具有这种一般风格的结构,而不是具有完全相同规则的
。为什么dmr选择这个特殊的

计划只是他能回答的问题。他的选择被广泛批评并广受好评。


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



The case labels in a `switch'' are exactly that: labels
for single points in the code, not identifiers of regions
of code. That makes it easy to send several values to the
same piece of code:

switch (character) {
case '' '':
case ''\t'':
case ''\n'':
case ''\f'':
case ''\r'':
process_white_space();
break;

case ''.'':
case '','':
case '':'':
process_punctuation();
break;

...
}

Many languages have a construct of this general flavor, not
all with quite the same rules. Why dmr chose this particular
scheme is a question only he can answer. His choice has been
both widely criticized and widely applauded.

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


Sensei写道:
Sensei wrote:
我仍然对一些C构造/关键字的原因感到好奇......

第一个是静态函数。将某个函数限制为仅在特定源文件中可见的原因是什么?难道不足以不给出原型(可见度)吗?


如果你不做静态功能,在链接阶段它可能是

找到由链接器在提供的

对象文件中寻找同名函数,即使你没有在其他源文件中提供其原型。确保您的功能永远不会找到在这个

的方式(即只能访问同一来源中的其他函数

文件)你声明它是`static`。

怎么样寄存器和volatile变量?当时编译器还不够智能,无法使用寄存器内变量进行优化?为什么有人建议编译器不要通过使变量变得易变来优化?


`register`变量是在编译器

不太优化时引入的,你正确地得到了它。


然而,`volatile`是/不/意味着不优化。这意味着

这个变量可能会在您的背后发生变化,而不会通过您的

计划的行动,因此不要对其值进行任何假设。在大多数情况下,但并非所有情况下,这实际上都会阻止编译器优化引用`volatile`变量的代码。

最后一个问题!这是关于switch语句的。这句话似乎对我来说与其他人完全不同。让我用一个
例子来解释。一段时间(条件)将执行
while()之后的语句,如果有人想在循环中有更多的指令要执行,那么应该使用{}。这也适用于if / else,
执行/循环,但不适用于交换机。一个案例不需要任何{}来执行多个指令,而且,必须给出一个制动器来执行单个案例,否则以下所有的非制动案例语句将被执行。为什么不是像{}和自动刹车那样的开关?


如果我错了,我肯定有人会纠正我,但我认为`switch`陈述的

哲学具有历史性原因,或者今天的一些理由,如果你不知道C的故事,那就不是很有意义了。但是,我不认为它的工作方式是

任何方式错误或奇怪的,只是不符合预期,或通常,给予

今天存在的其他编程语言。恕我直言,在许多情况下,

它的工作方式实际上是可取的。我能想到的唯一不足之处

是'break`语句的一些额外输入(但你保存/不/

输入括号!;-))。

它可能没用,但不是好奇......
I have still curiosity about the reason of some C constructs/keywords...

The first is about static functions. What was the reason of restricting
a function to be visible just in a specific source file? Wasn''t it
sufficient not to be given a prototype (for visibility)?
If you don''t make the function `static`, at the linking stage it may be
"found" by the linker looking for a function of the same name in the
object files provided, even if you did not provide its prototype in
other source files. To ensure your function is never "found" in this
way (i.e. is accessible only to other functions within the same source
file) you declare it `static`.
What about register and volatile variables? Was at that time a compiler
not smart enough to optimize with in-register variables? And why would
someone suggest the compiler not to optimze by making a variable
volatile?
The `register` variables were introduced at the time when compilers
were not as good at optimising, you got that one correctly.

The `volatile`, however, does /not/ mean "do not optimise". It means
"this variable may change behind your back, through no action of your
program, so do not make any assumptions about its value". In most, but
not all, cases this does in effect prevent the compiler from optimising
bits of code that refer to a `volatile` variable.
Last question! This is about the switch statement. The statement seems
to me to be completely different from others. Let me explain with an
example. A while(condition) will execute the statement after the
while(), and if someone wants to have more instructions to be executed
in the loop, then { } should be used. This is true also for if/else,
do/loop, but not with the switch. A case does not require any { } to
execute more than one instruction, moreover, a brake must be given to
make a single case being executed, otherwise all the following
non-brake case statements will be executed. Why wasn''t the switch like
the others with { } and automatic brake?
I''m sure someone will correct me if I''m wrong, but I think that the
philosophy of the `switch` statement has historical reasons, or some
rationale that today, and if you don''t know "the story of C", does not
make much sense. However, I do not think that the way it works is in
any way "wrong" or "strange", just "not as expected, or usual, given
other programming languages in existence today". IMHO, in many cases,
the way it works is actually preferrable. The only downside I can think
of is some extra typing for `break` statements (but you save on /not/
typing the braces! ;-) ).
It''s probably useless, but not for curiosity...




我不太在意你的想法......


干杯


弗拉基米尔



I don''t quite follow your thought here...

Cheers

Vladimir


2006-01- 31 17:01:29 +0100,Vladimir S. Oka <无**** @ btopenworld.com>说:

On 2006-01-31 17:01:29 +0100, "Vladimir S. Oka" <no****@btopenworld.com> said:

它可能没用,但不是好奇......
It''s probably useless, but not for curiosity...




I don''t quite follow your thought here...




好​​吧,像这样的问题可以被视为毫无用处。我好奇地问了这个
的好奇心,而不是任何更高的意图...... :)


-

Sensei< se ****** @ mac.com>


计算机不人道的部分原因是,一旦胜利,就会产生这样的好处。

编程和工作顺利,这是完全诚实的。 (Isaac Asimov)



Well, questions like this one can be seen as useless. I asked this just
for curiosity, not for any ``higher'''' intention... :)

--
Sensei <se******@mac.com>

Part of the inhumanity of the computer is that, once it is competently
programmed and working smoothly, it is completely honest. (Isaac Asimov)


这篇关于只是对一些结构的好奇心的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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