0
Does the objects we introduce in class deletes automatically after execution
5 odpowiedzi
+ 1
This depends on where in the memory you allocate your class instance.
There are three basic places where a class instance can be created.
1.) in the static space
2.) on the stack
3.) on the heap
If you allocate your class instance in the static space or on the stack, the class instance its being cleaned up automatically for you.
If you allocate it on the heap, you have to clean it up after usage or otherwise you will get so-called "memory leaks". This, btw does not only apply to class instances but for *all* instances (float, double, ...)
Here's an example for the three places:
class A {};
A a; // in the static space
int main() {
A b; // on the stack
static A c; // in the static space
A* d = new float; // on the heap
delete d;
}
Side note: global and static variables are of fixed size and usually stored in the executable therefore they do not have to be allocated at runtime, they exist already after the operating system loaded the executable. Therefore they do not have to be cleaned up.
+ 1
Its all about scope......as soon as the object goes out of scope unless its static....the object disallocates
+ 1
Hi Nisag, I have to admit that I'm not a beginner in C++. I did SoloLearn C++ to test my skills and have some fun :-) I have about 10+ years of experience in C++, Java, python and have been working as a researcher and educator in computer science. Nowadays I work as a freelancer and have a startup with a buddy of mine.
So it's not a course you can take or a book you can read. It's a longer commitment. Still my answer involves only knowledge I acquired as a student in a system software lecture.
Nevertheless, if you still have fun learning and keep on working on your professional skill everyone can get there. :-)
0
Thanks stefan.....how r u so advanced In c++..where did u learn it from..
0
Yes modern compilers implicitly include destructors in ur source code even if u don't mention it explicitly so objects gets deleted