小野@名古屋大学 です.

<YAS.04Jan20205152@kirk.is.tsukuba.ac.jp>の記事において
yas@is.tsukuba.ac.jpさんは書きました。
yas> > char src[10] = "abc";
yas> > だったら src[5] は 0 になります.
yas> この規格は、ひどいね。char a[10] ならか可愛いけど、char
yas> a[10000] = "abc" とかなると、想像したくない。去年は、配列の
yas> 代入の話をしていたわけなので、それがOKならこれもOKにしな
yas> いといけないんだろうけど。
この「ひどい」というのは, 「初期化子がなければどんな値になってい
るかわからないけど, 初期化子があれば必ず 0 になる」のがひどいって
意味ですね?

yas> 最初の問題にもどします。文字列リテラルによる初期化という話で
yas> はなくて、単に初期化していない要素をアクセスした時に、
yas> segmentation fault か bus error が起きるかどうかです。
一応規格上は
6.7.8 Initialization, paragraph 10:
If an object that has automatic storage duration is not
initialized explicitly, its value is indeterminate.

でこの indeterminate は 3.17.2:
indeterminate value
either an unspecified value or a trap representation.

unspecified value は 3.17.3:
unspecified value
valid value of the relevant type where this International
Standard imposes no requirements on which value is chosen in any
instance.

trap representation については 6.2.6.1 General, paragraph 5:
Certain object representations need not represent a value of the
object type. If the stored value of an object has such a
representation and is read by an lvalue expression that does not
have character type, the behavior is undefined. If such a
representation is produced by a side effect that modifies all or
any part of the object by an lvalue expression that does not have
character type, the behavior is undefined. Such a representation
is called a trap representation.

配列要素に限らないんですが, 初期化しない自動変数の値を参照という
のは, 規格上は「何が起きても知らない」ってことになりますかね.

yas> > 規格を調べていただければよいのですが....
yas> 規格には、興味は、あまりないので。興味があるのは、よいプログ
yas> ラムを書くことや、よいデバッグの方法です。
「よいプログラム」とはどういう意味でしょうか? その意味によっては
規格が重要になりえると思います.

yas> > 現実的には:
yas> > bus error はないかもしれませんが segmentation fault はありえます.
yas> > 規格上は:
yas> > undefined behavior なので, 何が起きても文句は言えません.
yas> 現実の方ね。
残念ながら現実についてはほとんど知りませんので....
-- 
名古屋大学大学院 情報科学研究科 計算機数理科学専攻
小野 孝男