Is there any problem with throwing an object constructed on the stack in a try-block by non-const reference, catching it and modifying it, then throwing it by reference to another catch block?
Below is a short example of what I'm refering to.
struct EC {
EC(string msg) { what = msg; }
string where;
string what;
void ap...
#include <iostream>
#include <cmath>
#define max(x,y) (x)>(y)? (x): (y)
int main() {
int i = 10;
int j = 5;
int k = 0;
k = max(i++,++j);
std::cout << i << "\t" << j << "\t" << k << std::endl;
}
...
This code has an interesting bug:
some_struct struct_array1[10] = {0};
some_struct struct_array2[10] = {0}
int i;
for (i = 0;
i < sizeof(struct_array1) / sizeof(struct_array1[0]);
struct_array1[i].value = struct_array2[i++].value = 1)
;
For most compilers, the above code results in setting the "value" field of all str...
Consider the following code:
#include <cstdlib>
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
int main()
{
string myAry[] =
{
"Mary",
"had",
"a",
"Little",
"Lamb"
};
const size_t numStrs = sizeof(myStr)/sizeof(myAry[0]);
v...
This question arose in the comments of a now-deleted answer to this other question. Our question was asked in the comments by STingRaySC as:
Where exactly do we invoke UB? Is it calling a member function through an invalid pointer? Or is it calling a member function that accesses member data through an invalid pointer?
With the ans...
Consider the following code:
struct Calc
{
Calc(const Arg1 & arg1, const Arg2 & arg2, /* */ const ArgN & argn) :
arg1(arg1), arg2(arg2), /* */ argn(argn),
coef1(get_coef1()), coef2(get_coef2())
{
}
int Calc1();
int Calc2();
int Calc3();
private:
const Arg1 & arg1;
const Arg2 & arg2;
// ...
const...
Hi
We are currently developing an application for a msp430 MCU, and are running into some weird problems. We discovered that declaring arrays withing a scope after declaration of "normal" variables, sometimes causes what seems to be undefined behavior. Like this:
foo(int a, int *b);
int main(void)
{
int x = 2;
int arr[5];
...
As far as I understand the wording in 5.2.9 Static cast, the only time the result of a void*-to-object-pointer conversion is allowed is when the void* was a result of the inverse conversion in the first place.
Throughout the standard there is a bunch of references to the representation of a pointer, and the representation of a void poi...
Hi everyone.
I'm writing a class, and this doubt came up. Is this undef. behaviour? On the other hand, I'm not sure its recommended, or if its a good practice. Is it one if I ensure no exceptions to be thrown in the init function?
//c.h
class C{
float vx,vy;
friend void init(C& c);
public:
C();
};
//c.cpp
C::C()
{
in...
I've been messing around with string.replace and I noticed something very odd with Webkit and Firebug's javascript consoles.
I can repeat this behavior in a blank browser window. (Look at the first and last lines)
>>> "/literature?page=".replace(/page=/i, "page=2")
"/literature?page="
>>> "/literature?page=".replace("page=", "page=2"...
As mentioned in this answer simply calling the destructor for the second time is already undefined behavior 12.4/14(3.8).
For example:
class Class {
public:
~Class() {}
};
// somewhere in code:
{
Class* object = new Class();
object->~Class();
delete object; // UB because at this point the destructor call is attempted ag...
Many bad things happened and continue to happen (or not, who knows, anything can happen) due to undefined behavior. I understand that this was introduced to leave some wiggle-room for compilers to optimize, and maybe also to make C++ easier to port to different platforms and architectures. However the problems caused by undefined behavio...
Some old code that I just came across:
MLIST * new_mlist_link()
{
MLIST *new_link = (MLIST * ) malloc(sizeof(MLIST));
new_link->next = NULL;
new_link->mapi = NULL;
new_link->result = 0;
}
This was being called to build a linked list, however I noticed there is no statement:
return new_link;
Even without the return...
I've read a lot, including here on SO that suggests this is a very bad idea in general and that the only thing you can do safely is exit the program. I'm not sure that this is true.
This is for a pooling memory allocator that hands off large allocations to malloc. During pool_free() a pointer needs to be checked it it belongs to a pool ...
When a is undefined, then a || 1 will throw an error, but a = a || 1 will not. Isn't that a little bit inconsistent?
irb(main):001:0> a
NameError: undefined local variable or method 'a' for main:Object
from (irb):1
from c:/ruby/bin/irb:12:in '<main>'
irb(main):002:0> a || 1
NameError: undefined local variable or method...
Regarding division by zero, the standards say:
C99 6.5.5p5 - The result of the / operator is the quotient from the division of the first operand by the second; the result of the % operator is the remainder. In both operations, if the value of the second operand is zero, the behavior is undefined.
C++03 5.6.4 - The binary / opera...
Is there any way to know if you program has undefined behavior in C++ (or even C), short of memorizing the entire spec?
The reason I ask is that I've noticed a lot of cases of programs working in debug but not release being due to undefined behavior. It would be nice if there were a tool to at least help spot UB, so we know there's the...
It is widely known that adding declarations/definitions to namespace std results in undefined behavior. The only exception to this rule is for template specializations.
What about the following "hack"?
#include <iostream>
namespace std_
{
void Foo()
{
std::clog << "Hello World!" << std::endl;
}
using namespace std;
}
int...
Possible Duplicate:
Take the address of a one-past-the-end array element via subscript: legal by the C++ Standard or not?
int array[10];
int* a = array + 10; // well-defined
int* b = &array[10]; // not sure...
Is the last line valid or not?
...
ATL::CComVariant has a handful of assignment operators. What I see in the implementation is that in assignment operators accepting LPCOLESTR, IUnknown* or IDispatch* the first action is to call Clear().
If the operator in invoked in such a way that a member variable of the same object is passed
CComVariant variant;
variant = L"string...