3,234 questions
2
votes
0
answers
156
views
Why catch block is ignored, and std::terminate is invoked? [duplicate]
I have a program that works as expected if compiled by GCC, but behaves weirdly after porting to Visual Studio.
The program throws an exception from noexcept(false) destructor (I know that it is a bad ...
2
votes
3
answers
437
views
What are non trivial destructors in C++ used for
I am new to C++, I came from Python where there are constructors but no destructors.
What is the point of them if the memory is automatically freed when the object goes out of scope ?
4
votes
1
answer
166
views
Why destruction order of array elements is not from last to first?
Does Rust have any rules regarding calling drop on arrays? Or vectors?
E.g. in C++ desctruction order of array elements is from last to first (opposite to construction order) while in Rust it seems ...
4
votes
1
answer
198
views
Why no cleanup if a contract precondition fails?
C++26 will introduce function contract specifiers, which are contract assertions associated with a function to express and check that function's pre- and post- conditions.
If enforced, contract ...
17
votes
1
answer
1k
views
100% coverage exit handler that handles all ways a program can exit in C
I have a program where I allocated a lot of data and used multiple libraries. I have a cleanup procedure (can be a function, label, setjmp, or just a section of code) that frees allocated memory and ...
2
votes
3
answers
169
views
Understanding the destructor call in item 8 of Effective C++, Third edition
In Effective C++, by Scott Meyers, item 8, there is a suggestion to wrap (using RAII) an object that has a close method that may throw, to automatically call close in the destructor, but since ...
0
votes
0
answers
58
views
Why the destructor pointer is null in construction vtable?
My code is below:
class GrandParent {
public:
GrandParent() {}
virtual ~GrandParent() {}
int grandparent_data = 100;
};
class Parent1 : virtual public GrandParent {
public:
Parent1() {}...
3
votes
1
answer
276
views
In C++, why does a derived class need to use a scope resolution operator when calling the destructor of its base class?
In C++, why can't a derived class directly and explicitly call the destructor of its base class without using a scope resolution operator?
I've heard that in C++, all destructors are processed by the ...
18
votes
1
answer
179
views
How to implement a destructor for an n-ary graph node that won't cause a stack overflow?
I'm working on modernizing an older C++ code base that contains a simple graph structure. It originally looked like the following:
struct node_value_t {
...
};
struct dag_node {
std::vector<...
9
votes
1
answer
373
views
Cannot match on Result in const fn due to "destructor cannot be evaluated at compile-time" even when value isn't dropped
I tried to match on a generic Result<&mut T, T> inside of a const fn, but the compiler did not let me. It would require being able to drop the value at the end of the scope, which is
...
-1
votes
1
answer
125
views
Does destructor take care of popping member variables from the stack
Take this very simple class. The main() function just creates an object of it. Even though the constructor and destructor don't have an implementation here. My assumption is that the constructor will ...
0
votes
0
answers
73
views
Are pointers to destructors pushed onto the stack. C++ [duplicate]
I am thinking about stack unwinding when an exception is thrown here. In the below simple example, when throw 505 is executed, from what I understand, the stack starts to unwind until it finds the ...
1
vote
0
answers
87
views
Is destruction during member function call undefined behavior? [duplicate]
Is the following undefined behavior or is it well defined?
calling a member function on an object. e.g. mystruct.method()
which triggers the destruction of the object. For example, the object is a ...
0
votes
1
answer
111
views
Inconsistent destructors as seen by gdb and objdump
My code is:
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
class Base
{
public:
Base(uint32_t len)
: len_(len)
{
buf_ = (char *)malloc(len_ * ...
2
votes
1
answer
112
views
I’m curious about how the destructor is overridden in C++ when declared as virtual
The base class and its derived class have different destructor names, so how is overriding possible? Could you explain what happens when a destructor is declared as a virtual function?