C++ Programming

Should you explicitly call a destructor in C programming?


Top Answer
User Avatar
Wiki User
2012-07-19 14:05:13
2012-07-19 14:05:13

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

Related Questions

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.

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.

until the object goes out of scope. or until u call a 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.

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.

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.

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

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.

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

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.

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.

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.

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

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.

Nothing, you misread something. In C, when you call a function, parameters passed by-value.

You cannot call a destructor directly. They are called automatically when an object falls from scope.delete is used in conjunction with the newoperator (similar to the way free is used in conjunction with malloc, calloc and realloc). When deleting an object instantiated with the new operator, the object's destructor is called automatically. Object references are deleted when they fall from scope (references can never be NULL while they are still in scope, and therefore cannot be deleted while in scope).

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.

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

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

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.

you should call it kitty or....................

al·lu·sionəˈlo͞oZHən/nounan expression designed to call something to mind without mentioning it explicitly; an indirect or passing reference.

you should call your piano Bob

Another way of defining assembler is to call it chip set programming. Different chips set have different protocols but what you are doing is programming the hardest way you can

Copyright ยฉ 2020 Multiply Media, LLC. All Rights Reserved. The material on this site can not be reproduced, distributed, transmitted, cached or otherwise used, except with prior written permission of Multiply.