undefined-behavior Questions
2
Solved
I was reviewing a code I proposed to initialize a std::array at compile-time for non-default-constructible objects: https://mcmap.net/q/905632/-idiom-for-initializing-an-std-array-using-a-generator...
Spoke asked 8/10 at 9:48
3
Solved
In [conv.lval] p3.4, the result of lvalue-to-rvalue conversion is described as follows:
Otherwise, the object indicated by the glvalue is read ([defns.access]), and the value contained in the obje...
Darcee asked 5/6 at 5:34
5
Solved
I'm having a disagreement with some co-workers over the following code:
int foo ( int a, int b )
{
return b > 0 ? a / b : a;
}
Does this code exhibit undefined behavior?
EDIT: The disagreem...
Niphablepsia asked 21/10, 2016 at 9:9
2
I was confused by the following paragraph about type aliasing from cppreference (source):
Whenever an attempt is made to read or modify the stored value of an object of type DynamicType through a g...
Graiae asked 20/5, 2021 at 13:24
1
Consider the following C code:
const int array[100];
int i = 0;
int x;
int get_next(void) {
return array[i++];
}
int foo(void) {
return get_next() + get_next();
}
Assuming that i == 0 when foo...
Haywood asked 31/5 at 3:20
2
Solved
Question
Does the following program have undefined behavior?
#include <iostream> // std::{ostream, streambuf}
// The streambuf ctor is protected so we need a wrapper to create one.
struct my...
Burrill asked 22/5 at 4:1
5
Solved
I've checked myself, I wrote a program like this
int main() {
int i;
cout << i;
return 0;
}
I ran the program a few times and the result was same all the time, zero.
I've tried it in C an...
Frequentation asked 11/5, 2015 at 16:3
6
One of the examples of undefined behavior from the C standard reads (J.2):
— An array subscript is out of range, even if an object is apparently accessible with the
given subscript (as in the l...
Segura asked 22/9, 2010 at 3:15
4
Solved
int i = i;
int main() {
int a = a;
return 0;
}
int a = a surely has undefined behaviour (UB), and more details on it is in
Is reading an uninitialized value always an undefined behaviour? Or ...
Eldoraeldorado asked 15/6, 2021 at 2:25
3
Solved
Infinite loops without side effect are undefined behaviour. See here for the example from cppreference. Much simpler example:
int foo() {
while(true) {}
return 42;
}
Now consider the almost eq...
Judie asked 17/4, 2019 at 19:23
1
Solved
C++26 has introduced erroneous behavior in addition to undefined, unspecified, and implementation-defined behavior (see Undefined, unspecified and implementation-defined behavior). How is this new ...
Equi asked 28/3 at 14:0
2
Does undefined behaviour retroactively mean that earlier visible side-effects aren't guaranteed?
In C++, a compiler can assume that no UB will happen, affecting behaviour (even visible side-effects like I/O) in paths of execution that will encounter UB but haven't yet, if I understand the phra...
Wun asked 20/9, 2023 at 21:48
3
Solved
I created some utilities which
help me to handle the management of a DinamicList. In the section that I
use to handle the removing of a element in a list, if there is
a element added that is stor...
Americanism asked 28/11, 2018 at 0:38
1
I want to know how to use std::bit_cast in a well-defined way, noticeably in presence of indetermined bits. When is std::bit_cast usage defined behavior, when is it undefined behavior?
Thus I need ...
Brant asked 13/2 at 14:30
8
Solved
Unsigned integer overflow is well defined by both the C and C++ standards. For example, the C99 standard (§6.2.5/9) states
A computation involving unsigned operands can never overflow,
because ...
Ledger asked 12/8, 2013 at 20:4
3
It is undefined behavior in C to perform arithmetic comparison on two unrelated pointers, that is, two pointers that don't point to the same array or object.
int a, b;
bool ub = &a < &b;...
Dues asked 1/2 at 14:33
8
Solved
Let's say I define a following C++ object:
class AClass
{
public:
AClass() : foo(0) {}
uint32_t getFoo() { return foo; }
void changeFoo() { foo = 5; }
private:
uint32_t foo;
} aObject;
The o...
Inheritance asked 22/2, 2013 at 8:51
8
Solved
In C++ there are a lot of ways that you can write code that compiles, but yields undefined behavior (Wikipedia). Is there something similar in C#? Can we write code in C# that compiles, but has und...
Keavy asked 7/12, 2009 at 15:15
4
Solved
First, to clarify, I am not talking about dereferencing invalid pointers!
Consider the following two examples.
Example 1
typedef struct { int *p; } T;
T a = { malloc(sizeof(int) };
free(a.p); /...
Inshrine asked 10/6, 2013 at 13:17
1
Solved
With GCC 13.2, the output of the following code depends on the optimization level:
#include <ctype.h>
#include <stdio.h>
char *SkipAName(char *s) {
if (('A' <= *s && *s <...
Coextensive asked 2/11, 2023 at 5:37
3
The C Standard specifies how finite floating point values are converted upon storing them into integer types, but it does not seem to specify the behavior for non finite values:
6.3.1.4 Real float...
Villous asked 29/10, 2023 at 15:54
3
Solved
I have the following code:
uint8_t buffer[16];
uint8_t data[16];
uint8_t buffer_length = 16;
uint8_t data_length = 0;
memcpy(buffer + buffer_length, data, data_length);
memcpy should be a no-op, ...
Savory asked 26/9, 2023 at 4:46
3
Solved
If a C program has undefined behavior, anything can happen. Therefore compilers may assume that any given program does not contain UB. So, suppose our program contains the following:
x += 5;
/* Do ...
Scuppernong asked 18/9, 2023 at 22:45
2
Yesterday I heard this in a talk from David Stone
Prior to C++20 it was not possible to implement std::vector, all vector implementations, if they are written in C++, had undefined behavior.
But ...
Tornado asked 13/9, 2023 at 5:44
1
Solved
Is there a guaranteed (not implementation-defined!) way to check for pointer alignment?
The most common way to query pointer alignment seems to be:
convert to integer
check whether the integer is ...
Threemaster asked 13/9, 2023 at 20:35
1 Next >
© 2022 - 2024 — McMap. All rights reserved.