explicit-destructor-call

Destructor call for scalar types & undefined behavior [duplicate]

故事扮演 提交于 2021-02-08 02:58:34
问题 This question already has an answer here : Pseudo-destructor call does not destroy an object (1 answer) Closed 5 years ago . I just wrote following program & it compiles & runs fine. (see live demo here.) #include <iostream> typedef int T; int main() { int a=3; std::cout<<a<<'\n'; a.~T(); std::cout<<a; return 0; } Why the program compiles fine? If I am not wrong scalar types don't have constructor and destructor in C++ . So, is this program well defined? Does explicit call to destructor

Destructor call for scalar types & undefined behavior [duplicate]

走远了吗. 提交于 2021-02-08 02:58:09
问题 This question already has an answer here : Pseudo-destructor call does not destroy an object (1 answer) Closed 5 years ago . I just wrote following program & it compiles & runs fine. (see live demo here.) #include <iostream> typedef int T; int main() { int a=3; std::cout<<a<<'\n'; a.~T(); std::cout<<a; return 0; } Why the program compiles fine? If I am not wrong scalar types don't have constructor and destructor in C++ . So, is this program well defined? Does explicit call to destructor

Does encapsulated char array used as object breaks strict aliasing rule

风格不统一 提交于 2020-01-04 03:03:21
问题 Do the following class break the strict aliasing rule: template<typename T> class store { char m_data[sizeof(T)]; bool m_init; public: store() : m_init(false) {} store(const T &t) : init(true) { new(m_data) T(t); } ~store() { if(m_init) { get()->~T(); } } store &operator=(const store &s) { if(m_init) { get()->~T(); } if(s.m_init) { new(m_data) T(*s.get()); } m_init = s.m_init; } T *get() { if (m_init) { return reinterpret_cast<T *>(m_data); } else { return NULL; } } } My reading of a standard

Is it allowed to call destructor explicitly followed by placement new on a variable with fixed lifetime?

最后都变了- 提交于 2019-12-18 04:31:35
问题 I know that calling destructor explicitly can lead to undefined behavior because of double destructor calling, like here: #include <vector> int main() { std::vector<int> foo(10); foo.~vector<int>(); return 0; // Oops, destructor will be called again on return, double-free. } But, what if we call placement new to "resurrect" the object? #include <vector> int main() { std::vector<int> foo(10); foo.~vector<int>(); new (&foo) std::vector<int>(5); return 0; } More formally: What will happen in C++

When should I prevent implicit destruction? How does it work?

南楼画角 提交于 2019-12-14 04:01:25
问题 I know that I can declare a destructor =delete or private in order to prevent the program from implicitly deleting the object at the end of scope. I also know that if it's private, I can have a member function that can explicitly call the destructor whenever I call it: void kill() { this–>~A(); } My questions are: Why would I ever want to prevent implicit destruction? Please give an example What would =delete do? Does it make sure the destructor never runs? So the object will exist forever

Do we need to explicitly call the destructor for the “simple POD classes” allocated with “placement new”?

社会主义新天地 提交于 2019-11-30 16:51:53
问题 Here by "simple", I mean a class with non-virtual empty destructor or POD type. Typical example: char buffer[SIZE]; T *p = new(buffer) T; ... p->~T(); // <---- always ? What happens if we don't call the explicit destructor on p ? I don't think it is undefined behavior or memory leak. Is there any problem with reusing buffer ? 回答1: For a POD-type or a class with a trivial destructor: no. The lifetime of the object will end when the storage for the object is released or reused. You don't have

Is it allowed to call destructor explicitly followed by placement new on a variable with fixed lifetime?

a 夏天 提交于 2019-11-29 05:32:09
I know that calling destructor explicitly can lead to undefined behavior because of double destructor calling, like here: #include <vector> int main() { std::vector<int> foo(10); foo.~vector<int>(); return 0; // Oops, destructor will be called again on return, double-free. } But, what if we call placement new to "resurrect" the object? #include <vector> int main() { std::vector<int> foo(10); foo.~vector<int>(); new (&foo) std::vector<int>(5); return 0; } More formally: What will happen in C++ (I'm interested in both C++03 and C++11, if there is a difference) if I explicitly call a destructor

Is it legal to call member functions after an object has been explicitly destroyed but before its memory was deallocated?

南笙酒味 提交于 2019-11-29 02:31:44
问题 I have this code: struct data { void doNothing() {} }; int main() { data* ptr = new data(); ptr->~data(); ptr->doNothing(); ::operator delete(ptr); } Note that doNothing() is being called after the object has been destroyed but before its memory was deallocated. It looks like "object lifetime" has ended however the pointer still points to proper allocated memory. The member function does not access any member variables. Would member function call be legal in this case? 回答1: Yes, in the case

why is the destructor call after the std::move necessary?

别说谁变了你拦得住时间么 提交于 2019-11-28 06:51:10
In The C++ programming language Edition 4 there is an example of a vector implementation, see relevant code at the end of the message. uninitialized_move() initializes new T objects into the new memory area by moving them from the old memory area. Then it calls the destructor on the original T object, the moved-from object. Why is the destructor call necessary in this case? Here is my incomplete understanding: moving an object means that the ownership of the resources owned by the moved-from object are transferred to the moved-to object. The remainings in the moved-from object are some

After an object is destroyed, what happens to subobjects of scalar type?

谁说我不能喝 提交于 2019-11-27 08:07:37
问题 Consider this code (for different values of renew and cleanse ): struct T { int mem; T() { } ~T() { mem = 42; } }; // identity functions, // but breaks any connexion between input and output int &cleanse_ref(int &r) { int *volatile pv = &r; // could also use cin/cout here return *pv; } void foo () { T t; int &ref = t.mem; int &ref2 = cleanse ? cleanse_ref(ref) : ref; t.~T(); if (renew) new (&t) T; assert(ref2 == 42); exit(0); } Is the assert guaranteed to pass? I understand that this style is