strict-aliasing

Violating of strict-aliasing in C, even without any casting?

会有一股神秘感。 提交于 2019-12-31 08:36:05
问题 How can *i and u.i print different numbers in this code, even though i is defined as int *i = &u.i; ? I can only assuming that I'm triggering UB here, but I can't see how exactly. (ideone demo replicates if I select 'C' as the language. But as @2501 pointed out, not if 'C99 strict' is the language. But then again, I get the problem with gcc-5.3.0 -std=c99 !) // gcc -fstrict-aliasing -std=c99 -O2 union { int i; short s; } u; int * i = &u.i; short * s = &u.s; int main() { *i = 2; *s = 100;

Violating of strict-aliasing in C, even without any casting?

旧巷老猫 提交于 2019-12-31 08:35:11
问题 How can *i and u.i print different numbers in this code, even though i is defined as int *i = &u.i; ? I can only assuming that I'm triggering UB here, but I can't see how exactly. (ideone demo replicates if I select 'C' as the language. But as @2501 pointed out, not if 'C99 strict' is the language. But then again, I get the problem with gcc-5.3.0 -std=c99 !) // gcc -fstrict-aliasing -std=c99 -O2 union { int i; short s; } u; int * i = &u.i; short * s = &u.s; int main() { *i = 2; *s = 100;

C/C++ strict aliasing, object lifetime and modern compilers

戏子无情 提交于 2019-12-30 06:26:35
问题 I am facing confusion about the C++ strict-aliasing rule and its possible implications. Consider the following code: int main() { int32_t a = 5; float* f = (float*)(&a); *f = 1.0f; int32_t b = a; // Probably not well-defined? float g = *f; // What about this? } Looking at the C++ specs, section 3.10.10, technically none of the given code seems to violate the "aliasing-rules" given there: If a program attempts to access the stored value of an object through an lvalue of other than one of the

Strict aliasing and memory locations

帅比萌擦擦* 提交于 2019-12-30 02:01:13
问题 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

Do I understand C/C++ strict-aliasing correctly?

爱⌒轻易说出口 提交于 2019-12-30 01:25:32
问题 I've read this article about C/C++ strict aliasing. I think the same applies to C++. As I understand, strict aliasing is used to rearrange the code for performance optimization. That's why two pointers of different (and unrelated in C++ case) types cannot refer to the same memory location. Does this mean that problems can occur only if memory is modified? Apart of possible problems with memory alignment. For example, handling network protocol, or de-serialization. I have a byte array,

Strict aliasing and overlay inheritance

試著忘記壹切 提交于 2019-12-29 06:52:09
问题 Consider this code example: #include <stdio.h> typedef struct A A; struct A { int x; int y; }; typedef struct B B; struct B { int x; int y; int z; }; int main() { B b = {1,2,3}; A *ap = (A*)&b; *ap = (A){100,200}; //a clear http://port70.net/~nsz/c/c11/n1570.html#6.5p7 violation ap->x = 10; ap->y = 20; //lvalues of types int and int at the right addrresses, ergo correct ? printf("%d %d %d\n", b.x, b.y, b.z); } I used to think that something like casting B* to A* and using A* to manipulate the

union for uint32_t and uint8_t[4] undefined behavior? [duplicate]

江枫思渺然 提交于 2019-12-28 03:04:06
问题 This question already has answers here : Purpose of Unions in C and C++ (15 answers) Closed 5 years ago . In the comments of this answer it is said that it would be undefined behavior to split up an integer into their bytes using a union like follows. The code given at that place is similar though not identical to this, please give a note if have I changed undefined-behavior-relevant aspects of the code. union addr { uint8_t addr8[4]; uint32_t addr32; }; Up to now I thought this would be a

Accessing pointer variable as a pointer to a different type in C

非 Y 不嫁゛ 提交于 2019-12-24 19:48:22
问题 Is it good practice to access a pointer variable by dereferencing a pointer to a pointer, which points to a different type or void ? Could this break strict aliasing rules? C and C++ have some differences in aliasing rules. In this question we focus on C. The other question considering C++ can be found here. In the following example a double* is accessed as a void* . int create_buffer(void** ptr, ...) { *ptr = malloc(...); ... } int main(void) { double* buffer; // The problematic code is here

Is it legal to modify any data pointer through a void **

自闭症网瘾萝莉.ら 提交于 2019-12-23 09:26:00
问题 Is it legal to access a pointer type through a void ** ? I've looked over the standards quotes on pointer aliasing but I'm still unsure on whether this is legal C or not: int *array; void **vp = (void**)&array; *vp = malloc(sizeof(int)*10); Trivial example, but it applies to a more complex situation I'm seeing. It seems that it wouldn't be legal since I'm accessing an int * through a variable whose type is not int * or char * . I can't come to a simple conclusion on this. Related: Does C have

Do the c++11 strict alias rules allow accessing uint64_t via char *, char(&)[N],even std::array<char, N>& with -fstrict-aliasing -Wstrict-aliasing=2?

你离开我真会死。 提交于 2019-12-22 08:39:11
问题 According to this stackoverflow answer about C++11/14 strict alias rules: If a program attempts to access the stored value of an object through a glvalue of other than one of the following types the behavior is undefined: the dynamic type of the object, a cv-qualified version of the dynamic type of the object, a type similar (as defined in 4.4) to the dynamic type of the object, a type that is the signed or unsigned type corresponding to the dynamic type of the object, a type that is the