object-destruction

Using placement new in a container

旧街凉风 提交于 2021-02-11 05:03:38
问题 I just came across some container implementation in C++. That class uses an internal buffer to manage its objects. This is a simplified version without safety checks : template <typename E> class Container { public: Container() : buffer(new E[100]), size(0) {} ~Container() { delete [] buffer; } void Add() { buffer[size] = E(); size++; } void Remove() { size--; buffer[size].~E(); } private: E* buffer; int size; }; AFAIK this will construct/destruct E objects redundantly in Container() and

Object creation and destruction order in C++

扶醉桌前 提交于 2020-01-16 03:50:40
问题 I wrote a simple program to learn more about the order of creating and destructing objects in C++ (using Visual Studio 2015). Here it is: #include <iostream> #include <string> using namespace std; class A { public: A(string name) : name(name) { cout << "A(" << name << ")::constructor()" << endl; } ~A() { cout << "A(" << name << ")::destructor()" << endl; } private: string name; }; class C { public: C(string name, A a) : name(name), a(a) { cout << "C(" << name << ")::constructor()" << endl; }

Object creation and destruction order in C++

只愿长相守 提交于 2020-01-16 03:50:30
问题 I wrote a simple program to learn more about the order of creating and destructing objects in C++ (using Visual Studio 2015). Here it is: #include <iostream> #include <string> using namespace std; class A { public: A(string name) : name(name) { cout << "A(" << name << ")::constructor()" << endl; } ~A() { cout << "A(" << name << ")::destructor()" << endl; } private: string name; }; class C { public: C(string name, A a) : name(name), a(a) { cout << "C(" << name << ")::constructor()" << endl; }

Deleting a class object in java

自作多情 提交于 2020-01-01 18:02:05
问题 I have a class named Point as below: public class Point { public int x; public int y; public Point(int X, int Y){ x = X; y = Y; } public double Distance(Point p){ return sqrt(((this.x - p.x) * (this.x - p.x)) + ((this.y - p.y) * (this.y - p.y))); } protected void finalize() { System.out.println( "One point has been destroyed."); } } I have an object from this class named p as below: Point p = new Point(50,50); I want to delete this object, I searched how to do it, the only solution I found

Good uses of the finalize() method [duplicate]

纵然是瞬间 提交于 2019-12-13 11:35:46
问题 This question already has answers here : Why would you ever implement finalize()? (21 answers) Closed 2 years ago . This is mostly out of curiosity. I was wandering if anyone has encountered any good usage for Object.finalize() except for debugging/logging/profiling purposes ? If you haven't encountered any what would you say a good usage would be ? 回答1: If your Java object uses JNI to instruct native code to allocate native memory, you need to use finalize to make sure it gets freed. 回答2:

C++ destruction order: Calling a field destructor before the class destructor

不羁的心 提交于 2019-12-10 16:47:46
问题 Is there any way to call a field destructor before the class destructor? Suppose I have 2 classes Small and Big , and Big contains an instance of Small as its field as such: class Small { public: ~Small() {std::cout << "Small destructor" << std::endl;} }; class Big { public: ~Big() {std::cout << "Big destructor" << std::endl;} private: Small small; }; int main() { Big big; } This, of course, calls the big destructor before the small destructor: Big destructor Small destructor I need the Small

Why do finalizers have a “severe performance penalty”?

末鹿安然 提交于 2019-11-27 13:32:35
Effective Java says : There is a severe performance penalty for using finalizers. Why is it slower to destroy an object using the finalizers? Because of the way the garbage collector works. For performance, most Java GCs use a copying collector, where short-lived objects are allocated into an "eden" block of memory, and when the it's time for that generation of objects to be collected, the GC just needs to copy the objects that are still "alive" to a more permanent storage space, and then it can wipe (free) the entire "eden" memory block at once. This is efficient because most Java code will

Why do finalizers have a “severe performance penalty”?

空扰寡人 提交于 2019-11-26 16:23:30
问题 Effective Java says : There is a severe performance penalty for using finalizers. Why is it slower to destroy an object using the finalizers? 回答1: Because of the way the garbage collector works. For performance, most Java GCs use a copying collector, where short-lived objects are allocated into an "eden" block of memory, and when the it's time for that generation of objects to be collected, the GC just needs to copy the objects that are still "alive" to a more permanent storage space, and