undefined-behavior Questions
1
Solved
Are these expressions defined?
int x = 5, y;
y = ++(int){++x};
//or
y = (int){x++}++;
and (I cant find any reason why not to be defined)
int x = 5;
x = ++(int){++x};
//or
x = (int){x++}+...
Moulden asked 11/9, 2023 at 12:58
3
Solved
In C++ loops such as
for(;;) {}
used to be undefined behavior prior to P2809R3. Trivial infinite loops are not Undefined Behavior, whereas they are not in C(?).
In the aforementioned proposal, it ...
Demars asked 23/8, 2023 at 10:22
3
Solved
Does the definition int a = 0, b = a++, c = a++; have defined behavior in C?
Or almost equivalently, does the comma in an object definition introduce a sequence point as for the comma operator in e...
Schaper asked 9/8, 2023 at 12:10
1
Solved
I was starting from researching the question "is &((T*)NULL)->member UB in C?". This is an example in my textbook, which introduced the old implementation of offsetof.
I know that ...
Upstart asked 13/8, 2023 at 15:27
2
Solved
After P0593R6 ('Implicit creation of objects for low-level object manipulation') was accepted in C++20, C++23 will get std::start_lifetime_as() which 'completes the functionality proposed in [P0593...
Shadshadberry asked 10/6, 2023 at 10:43
2
Solved
I have three free functions: F0, F1 and F2. F0 calls F1, which in turn calls F2.
F0 and F2 are C++ functions, where as F1 is a C function. F2 is exposed to F1 via: extern "C"
The code for each of...
Proponent asked 27/4, 2018 at 20:9
15
Solved
#include <stdio.h>
int main(void)
{
int i = 0;
i = i++ + ++i;
printf("%d\n", i); // 3
i = 1;
i = (i++);
printf("%d\n", i); // 2 Should be 1, no ?
volatile int u = 0;
u = u++ + ++u;...
Jacobinism asked 4/6, 2009 at 9:17
3
#include <cstdint>
#include <iostream>
int main() {
uint32_t i = -64;
int32_t j = i;
std::cout << j;
return 0;
}
Most compilers I've tried will create programs that output...
Insistent asked 25/9, 2013 at 1:5
1
Solved
Preamble
This is a description of what I am trying to do with the code, skip to the next section to see the actual issue.
I want to use coroutines in an embedded system, where I can't afford too ma...
Inflexed asked 9/6, 2023 at 15:23
1
Is placement new of derived object within a vector / array defined to be base object legal
#include <iostream>
#include <memory>
#include <vector>
struct A
{
virtual ~A()
{
s...
Ribwort asked 1/6, 2023 at 9:24
4
I stumbled upon the following code snippet:
#include <iostream>
#include <string>
using namespace std;
class First
{
string *s;
public:
First() { s = new string("Text");}
~First() ...
Hayashi asked 12/11, 2018 at 12:31
3
Solved
The C++ standard comes with an stunning number of definitions for unclear1 behavior which mean more or less the same with subtle differences. Reading this answer, I noticed the wording "the program...
Biocellate asked 4/3, 2014 at 18:45
2
Let S be a struct type which contains a character array data which has the maximum alignment and a fixed size. The idea is that S is able to store any object of type T whose size does not exceed th...
Wastage asked 15/4, 2018 at 11:46
3
I am surprised to accidentally discover that the following works:
#include <iostream>
int main(int argc, char** argv)
{
struct Foo {
Foo(Foo& bar) {
std::cout << &bar <&...
Greeley asked 16/9, 2015 at 12:20
2
What undefined behavior does Python have? Undefined meaning not in the specification of the language. The only example I know of is modifying a sequence while iterating through it. Before learning ...
Doorstep asked 8/5, 2020 at 23:30
5
Solved
Let's say I've got a function that accepts a 64-bit integer, and I want to call
it with a double with arbitrary numeric value (i.e. it may be very large in
magnitude, or even infinite):
void DoSom...
Feudal asked 15/9, 2014 at 22:25
9
Solved
Updated, see below!
I have heard and read that C++0x allows an compiler to print "Hello" for the following snippet
#include <iostream>
int main() {
while(1)
;
std::cout << "Hello...
Canard asked 28/8, 2010 at 21:32
2
Solved
Can I use a std::array<int, N> to alias parts of a int[] without invoking UB?
https://en.cppreference.com/w/cpp/container/array
"This container is an aggregate type with the same semanti...
Dasha asked 19/12, 2022 at 21:16
2
Solved
Assuming X and Y are suitable types for such usage, is it UB to use std::start_lifetime_as<X> on an area of memory in one thread as one type and use std::start_lifetime_as<Y> on the exa...
Payroll asked 20/12, 2022 at 4:48
2
The C++20 standard says in Function Call, 7.6.1.3/8:
The initialization of a parameter, including every associated value computation and side effect, is indeterminately sequenced with respect to t...
Attrition asked 7/2, 2022 at 3:1
3
Solved
Reading https://en.cppreference.com/w/cpp/language/reinterpret_cast I wonder what are use-cases of reinterpret_cast that are not UB and are used in practice?
The above description contains many cas...
Zionism asked 27/10, 2022 at 5:10
4
Solved
I have a report, unconfirmed by me but from a reliable source, that the code
qsort(a, n, sizeof *a, cmpfunc);
is compiled by a modern version of gcc as if it had been written
if(n == 0)
__b...
Gandhi asked 26/10, 2022 at 12:29
1
#include <iostream>
using namespace std;
const int BUFSIZE = 1 << 20;
char padded_buffer[64 + BUFSIZE + 64];
char* buffer = padded_buffer + 64;
int main()
{
buffer[-1] = '?';
// is ...
Seraphina asked 19/10, 2022 at 15:25
4
Solved
I have an int x. For simplicity, say ints occupy the range -2^31 to 2^31-1. I want to compute 2*x-1. I allow x to be any value 0 <= x <= 2^30. If I compute 2*(2^30), I get 2^31, which is an i...
Ryals asked 17/10, 2022 at 20:5
1
Solved
I recently came across a weird double-free bug in a program when capturing a shared_ptr in a lambda. I was able to reduce it this the following minimal example:
#include <memory>
#include <...
Gertrudgertruda asked 15/10, 2022 at 14:12
© 2022 - 2024 — McMap. All rights reserved.