strict-aliasing

Are all pointers derived from pointers to structure types the same?

我怕爱的太早我们不能终老 提交于 2019-11-30 11:22:59
问题 The Question The question of whether all pointers derived from pointers to structure types are the same, is not easy to answer. I find it to be a significant question for the following two primary reasons. A. The lack of a pointer to pointer to 'any' incomplete or object type, imposes a limitation on convenient function interfaces, such as: int allocate(ANY_TYPE **p, size_t s); int main(void) { int *p; int r = allocate(&p, sizeof *p); } [Complete code sample] The existing pointer to 'any'

Fix for dereferencing type-punned pointer will break strict-aliasing

↘锁芯ラ 提交于 2019-11-30 11:01:18
问题 I'm trying to fix two warnings when compiling a specific program using GCC. The warnings are: warning: dereferencing type-punned pointer will break strict-aliasing rules [-Wstrict-aliasing] and the two culprits are: unsigned int received_size = ntohl (*((unsigned int*)dcc->incoming_buf)); and *((unsigned int*)dcc->outgoing_buf) = htonl (dcc->file_confirm_offset); incoming_buf and outgoing_buf are defined as follows: char incoming_buf[LIBIRC_DCC_BUFFER_SIZE]; char outgoing_buf[LIBIRC_DCC

Memcpy implementation, strict aliasing

送分小仙女□ 提交于 2019-11-30 10:20:54
While learning c I have implemented my own memcpy functions. I have used a wider type( uint32_t ) in the function. (For simplicity the function is restricted to types that are multiples of 4 and the data is properly aligned ) void memcpy4( void* dst , void* src , int size ) { size /= 4; for ( int i = 0 ; i < size ; i++ ) ((uint32_t*)dst)[i] = ((uint32_t*)src)[i]; } I did some reading on type punning and strict aliasing and I believe the function above breaks the rule. The correct implementation would be this since you can use a char: void memcpy4( void* dst , void* src , int size ) { for ( int

Can Aliasing Problems be Avoided with const Variables

百般思念 提交于 2019-11-30 09:43:32
My company uses a messaging server which gets a message into a const char* and then casts it to the message type. I've become concerned about this after asking this question . I'm not aware of any bad behavior in the messaging server. Is it possible that const variables do not incur aliasing problems? For example say that foo is defined in MessageServer in one of these ways: As a parameter: void MessageServer(const char* foo) Or as const variable at the top of MessageServer : const char* foo = PopMessage(); Now MessageServer is a huge function, but it never assigns anything to foo , however at

How do I reinterpret data through a different type? (type punning confusion)

一世执手 提交于 2019-11-30 09:19:42
问题 #include <iostream> int main(int argc, char * argv[]) { int a = 0x3f800000; std::cout << a << std::endl; static_assert(sizeof(float) == sizeof(int), "Oops"); float f2 = *reinterpret_cast<float *>(&a); std::cout << f2 << std::endl; void * p = &a; float * pf = static_cast<float *>(p); float f3 = *pf; std::cout << f3 << std::endl; float f4 = *static_cast<float *>(static_cast<void *>(&a)); std::cout << f4 << std::endl; } I get the following info out of my trusty compiler: me@Mint-VM ~/projects $

C99 strict aliasing rules in C++ (GCC)

浪子不回头ぞ 提交于 2019-11-30 08:13:53
As far as I understand, GCC supports all of its C99 features in C++. But how is C99 strict aliasing handled in C++ code? I know that casting with C casts between unrelated types is not strict-aliasing-safe and may generate incorrect code, but what about C++? Since strict aliasing is not part of C++ standard (is that correct?), GCC must be specifying the semantics itself. I figure const_cast and static_cast cast between related types, hence they are safe, while reinterpret_cast can break strict aliasing rules. Is this a correct understanding? No, you are probably mixing different things. Strict

C++'s Strict Aliasing Rule - Is the 'char' aliasing exemption a 2-way street?

为君一笑 提交于 2019-11-30 06:54:20
Just a couple weeks ago, I learned that the C++ Standard had a strict aliasing rule. Basically, I had asked a question about shifting bits -- rather than shifting each byte one at a time, to maximize performance I wanted to load my processor's native register's with (32 or 64 bits, respectively) and perform the shift of 4/8 bytes all in a single instruction. This is the code I wanted to avoid: unsigned char buffer[] = { 0xab, 0xcd, 0xef, 0x46 }; for (int i = 0; i < 3; ++i) { buffer[i] <<= 4; buffer[i] |= (buffer[i + 1] >> 4); } buffer[3] <<= 4; And instead, I wanted to use something like:

Unions, aliasing and type-punning in practice: what works and what does not?

不羁的心 提交于 2019-11-30 06:53:19
I have a problem understanding what can and cannot be done using unions with GCC. I read the questions (in particular here and here ) about it but they focus the C++ standard, I feel there's a mismatch between the C++ standard and the practice (the commonly used compilers). In particular, I recently found confusing informations in the GCC online doc while reading about the compilation flag -fstrict-aliasing . It says: -fstrict-aliasing Allow the compiler to assume the strictest aliasing rules applicable to the language being compiled. For C (and C++), this activates optimizations based on the

Strict aliasing and memory locations

你。 提交于 2019-11-30 06:44:53
Strict aliasing prevents us from accessing the same memory location using an incompatible type. int* i = malloc( sizeof( int ) ) ; //assuming sizeof( int ) >= sizeof( float ) *i = 123 ; float* f = ( float* )i ; *f = 3.14f ; this would be illegal according to C standard, because the compiler "knows" that int cannot accessed by a float lvalue. What if I use that pointer to point to correct memory, like this: int* i = malloc( sizeof( int ) + sizeof( float ) + MAX_PAD ) ; *i = 456 ; First I allocate memory for int , float and the last part is memory which will allow float to be stored on aligned

Casting between primitive type pointers

寵の児 提交于 2019-11-30 05:51:40
问题 Is the following well-defined: char* charPtr = new char[42]; int* intPtr = (int*)charPtr; charPtr++; intPtr = (int*) charPtr; The intPtr isn't properly aligned (in at least one of the two cases). Is it illegal just having it there? Is it UB using it at any stage? How can you use it and how can't you? 回答1: First, of course: the pointer is guaranteed to be aligned in the first case (by §5.3.4/10 and §3.7.4.1/2), and may be correctly aligned in both cases. (Obviously, if sizeof(int) == 1 , but