Category : undefined-behavior

Sarah Connors is fName and lName, 10000 is loanAmount 12 interestRate and 3 is the number of years for the loan, this is all located in "Lab4Data.txt" #include <iostream> #include <fstream> #include <string> #include <iomanip> #include <cmath> using namespace std; int main() { string fName, lName; double interestRate, loanAmount, monthlyPayment, totalPayed, interestAmount, double numberOfPayments ifstream ..

Read more

In the accepted answer to "Iterator to last element of std::vector using end()–" @barry states: Note that if vector::iterator is just T* (which would be valid), the first form above is ill-formed. The second two work regardless, so are preferable. referring to his code: std::vector<int>::iterator it = –container.end(); std::vector<int>::iterator it = container.end() – 1; std::vector<int>::iterator ..

Read more

I’m certain that this is caused by undefined behaviour, but I can’t figure out what is wrong with my code (I’m not that used to coding in C++). Problem: https://codeforces.com/problemset/problem/609/E I got most of the code from here: https://www.geeksforgeeks.org/kruskals-minimum-spanning-tree-algorithm-greedy-algo-2/ Any help is appreciated as I’m losing my mind about this. Code: #include <bits/stdc++.h> using namespace ..

Read more

An interesting discussion has arisen in the comments to this recent question: Now, although the language there is C, the discussion has drifted to what the C++ Standard specifies, in terms of what constitutes undefined behaviour when accessing the elements of a multidimensional array using a function like std::memcpy. First, here’s the code from that ..

Read more

Consider the following example from [basic.life#8] in the standard (this is the relevant part of it): const C& C::operator=( const C& other) { if ( this != &other ) { this->~C(); new (this) C(other); } return *this; } This is simple enough, but I started to wonder what happens when the class is part of ..

Read more