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.