[Bug c++/103843] Direct call to Desctructor is optimized out
georgii.shagov@be-tse.de
gcc-bugzilla@gcc.gnu.org
Mon Dec 27 12:03:20 GMT 2021
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=103843
Georgii.Shagov <georgii.shagov@be-tse.de> changed:
What |Removed |Added
----------------------------------------------------------------------------
Resolution|INVALID |FIXED
--- Comment #5 from Georgii.Shagov <georgii.shagov@be-tse.de> ---
> No, the destructor is not just a function in C++, it terminates the object
> being alive (not de-allocated though). You can then reuse the space for
> another object, either the same type or a different type by using the
> inplacement new.
>
> > Why the content of the class had been re-initialized?
>
> It was not re-initialized rather the store was removed.
>
> > IMU: there should be not such obvious difference between optimized and
> > non-optimized code
>
> why not, it is undefined code.
I really appreciate and value your Reply, Andrew.
I have modified the code a little bit:
#include <iostream>
class S {
public:
S() = default;
~S() { i=10; }
void foo() { this->~S(); }
int getI() const { return i; }
private:
int i{100};
};
int main()
{
S s;
do {
std::cout << "Before foo: " << s.getI();
s.foo();
std::cout << "; After: " << s.getI() << std::endl;
} while (false);
return 0;
}
$g++ -O3 ./d.cpp
$./a.out
Before foo: 100; After: 0
$g++ -O0 ./d.cpp
$./a.out
Before foo: 100; After: 10
> No, the destructor is not just a function in C++, it terminates the object
> being alive (not de-allocated though).
I do understand your point, yet I would not feel comfortable in sharing one.
- It is not clear: what function/code has reset S::i value to 0 after a Direct
call to Destructor?
- Why 0? :-?
- In order to avoid misleading I have allocated the object on the stack as you
can see. So, no-memory mgmt is not involved
- Does it mean gcc implants some code, being called after Destructor call,
resetting the content of the class Instance to 0? ::memset??
- Is there any agreement unto this? I would be happy to learn more about this.
Thnx in advance,
Yours truly, George
More information about the Gcc-bugs
mailing list