Best Answer

Not to be pedantic, but you cannot call a destructor explicitly. Destructors are implicitly called when an object falls from scope or when you delete a pointer to an object.

Any object created dynamically (with the new keyword) must be deleted as soon as you are finished with it, and before the pointer falls from scope. In this sense, you are explicitly calling the object's destructor, however it's really being called implicitly by you deleting the pointer. It's also good practice to explicitly NULL your pointer immediately after deleting the object it pointed to.

An object reference is destroyed automatically when the reference falls from scope. If you have a pointer to that reference, do not delete the pointer, but do assign it to NULL as soon as possible to prevent any access to the deleted object. If you do delete a pointer to a reference that's still in scope, you will render the reference NULL and a NULL reference will render your program invalid.

User Avatar

Wiki User

โˆ™ 2012-07-19 14:05:13
This answer is:
User Avatar
Study guides
See all Study Guides
Create a Study Guide

Add your answer:

Earn +20 pts
Q: Should you explicitly call a destructor in C programming?
Write your answer...
Related questions

Why use a virtual destructor C plus plus?

It is used in combination with pointers that point to a base class. When the pointer points to a base class, but is assigned to derived object, then a destructor called by the pointer needs to be the correct one. It needs to call the destructor of the derived object, so in this case, the base class destructor needs to be virtual to let the compiler know to call the destructor of the derived object.

When is a destructor called in c plus plus?

Destructor usually called when programs exists. You can call destructor during execution of your program to free memory. If destructor is not called you get what is called memory leak. And if you operate with large volume of data in no time you can run out of memory.

Why constructor and destructor cannot be made static?

The term "destructor" made me believe this question is related to .Net languages. A destructor is to destroy an instance of object. If it is available at static/class level, what is it going to destroy? The entire class, so the class no longer available? Thus, semantically, destructor should be an instance method. Constructor is on the opposite end of the life cycle of an instance. However, in .NET, a static constructor is allowed. Personally, I call this static constructor as a class initialization method. This method will be invoked by the .net framework only once when the class is loaded into the application domain. With the similar concept, there should be a "finalizer" of the class when the class is unloaded out of the application domain. But wait, does a class ever go out of the application domain once it's loaded? Yes, only at the termination of the application! Currently a class cannot be unloaded explicitly in codes and thus no point to have a static finalizer.

What is pure virtual destructor?

There is no such thing as a pure-virtual destructor. Classes can declare destructors to be virtual to ensure base class destructors call their derived class destructors first, but they cannot be declared pure virtual. This would imply the class is abstract and that the destructor must be overridden by all derived classes. But class destructors cannot be overridden. Every class, including an abstract class, must handle its own destruction. The only reason to declare a virtual destructor (but not a pure-virtual destructor) is when the class has one or more virtual methods besides the destructor.

What happens if an exception is throws from an object's destructor?

If a destructor throws an exception, the instance is left in an invalid state. When an exception is thrown, the destructor automatically terminates at the point of the throw, unwinding the call stack until an exception handler is found (if one is provided). However, any resources yet to be released by the destructor, including all the instance's base classes, cannot be destroyed. When writing your own destructors, it is important to never throw an exception. If an exception could be thrown from within your destructor, you must catch it and handle it within the same destructor -- you must not rethrow the exception.

When is an object created and what is its lifetime?

until the object goes out of scope. or until u call a destructor

What is the difference between delete and delete?

delete is used to destroy a single object. It will call the destructor of the object and free the memory. delete[] is used to free the memory which was allocated using new []. delete [] will call the destructor for each object in array and free the memory. ________________ As far as I can tell, they aren't delete bit different.

Difference between super and this in java?

The keyword super is used to explicitly call methods/values from the parent class The keyword this is used to explicitly call methods/values from the current class

You cannot throw an exception from a destructor?

Sure you can, but it's never a good idea. You must never allow an exception to escape from a destructor. To do so would terminate the destructor prematurely, unwinding the call stack in search of an exception handler. This means that any remaining resources consumed by the instance, including its base classes, would be left in an invalid state with no possible way to recover those resources besides terminating the program. Any object that has the potential to throw an exception during its own destruction should always be treated with suspicion; it is a major design flaw.

How do explicitly invoke superclass constructor from a subclass?

Using the super keyword. If you call super() from within your constructor, it will explicitly invoke the superclass version of the constructor.

What do you call a person who is addicted to computer programming?

One thing that you can call a person that is addicted to computer programming is a computer nerd. A computer nerd is always on the computer.

What are the limitations of structured programming?

By itself, structured programming does not support the notion of a function call. This is achieved through an extension of structured programming known as procedural programming. Object-oriented programming extends procedural programming such that data and the functions that operate upon the data can be encapsulated within an object.

What is the code for dish network with a GE universal remote?

You should have an Owner's Manual that came with your remote with programming instructions and codes. If you can't find the codes, you should call the manufacturer, we wouldn't have the codes for their equipment.

What programming language was Call of Duty 5 written in?


What is selective call acceptance?

Programming a telephone to accept only selected callers

How was call of duty created and what programming language used?

c++ and opengl

What object base programming?

Object base programming is the one that in programming code only object is used to call method(function, procedure whatever you call it). Some small amount of primitive data type is also used. Take a dog as object. Dog has name, age. if you want to find out the name and age of the dog, you need to make a method called public getName(){retrun name} etc. and call the same method. while you call the method, you must use the dog object. In this type of programming object carries its properties with it. that is it my frind. good luck

Will you do badly in physics chemistry and engineering if you are good at math but bad at programming?

Physics and engineering in college are almost all math, so you should have no problem there. You may be bad at programming a computer, but perhaps you are not so bad at being a user. For example in electronic or mechanical engineering you will use very sophisticated CAD and math software, but as a user; not really programming as a software engineer does. But I would say not to give up yet on programming. Everyone goes through what I call the "sweaty armpits" stage of programming, but after that most people (especially people who like math) really take to programming.

What is basic interpreter?

An interpreter, in computer programming, is software that takes an accepted syntax, and "interprets" it for the processor, which then executes the results of the interpretation. This is different than a compiler, which directly outputs machine code into a separate binary file (C++ falls in this category), and also different from a script, which is typically run without an integrated, interactive debugging environment (Perl falls in this category). BASIC is a programming language that was designed to be a beginner's programming language, lacking many features that would be found in many "complicated" languages, such as object-oriented programming, template programming, etc. Many other features, such as function calls, were made explicit compared to other similar languages. For example, in most languages, you would simply type the name of a function to execute it, while in BASIC, you would use GOSUB to explicitly call the function.

Why c is call structured programming?

Because you can use programming structures, namely: sequence, selection (if, switch) and repetition (while, for, do-while)

Where can one learn to do database programming?

Database programming can be learned through the local university or through other private courses. Finding information can be as easy as making a call to the university.

What is meant by a system call in operating system?

A "System call" you be a function accessible from a programming language to the base hardware of the computer (eg to get the time).

Is a function call a form of branching in C plus plus?

No. A branch is akin to a goto statement in procedural programming. The code branches off to a new code segment, never to return. A function call is akin to a subroutine in structured programming. When the subroutine is finished, control is returned to the instruction immediately following the function call, just as if the function's code were inline expanded at the call site.

How can you see what dish package you have?

You can either call Dish Network (use the same number you signed up with and the automated system should automatically detect your account) or you can sign up online with Dish and view your bill, programming and equipment.

What do you call programming languages that require you to declare the type of each variable?

Statically typed languages.