The epitome of C + + memory management

Source: Internet
Author: User

All say C + + memory management is a big hole. It's actually true.

C + + has destructors, and C + + performs two actions whenever an object expires

1. Execute the destructor.

2. Delete all data for objects and objects.

A lot of people will ask, since there is the object to delete the operation, what is the destructor function? I had the same question at first, but! We all know C + + has a magical type, pointers! Pointer he is a 4-byte variable that can even be converted to type int such as printing. Understand this, you know the role of C + + destructor.

Delete keyword, he can be followed by a pointer, or a delete []array such as an array variable, in fact, the meaning is the same, it is looking for a group of addresses, it points to the heap memory released.

Look at a set of class definitions:

#pragmaOnce#include<string>structbattlevalue{ Public:    intATK; intdef; STD::stringname; STD::stringdesc;    Battlevalue (); ~battlevalue ();};classrole{ Public: Role (); Role (intATK, std::stringname); ~Role (); Role (Constrole&Object); FriendvoidShow (role role); Battlevalue*data;};

In this example, we see that the role class has a variable named data, and the type is a pointer to the Battlevalue type. In fact, when we use the object of this role class, we can choose whether to assign a valid value to this data. However, it is important to note that even if you assign a value to data as in a constructor, you assign a 4-byte pointer variable to it, so look at the source file.

#include<iostream>#include"Role.h"usingstd::cout;usingStd::endl; Battlevalue::battlevalue () {cout<<"Battle Init"<<Endl;} Battlevalue::~Battlevalue () {cout<<"Battle Free"<<Endl;} Role::role () {cout<<"Default Role"<<Endl; Data=Newbattlevalue ();} Role::role (intAtkstringname) {cout<<"Parameterrole"<<Endl; Data=NewBattlevalue (); Data-&GT;ATK =ATK; Data->name =name;} Role::role (Constrole&Object) {cout<<"Copy"<<Endl; Data=NewBattlevalue (); Data-&GT;ATK =Object.data->ATK; Data->name =Object.data->name;} Role::~Role () {cout<<" Free Self"<<Endl; Deletedata;}voidShow (role role) {cout<<"Name:"<<role.data->name<<"ATK:"<<role.data->atk<<Endl;}intMain () {Role Rock ( -,"Rockderia");    Show (Rock); System ("Pause");}

This code is executed after this is the result

parameter Rolebattle initcopybattle initname:rockderia  ATK:    Please press any key to continue ...  Free   Free

Because I was debugging under Windows, the last two lines of the above results are almost invisible in cmd, and a flash through the window closes. This does not affect us to analyze.

At the beginning of the program, the role object was created using the constructor method of the parameter, and then a Battlevalue object was created in the constructor of the role, and then we assigned the address of the object to the data variable. We then invoke the friend function of the role class because it is called by a value argument, so the role is copied when the show method is called.

The copy constructor of the role class was called when copying. We are copying the constructor Role (const role& object), and we can see that we re-assigned the value of data, a new memory address.

Read here, presumably everyone has already understood the almost. When an object expires, it removes all data from the object after the destructor is called, but what is *data? I do not know, C + + only know the data, not to dereference, only responsible for the 4 bytes deleted. So what about the heap of Battlevalue objects? Where's the GC? No, I'm sorry. Memory leaks.

That's why we've added a delete data operation to the destructor of the role, and we can see that it's also two points.

1. Call the Battlevalue class's destructor

2. Remove all data from the Battlevalue object

Only in this way can we achieve what we want, that is, a role object, a Battlevalue object, role dead Battlevalue dead. Role is born Battlevalue.

In fact, this is to facilitate understanding, if I knew so, it can not be so troublesome, directly save a Battlevalue object in the role class, do not put pointers, so C + + will help us handle memory. Of course, the premise is that demand is

A Role object a Battlevalue object, role dead Battlevalue dead. Role is born Battlevalue. But what if the demand is not? If all the characters, archers with a set of combat effectiveness, warriors with a set of combat effectiveness, mage with a set of combat effectiveness ... So each of your character objects gives him a fight. Is it too wasteful for memory?

This time to say, or the hands of the real! But how does the memory manage?

All say C + + memory management is a big hole. It's actually true.

The epitome of C + + memory management

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.