612 questions
- Bountied 0
- Unanswered
- Frequent
- Score
- Trending
- Week
- Month
- Unanswered (my tags)
4
votes
2
answers
124
views
C99: Is it UB to access the first member of a struct via a pointer of another struct type when the first member item is identical?
If we have similar structs (Base, Derived1 and Derived2) that both have a first element of type int:
typedef struct stBase {
int type;
} Base;
typedef struct stDerived1 {
int type; // same ...
4
votes
3
answers
243
views
How undefined behavior of strict aliasing could happen if data is only read?
I'am verifying strict aliasing in C with the following code:
#include <stdio.h>
int main() {
int x = 42;
float *pf = (float *)&x; // strict aliasing violation
//*pf = 3.14;...
4
votes
6
answers
215
views
Correct interpretation of clause 6.5 Expressions in the draft C standard
I’m reading draft WG 14/N 3088, paragraph 6.5 Expressions, paragraph 7:
7 An object shall have its stored value accessed only by an lvalue
expression that has one of the following types:98)
— a type ...
2
votes
0
answers
227
views
Difficulty reading volatile memory
I have code that has worked well when compiled with gcc but fails to work correctly when compiled with clang. I tracked down the issue to a read of volatile memory (on a microcontroller). I found by ...
5
votes
1
answer
243
views
How do strict aliasing rules apply to pointers-to-pointers-to-characters (and functions like `strtol`)?
How do strict aliasing rules apply to pointers-to-pointers-to-characters? For example, does the following contain undefined behavior? (godbolt)
#include <iostream>
#include <cstdlib>
long ...
3
votes
3
answers
165
views
Creating a possibly unaligned pointer, but the location of the undefined behavior is not clear
In the C norm, the following rule:
C23 6.3.2.3 § 7
A pointer to an object type may be converted to a pointer to a different object type. If the resulting pointer is not correctly aligned for the ...
5
votes
3
answers
234
views
Is it allowed to use unions to modify parts of an object?
According to GCC's documentation and the various answers I read on Stack Overflow, it is allowed to use unions for type punning in C, like:
union a_union {
int i;
double d;
};
int f() {
union ...
9
votes
1
answer
238
views
std::launder when there are two objects in the same memory location
I was doing some reading on std::launder, and I thought of a scenario that the standard didn't seem to address.
cppreference.com defines std::launder(T *p) like this:
Formally, given
the pointer p ...
1
vote
1
answer
151
views
Tagging pointers & well-defined ways to create a misaligned pointer
C11 6.3.2.3p7 says:
A pointer to an object type may be converted to a pointer to a
different object type. If the resulting pointer is not correctly
aligned 68) for the referenced type, the behavior ...
5
votes
4
answers
430
views
Making a C++ struct that has the memory layout of an array
I have a set of structs that looks something like
struct A
{
int x;
int y;
};
struct B
{
int x;
int y;
int z;
};
struct Top
{
A a;
B b;
int* getPtr() { return &a.x; }
}...
3
votes
2
answers
195
views
Flexible array member issues with alignment and strict aliasing
struct Node {
Node *left;
Node *right;
int height;
char data[];
};
This is how I used to define my data structure nodes, I find it very useful because I can embed the data directly in the ...
6
votes
1
answer
127
views
Can a real floating-point type alias a complex floating-point type in C?
According to the C Standard:
Each complex type has the same representation and alignment requirements as an array
type containing exactly two elements of the corresponding real type; the first ...
3
votes
1
answer
142
views
Need for an intermediate struct in a type punning example with `std::bit_cast`
I was reading this excellent material about the strict aliasing rule: https://stackoverflow.com/a/51228315/21691539 or https://gist.github.com/shafik/a956a17d00024b32b35634eeba1eb49e
But in the ...
1
vote
1
answer
81
views
Strict aliasing violation in C [duplicate]
Does the following violate strict aliasing rule in C?
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
int ialignment_of(double *p)
{
return (int)(((uintptr_t) p) % ...
3
votes
1
answer
178
views
clang/gcc assume that a pointer into a member may alias another member of a different type?
It looks like in the following example the compiler assumes that the pointer to double passed to bar() may alias the integer member a:
struct A {
int a;
double b;
};
void bar(double*);
int ...