`

c++ - c++ explicitly call the destructor

    博客分类:
  • c++
c++ 
阅读更多

 

Destructor server primarily to relinquish resources acquired either within the constructor or during the lifetime. the lifetime of the class object, again such as freeing a mutal exclsion lock or deleting memory allocated through operator new.

 

 

 

destructor is implicitly called when the object is out of scope, such as the local object goes out of scope, or when program exit, the global object destructors are called. or explicitly when you call the delete operator. 

 

given the following code. 

 

void destructor_life_cycle_test()
{
	Account local("Anna live Plurablle", 1000);
	Account &loc_ref = global;
	auto_ptr<Account> pact(new Account("stephen Dedalus"));

	{
		Account local_too("Stephen Hero");
		// the local_too will destroy before exit the current local space. 
	}
	// the auto_ptr will destruct here

}

 

can you see which destructor is called at different stage?

 

 

So, why do we need to call explicitly the destructor, given the following code, 

 

given the following example. 

 

 

	char *arena = new char[sizeof Image];

	Image * ptr = new (arena) Image("Quasimodo");

	Image *ptr = new (arena) Image("Esmerelda");

	//you cannot delete the ptr, because it will reclaim the memory in addition to call the destructor
	delete ptr;

	// but instead, you should do something like this:
	ptr->~Image();

	// and when  you finish using the arena memory , you can free the location by this 
	delete arena;
	// it will not call the destructor, because arena is of type char *

 

it is obvious that we might need explictly call the destructor when we are manually managing the memory and allocation, we need to call the destructor to explicitly manage the life cycle of the object ourselves.

 

 

 

 

 

分享到:
评论

相关推荐

    Google C++ Style Guide(Google C++编程规范)高清PDF

    Style, also known as readability, is what we call the conventions that govern our C++ code. The term Style is a bit of a misnomer, since these conventions cover far more than just source file ...

    Functional Programming in C++

    Functional Programming in C++ teaches developers the practical side of functional programming and the tools that C++ provides to develop software in the functional style. This in-depth guide is full ...

    NIST SP800-116.pdf

    [HSPD-12] explicitly requires the use of PIV credentials “in gaining physical access to Federally-controlled facilities and logical access to Federally-controlled information systems.” The PIV Card ...

    3D Simulation of the IFRF Industrial Pulverized-coal Furnace

    explicitly. This tutorial uses the mixture fraction/PDF model with the k-epsilon turbulence model and P-1 radiation model. If you have not used these models before, it would be helpful to refer to the...

    Effective C++(第三版)

    explicitly disallow the use of compiler-generated functions you do not want. 条款07:为多态基类声明virtual析构函数 declare destructors virtual in polymorphic base classes. 条款08:别让异常逃离析构函数...

    wget-1.11.4-1

    --no-proxy explicitly turn off proxy. -Q, --quota=NUMBER set retrieval quota to NUMBER. --bind-address=ADDRESS bind to ADDRESS (hostname or IP) on local host. --limit-rate=RATE limit download rate...

    LEAD.rar_#classifier_#matlab_#multi-label_The Network_bayesian n

    This toolbox contains programs for the multi-label classifier which explicitly exploits label dependency with Bayesian network structure.

    k-nearest-neighbors-from-global-to-local

    1. **Formulating Optimal Weights**: The authors formulate a notion of optimal weights that explicitly balances the bias and variance. This formulation enables them to find the optimal weights and \( k...

    BURNINTEST--硬件检测工具

    test phase" was selected the DTR and RTS lines would be explicitly disabled to prevent any toggling of these lines. Previously these where enabled, but not explicitly toggled. Release 5.3 build ...

    i2c 总线协议规范

    need not be specified explicitly. ?The Fast-mode is added. This allows a fourfold increase of the bit rate up to 400 kbit/s. Fast-mode devices are downwards compatible i.e. they can be used in a ...

    demonstrating and evaluating Java Plug-in

    The examples on this page provide demonstration ... Hence, the HTML of each page explicitly specifies the use of the JavaTMPlug-in rather than the default Java virtual machine contained in your browser.

    squashfs2.2-r2.tar.gz

    The Dest argument is the destination where the squashfs filesystem will be written. This can either be a conventional file or a block device. If the file doesn't exist it will be created, if it does ...

    AndroidStudio中使用NDK编译器笔记

    # explicitly specify the standard system locations here. find_library( # Sets the name of the path variable. android-lib # Specifies the name of the NDK library that # you want CMake to locate. ...

    在随处都可以进行zoom的demo

    The standard usage of akZoom is "on demand", this means you have to call akZoom explicitly after plotting something like shown in the akZoom_examples.m file. However, if you are satisfied with the ...

    FVFOM: A three dimensional semi-implicit unstructured grid ocean model

    terms in the momentum equations are discretized explicitly by integral method. The partial cell method is used for resolving topography, which let the model can be better represent irregular ...

    matlab虚线图代码-callGraph:一种多语言工具,用于解析函数定义和调用的源代码

    c/c++/java,因为它们复杂多样的语法需要繁重的机器。 Usage: callGraph If your script calls helper modules, and you want the call graph to display the modules' functions, list the modules explicitly on...

    .Net架构之美

    including the modeling techniques that ensure your architecture fully and explicitly addresses user requirements. They deftly cover essential concepts (UML, design patterns), the core system &#40;...

    人脸识别 L-Softmax

    one of the most common used supervision components in convolutional neural networks (CNNs). Despite its simplicity, popularity and excellent performance, the component does not explicitly encourage ...

    LCTF软件备份VariSpec™ Liquid Crystal Tunable Filters

    This package provides a set of functions to control the VariSpec filter, which may be called from C or C++ programs. It incorporates all aspects of the filter communication, including low-level serial...

    jwts-not-safe-e-book.pdf

    2. **Option 2: Make the Database Lookup So Fast That the Additional Call Won’t Matter:** - Utilizing high-performance databases like Redis can significantly reduce the time required for database ...

Global site tag (gtag.js) - Google Analytics