typedef struct Msg{unsigned int a;unsigned int b;} Msg;
void SendWord(uint32_t);
int main(void){// Get a 32-bit buffer from the systemuint32_t* buff = malloc(sizeof(Msg));
// Alias that buffer through messageMsg* msg = (Msg*)(buff);
// Send a bunch of messagesfor (int i = 0; i < 10; ++i){msg->a = i;msg->b = i+1;SendWord(buff[0]);SendWord(buff[1]);}}
int x = 1;int *p = &x;printf("%d\n", *p); // *p gives us an lvalue expression of type int which is compatible with int
-与对象的有效类型兼容的类型的限定版本,
int x = 1;const int *p = &x;printf("%d\n", *p); // *p gives us an lvalue expression of type const int which is compatible with int
-与对象的有效类型对应的有符号或无符号类型的类型,
int x = 1;unsigned int *p = (unsigned int*)&x;printf("%u\n", *p ); // *p gives us an lvalue expression of type unsigned int which corresponds to// the effective type of the object
int x = 1;const unsigned int *p = (const unsigned int*)&x;printf("%u\n", *p ); // *p gives us an lvalue expression of type const unsigned int which is a unsigned type// that corresponds with to a qualified version of the effective type of the object
-在其成员中包含上述类型之一的聚合或联合类型(包括递归地,子聚合或包含联合的成员),或
struct foo {int x;};
void foobar( struct foo *fp, int *ip ); // struct foo is an aggregate that includes int among its members so it// can alias with *ip
foo f;foobar( &f, &f.x );
-字符类型。
int x = 65;char *p = (char *)&x;printf("%c\n", *p ); // *p gives us an lvalue expression of type char which is a character type.// The results are not portable due to endianness issues.
C++17标准草案怎么说
C++17标准草案第0节说:
如果程序尝试通过以下类型之一以外的glvalue访问对象的存储值,则行为未定义:63
(11.1)-对象的动态类型,
void *p = malloc( sizeof(int) ); // We have allocated storage but not started the lifetime of an objectint *ip = new (p) int{0}; // Placement new changes the dynamic type of the object to intstd::cout << *ip << "\n"; // *ip gives us a glvalue expression of type int which matches the dynamic type// of the allocated object
(11.2)-对象的动态类型的cv限定版本,
int x = 1;const int *cip = &x;std::cout << *cip << "\n"; // *cip gives us a glvalue expression of type const int which is a cv-qualified// version of the dynamic type of x
(11.3)-与对象的动态类型相似(如7.5中定义的)的类型,
(11.4)-对应于对象动态类型的有符号或无符号类型的类型,
// Both si and ui are signed or unsigned types corresponding to each others dynamic types// We can see from this godbolt(https://godbolt.org/g/KowGXB) the optimizer assumes aliasing.signed int foo( signed int &si, unsigned int &ui ) {si = 1;ui = 2;
return si;}
(11.5)-对应于对象动态类型的cv限定版本的有符号或无符号类型的类型,
signed int foo( const signed int &si1, int &si2); // Hard to show this one assumes aliasing
struct foo { int x; };
struct bar : public foo {};
int foobar( foo &f, bar &b ) {f.x = 1;b.x = 2;
return f.x;}
(11.8)-一个char,无符号char或std::byte类型。
int foo( std::byte &b, uint32_t &ui ) {b = static_cast<std::byte>('a');ui = 0xFFFFFFFF;
return std::to_integer<int>( b ); // b gives us a glvalue expression of type std::byte which can alias// an object of type uint32_t}
int x = 1;
// In Cfloat *fp = (float*)&x; // Not a valid aliasing
// In C++float *fp = reinterpret_cast<float*>(&x); // Not a valid aliasing
printf( "%f\n", *fp );
struct uint_chars {unsigned char arr[sizeof( unsigned int )] = {}; // Assume sizeof( unsigned int ) == 4};
// Assume len is a multiple of 4int bar( unsigned char *p, size_t len ) {int result = 0;
for( size_t index = 0; index < len; index += sizeof(unsigned int) ) {uint_chars f;std::memcpy( f.arr, &p[index], sizeof(unsigned int));unsigned int result = bit_cast<unsigned int>(f);
result += foo( result );}
return result;}
int a = 1;short j;float f = 1.f; // Originally not initialized but tis-kernel caught// it was being accessed w/ an indeterminate value below
printf("%i\n", j = *(reinterpret_cast<short*>(&a)));printf("%i\n", j = *(reinterpret_cast<int*>(&f)));
int *x = new int[2]; // 8 bytes: [0,7].int *u = (int*)((char*)x + 6); // regardless of alignment of x this will not be an aligned address*u = 1; // Access to range [6-9]printf( "%d\n", *u ); // Access to range [6-9]
./bin/tis-kernel -sa example1.c...example1.c:9:[sa] warning: The pointer (short *)(& a) has type short *. It violates strict aliasingrules by accessing a cell with effective type int....
example1.c:10:[sa] warning: The pointer (int *)(& f) has type int *. It violates strict aliasing rules byaccessing a cell with effective type float.Callstack: main...
example1.c:15:[sa] warning: The pointer (short *)p has type short *. It violates strict aliasing rules byaccessing a cell with effective type int.