uint32_t を使用して任意のタイプのアイテムを移動し、それらを読み戻すことは、厳密なエイリアシング規則に違反しますか? もしそうなら、uint32_ts の配列から任意の型の配列への memcpy への厳密なエイリアシング規則にも違反し、要素を読み戻しますか?
次のコード サンプルは、両方のケースを示しています。
#include <assert.h>
#include <stdio.h>
#include <stdint.h>
#include <string.h>
int main(void) {
const char *strings[5] = {
"zero", "one", "two", "three", "four"
};
uint32_t buffer[5];
int i;
assert(sizeof(const char*) == sizeof(uint32_t));
memcpy(buffer, strings, sizeof(buffer));
//twiddle with the buffer a bit
buffer[0] = buffer[3];
buffer[2] = buffer[4];
buffer[3] = buffer[1];
//Does this violate strict aliasing?
const char **buffer_cc = (const char**)buffer;
printf("Test 1:\n");
for (i=0; i<5; i++)
printf("\t%s ", buffer_cc[i]);
printf("\n");
//How about this?
memcpy(strings, buffer, sizeof(strings));
printf("Test 2:\n");
for (i=0; i<5; i++)
printf("\t%s ", strings[i]);
printf("\n");
return 0;
}
32 ビット プラットフォームに関する私の仮定は無視してください。また、要素が uint32_t と同じサイズでない場合は、それらをパディングして正しい数の uint32_t をコピーすることを知っています。私の質問は、そうすることが厳密なエイリアシングに違反するかどうかに焦点を当てています。