« first day (2279 days earlier)      last day (557 days later) » 

lb0
2:54 PM
Hi guys, I am looking at some high stack usage of some code (gcc, via -fstack-usage) .. and I'm actually surprised that a simple object reinitialization like

a = {}; // (assume we got a "A& a" from somewhere

seems to cause temporary object first being created on the stack, and then copy-assigned, causing some sizeof(A) stack usage (even with gcc -O3, C++14).

Why isn't the compiler automatically doing it similar to a placement new like
new (&a) A(); (yeah, call destructor before, https://stackoverflow.com/questions/2166099/calling-a-constructor-to-re-initialize-object frowns upon that "h
 
nwp
Because operator= requires 2 objects to work with, a source and a destination. In special cases the compiler may be able to optimize that operator out, but in general it cannot.
In general a default constructor and the assignment operator can do the craziest things.
Adding a function A::clear() that works with a single object is the obvious solution, but I understand that you're reluctant to manually reset every field and would prefer to use a defaulted object instead, but I don't think that is possible without making said defaulted object.
Sadly we still lack the reflection tools to iterate over members and to read default values.
void A::clear() {
    static const A defaultA;
    *this = defaultA;
}
This gives you some of the convenience without causing stack issues.
 
lb0
Yes, I agree and a dedicated clear method also came to mind... but of course It's within something templated, so there are immediately multiple classes that would need my new clear method for that.

So if the intent is get rid of my old object and just reinitialize like default constructor would do it, isn't there anything nicer to
a.~A();
new (&a) A();
and what could go wrong with it? (Apparently the stackoverflow link already says there is nothing nicer .. despite the clearest thing adding a dedicated clear().. )
 
nwp
3:10 PM
It causes issues with references/pointers/iterators. The referenced object is dead, thus all references to it are invalid. The fact that a new object is at the same location doesn't matter, the references are still invalid. Most of the time this UB will not manifest, but if you have const members a compiler is likely to assume said const members will not change, but they may since it's a new object.
struct A {
    const int i = 42;
};

A a{0};
a.~A();
new (&a) A();
std::cout << a.i; //may print 42 or 0 or cause UBSan to abort the program, who knows
Maybe you need to go through an A &ar = &a; to trigger the UB and using a directly is fine, I forgot the exact rules.
 
lb0
Wow, ok, thanks for reminding me of "in general a default constructor and the assignment operator can do the craziest things" and pointing out that UB behaviour!
 
nwp
It's one of those dark corners of C++ that you rarely go into. Bonus fun: std::cout << std::launder(&a)->i; would be fine. I think.
 
@nwp Yeah, I think the code you've shown has defined behavior (though yes, it's obscure enough that it wouldn't be terribly surprising to find a compiler bug this way, and if you did the compiler writers would consider fixing it the lowest priority humanly possible, unless it also affected more sensible code).
 
nwp
3:30 PM
@lb0 You could put a static const T defaultT; in said template. Of course it'd cause a bit of waste if you have multiple templates. Since C++17 you can put template <class T> const T defaultT; globally and then you can use defaultT<T> from all your templates.
 

« first day (2279 days earlier)      last day (557 days later) »