C + + Memory Management Change (3): Alternative memory management

Source: Internet
Author: User
Tags header

The simplest C++/java program

The simplest Java program:

class Program
{
public static void main()
{
new int;
}
}

对应的C++程序:

void main()
{
new int;
}

I don't think a Java programmer would think there was a problem with the Java code above. But all the rigorous C + + programmers immediately pointed out: the above C + + program has a problem, it has a memory leak. But one of the ideas I want to talk to you about today is that there is nothing wrong with this C + + program.

Memory management for docx programs

Docx is a document authoring tool that I developed. Here are some introductions about it. In this section, I want to talk about the alternative memory management approach I tried in docx.

The overall process of docx is:

Read a C + + source code (or header) file (. H/.c/.hpp/.cpp, etc.), analyze the annotations in it, and extract and generate the XML document.

Converts an XML document to HTM through an XSLT transformation.

Analyze all include directives in the source code to get the corresponding header file path, and if a header file has not been parsed, skip to 1 to repeat the steps.

Finally, all generated HTM packages generate CHM files.

In the beginning, as java/c# programmers do, all new in my Code does not consider delete. Of course, it worked very well until one day My documents accumulated to a certain extent. As we foresaw, the Docx program ran into a crash.

So, what do we do? Find all the places you need to delete and fill in the delete?

This is not really necessary. In the front, I've introduced autofreealloc (see "C + + Memory Management Change (2): The most compact garbage collector"), and perhaps someone is muttering about what this memory allocator does. -Well, now you can see one of its typical uses:

After the collapse of our docx, I just made the following changes:

Add a global variable: Std::autofreealloc alloc;

All NEW Type (arg1, arg2, ..., argn), change to Std_new (Alloc, Type) (Arg1, arg2, ..., argn);

All NEW type[n], change to Std_new_array (Alloc, Type, N);

Every time a source code file is processed, a alloc.clear () is invoked.

Okay, since then, docx no more memory leaks and no more crashes with memory shortages.

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.