A pointer to an object or incomplete type may be converted to a pointer to a different object or incomplete type. If the resulting pointer is not correctly aligned57) for the pointed-to type, the behavior is undefined.
逆参照した場合の挙動が処理系定義 or 未定義と覚え違いをしていました。ポインタの時点でそうなんですね。 が、同じ 6.3.2.3 Pointers の 5 で
An integer may be converted to any pointer type. Except as previously specified, the result is implementation-defined, might not be correctly aligned, might not point to an entity of the referenced type, and might be a trap reprensentation.
by
Anonymous Coward
on 2013年02月04日 11時01分
(#2318397)
7.20.3 Memory management functions の 1 に
The pointer returned if the allocation succeeds is suitably aligned so that it may be assigned to a pointer to any type of object and then used to access such an object or an array of such objects in the space allocated (until the space is explicitly deallocated).
(uint64_t *)((char *)malloc(... が規格上未定義 (スコア:0)
なのでプログラムのバグ。
Re: (スコア:1)
どの辺がダメなんでしょう?
void * → char * : 言うまでもなくOKだと思います。
char *型に +1 : これもOKだと思います。
char * → uint64_t * : 異なるオブジェクト型のポインタの相互変換はOKだったと思いますがダメでしたっけ?
まあ、これが未定義だとしても別の方法で奇数アドレスを持つ、十分な大きさの有効なオブジェクトをsrcに代入すればいいわけで。
そもそも未定義とならない方法でそれを実現できる方法はなかったりします?
巧妙に潜伏したバグは心霊現象と区別が付かない。
Re: (スコア:3, 参考になる)
元ACとは別人ですが。
6.3.2.3 Pointers の 7 に
とありますので、 char * を uint64_t * に変換した時点で C99 的には undefined だと思います。
何が correctly aligned かは規格書中では述べられていない(と思う)ので
コンパイラのバグかどうかはちょっとわか
Re: (スコア:1)
逆参照した場合の挙動が処理系定義 or 未定義と覚え違いをしていました。ポインタの時点でそうなんですね。
が、同じ 6.3.2.3 Pointers の 5 で
とありますので、char *経由ではなくintptr_t経由ならOKのように思えました。またはポインタ経由でもgccで保証があればいいわけですが未調査です。
※ 「prev
巧妙に潜伏したバグは心霊現象と区別が付かない。
Re:(uint64_t *)((char *)malloc(... が規格上未定義 (スコア:0)
7.20.3 Memory management functions の 1 に
The pointer returned if the allocation succeeds is suitably aligned so that it may be assigned to a pointer to any type of object and then used to access such an object or an array of such objects in the space allocated (until the space is explicitly deallocated).
とmalloc系による動的メモリ確保したアドレスは、「任意の」型として利用可能なアライメントが取られるように思えます。
あと、 -O3 に -ftree-vectorize が含まれているので SIMD の使用は明示されてしまっていると思います。
結局はコレ(最適化オプションにより↑前提条件を無視してよいと明示している)なんじゃないですかね?