C ++ is everywhere New, but there are no doubts about delete

Source: Internet
Author: User

In the previous blog, I learned about the tinyxml tool architecture. In this blog, We will detail how to use tinyxml to manipulate xml. And the problems we should pay attention to during the operation.

First, import the tinyxml source file to your project. The effect is as follows:

In fact, directly add the file to the project:
 
If your system is win7 or office is above 2007, the file adding function is not available, and the result is: Or a direct crash. Or no response.
The environment is ready, so let's get started. As with database table operations-add, delete, modify, and query. According to this logic, it starts from adding (creating.
The created format is as follows:
<Persons>
<Person>
<Name> lhy </name>
<Age> 22 </age>
</Person>
</Persons>

In the previous blog, we also introduced all the classes in the tinyxml parser and their relationships.
The code for creating xml in the preceding format is as follows:
[Html] view plaincopyprint?
// Create an XML document object.
TiXmlDocument * myDocument = new TiXmlDocument ();
// Create a root element and connect it.
TiXmlElement * RootElement = new TiXmlElement ("Persons ");
MyDocument-> LinkEndChild (RootElement );
 
// Create a Person element and connect it.
TiXmlElement * PersonElement = new TiXmlElement ("Person ");
RootElement-> LinkEndChild (PersonElement );
 
// Create and connect the name and age elements.
TiXmlElement * NameElement = new TiXmlElement ("name ");
TiXmlElement * AgeElement = new TiXmlElement ("age ");
 
PersonElement-> LinkEndChild (NameElement );
PersonElement-> LinkEndChild (AgeElement );
 
// Set and connect the content of the name and age elements.
TiXmlText * NameContent = new TiXmlText ("lhy ");
TiXmlText * AgeContent = new TiXmlText ("22 ");
 
NameElement-> LinkEndChild (NameContent );
AgeElement-> LinkEndChild (AgeContent );
 
MyDocument-> SaveFile ("d: \ lhy \ xml.txt"); // save it to the file

// Create an XML document object.
TiXmlDocument * myDocument = new TiXmlDocument ();
// Create a root element and connect it.
TiXmlElement * RootElement = new TiXmlElement ("Persons ");
MyDocument-> LinkEndChild (RootElement );

// Create a Person element and connect it.
TiXmlElement * PersonElement = new TiXmlElement ("Person ");
RootElement-> LinkEndChild (PersonElement );

// Create and connect the name and age elements.
TiXmlElement * NameElement = new TiXmlElement ("name ");
TiXmlElement * AgeElement = new TiXmlElement ("age ");

PersonElement-> LinkEndChild (NameElement );
PersonElement-> LinkEndChild (AgeElement );

// Set and connect the content of the name and age elements.
TiXmlText * NameContent = new TiXmlText ("lhy ");
TiXmlText * AgeContent = new TiXmlText ("22 ");

NameElement-> LinkEndChild (NameContent );
AgeElement-> LinkEndChild (AgeContent );

MyDocument-> SaveFile ("d: \ lhy \ xml.txt"); // save it to the file

As long as you understand the relationship between nodes in xml, creation is not a problem. To put it bluntly, it is a multi-generation relationship.
The creation is done, but as a C ++ programmer, after writing it, it always feels awkward. Do you also see the existence of these tricks?
By the way, there are a lot of New pointers in some code. In C ++, there can be no garbage collection mechanism in java. You must handle the discarded garbage by yourself. But there is no Delete statement in the code?
 
I checked the information online and found that many creation codes do not have the Delete statement? Is it because everyone is copying and pasting? Or tinyxml?
I have summarized the following points, but I still have doubts in the development process. However, there is no problem in the development process, so my program is now so.
Statement 1: in many articles, there is no delete for new because tinyxml can be automatically released and pointers are automatically destroyed without manual release by developers.
Question: Can new users be automatically released? New indicates that it is created on the stack. When will it be automatically released? When the program ends, is it automatically released? So how can we determine the program is over? (How to analyze the memory area of another module in one module will be discussed later.
Since tinyxml has the self-destruction function, we can query its source code and find that, in tinyxml, corresponding pointers are released in the destructor. But not every node.
Details in source code:
[Html] view plaincopyprint?
TiXmlNode ::~ TiXmlNode ()
{
TiXmlNode * node = firstChild;
TiXmlNode * temp = 0;
 
While (node)
{
Temp = node;
Node = node-> next;
Delete temp;
}
}
Void TiXmlNode: Clear ()
{
TiXmlNode * node = firstChild;
TiXmlNode * temp = 0;
 
While (node)
{
Temp = node;
Node = node-> next;
Delete temp;
}
 
FirstChild = 0;
LastChild = 0;
}

TiXmlNode ::~ TiXmlNode ()
{
TiXmlNode * node = firstChild;
TiXmlNode * temp = 0;

While (node)
{
Temp = node;
Node = node-> next;
Delete temp;
}
}
Void TiXmlNode: Clear ()
{
TiXmlNode * node = firstChild;
TiXmlNode * temp = 0;

While (node)
{
Temp = node;
Node = node-> next;
Delete temp;
}

FirstChild = 0;
LastChild = 0;
}
We also know that there is an inheritance relationship between classes in tinyxml.
Let's look at the TixmlElement class in tinyxml:
[Html] view plaincopyprint?
TiXmlElement ::~ TiXmlElement ()
{
Clearis ();
}
Void TiXmlElement: clearis ()
{
Clear ();
While (attributeSet. First ())
{
TiXmlAttribute * node = attributeSet. First ();
AttributeSet. Remove (node );
Delete node;
}
}

TiXmlElement ::~ TiXmlElement ()
{
Clearis ();
}
Void TiXmlElement: clearis ()
{
Clear ();
While (attributeSet. First ())
{
TiXmlAttribute * node = attributeSet. First ();
AttributeSet. Remove (node );
Delete node;
}
}
TixmlElement inherits TiXmlNode. However, no destructor of the TiXmlDocument class are found in TiXmlDocument.
The second argument: The TiXmlDocument object is the root node of the tree. In a complete document, except for it, the other nodes must be its descendants, therefore, TinyXml uses a clever method to analyze the objects corresponding to each node-The destructor of each node are delegated to its father, in this way, as long as the father is correctly analyzed or the father's Clear function is called, all its descendants will be correctly analyzed, therefore, for the entire document, as long as the TiXmlDocument object is correctly destructed.
In the code above, we found that all nodes are under the root node.
In fact, this code: myDocument-> LinkEndChild (RootElement); Using polymorphism. The relationships between classes are as follows:
 

The LinkEndChild source code is as follows: it is a method in the parent class TiXmlNode.
[Html] view plaincopyprint?
TiXmlNode * TiXmlNode: LinkEndChild (TiXmlNode * node)
{
Assert (node-> parent = 0 | node-> parent = this );
Assert (node-> GetDocument () = 0 | node-> GetDocument () = this-> GetDocument ());
 
If (node-> Type () = TiXmlNode: DOCUMENT)
{
Delete node;
If (GetDocument ()-> SetError (TIXML_ERROR_DOCUMENT_TOP_ONLY, 0, 0, TIXML_ENCODING_UNKNOWN );
Return 0;
}
Node-> parent = this;
Node-> prev = lastChild;
Node-> next = 0;
If (lastChild)
LastChild-> next = node;
Else
FirstChild = node; // it was an empty list.
 
LastChild = node;
Return node;
}

TiXmlNode * TiXmlNode: LinkEndChild (TiXmlNode * node)
{
Assert (node-> parent = 0 | node-> parent = this );
Assert (node-> GetDocument () = 0 | node-> GetDocument () = this-> GetDocument ());

If (node-> Type () = TiXmlNode: DOCUMENT)
{
Delete node;
If (GetDocument ()-> SetError (TIXML_ERROR_DOCUMENT_TOP_ONLY, 0, 0, TIXML_ENCODING_UNKNOWN );
Return 0;
}
Node-> parent = this;
Node-> prev = lastChild;
Node-> next = 0;
If (lastChild)
LastChild-> next = node;
Else
FirstChild = node; // it was an empty list.

LastChild = node;
Return node;
}
In this case, if you delete the root node and myDocument in the program, it is equivalent to deleting TiXmlNode, which is equivalent to calling the destructor of TiXmlNode.
Question: in this way, the analysis structure is from the leaf to the root of the tree. According to the destructor in TiXmlNode, we can conclude that it is from the root of the tree to the leaf.

Note the following when deleting myDocument:
When creating a document, that is, the myDocument in the program section. To create a stack, you must manually release it. For example, in the above snippet, it is created on the stack.
TiXmlDocument * myDocument = new TiXmlDocument ();
If we create a function from the stack, we do not need to manually release it, but the program automatically calls the destructor. At the same time, we should note that other elements must be created on the stack. In the TiXmlNode destructor, It is delete, but the stack stuff does not need to be deleted. Therefore, the connected descendant nodes except the root node must be created from the stack.


After our explanation, do you understand the principles in tinyxml? As long as you understand the role of classes in tinyxml and the relationship between classes, it is okay to look at the source code.

This blog explains the problems in creating an xml demo. In the next blog, we will answer the question based on the xml parsing.

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.