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.
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:(uint64_t *)((char *)malloc(... が規格上未定義 (スコア:1)
どの辺がダメなんでしょう?
void * → char * : 言うまでもなくOKだと思います。
char *型に +1 : これもOKだと思います。
char * → uint64_t * : 異なるオブジェクト型のポインタの相互変換はOKだったと思いますがダメでしたっけ?
まあ、これが未定義だとしても別の方法で奇数アドレスを持つ、十分な大きさの有効なオブジェクトをsrcに代入すればいいわけで。
そもそも未定義とならない方法でそれを実現できる方法はなかったりします?
巧妙に潜伏したバグは心霊現象と区別が付かない。
Re:(uint64_t *)((char *)malloc(... が規格上未定義 (スコア:3, 参考になる)
元ACとは別人ですが。
6.3.2.3 Pointers の 7 に
とありますので、 char * を uint64_t * に変換した時点で C99 的には undefined だと思います。
何が correctly aligned かは規格書中では述べられていない(と思う)ので
コンパイラのバグかどうかはちょっとわかりませんが…。
あと、 -O3 に -ftree-vectorize が含まれているので SIMD の使用は明示されてしまっていると思います。
Re:(uint64_t *)((char *)malloc(... が規格上未定義 (スコア:1)
逆参照した場合の挙動が処理系定義 or 未定義と覚え違いをしていました。ポインタの時点でそうなんですね。
が、同じ 6.3.2.3 Pointers の 5 で
とありますので、char *経由ではなくintptr_t経由ならOKのように思えました。またはポインタ経由でもgccで保証があればいいわけですが未調査です。
※ 「previously specified」は、nullポインタと0についての話 (2で規定) のことだと思います。
また、アライメントについての話は、C11で 6.2.8 Alignment of objects という節が追加されています。
ただ、アライメントのサイズがどうなるかという話のみで、アライメントされていない場合のアクセスはどうなるかまでは書かれていません。
なお、C11で追加された _Alignof の結果は、char: 1、short: 2、int: 4、long: 8、float: 4、double: 8 (gcc-4.7.2, x86_64 -- 4.6.3では_Alignof未サポート) でした。sizeofと一致しますね。
調べるべきことが残っている上に増えてもいますが、とりあえずはここまで。
巧妙に潜伏したバグは心霊現象と区別が付かない。
Re: (スコア: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 の使用は明示されて