Analyze the six key technologies of MFC (1 to 6)

Source: Internet
Author: User

Analysis of the six key technologies of MFC (Part 1) -- the initialization process of the MFC Program

Http://blog.csdn.net/liyi268/archive/2005/02/22/297875.aspx

Analysis of the six key technologies of MFC (Part 2) -- runtime type recognition (rtti)

Http://blog.csdn.net/liyi268/archive/2005/02/27/304175.aspx

Six key technologies of MFC (part 3)-Dynamic Creation

Http://blog.csdn.net/liyi268/archive/2005/03/04/310895.aspx

Six key technologies of MFC (part 4)-permanent storage (serialize)

Http://blog.csdn.net/liyi268/archive/2006/03/13/623367.aspx

Six key technologies of MFC (5) (6) -- message ing and Command Transmission

Http://blog.csdn.net/liyi268/archive/2006/03/13/623391.aspx

 

 

%

 

Let "dynamic creation", "static creation", "dynamic memory allocation", and "Dynamic Association"

 

After "Analysis of the six key technologies of MFC (iii)" published the day before yesterday, many friends asked questions. These questions have surprised me-I don't know what to ask? Later I understood it, because a friend asked, "Do you have to do so many things to create objects dynamically? What is dynamic object creation ?"

I know that he hasn't understood the meaning of dynamic creation in MFC. Dynamic Creation of MFC has a deeper significance than dynamic memory allocation with new. The dynamic creation of MFC is a class name that is not predicted to be obtained by the program after the program runs. For example, the program opens a file or accepts a string from the keyboard, the MFC program can generate an object based on your input. The key factor for achieving this function is the cruntimeclass linked list. With this linked list, we can say to the program, "Help me 'generation' a xxx ." The program does not know in advance what type of object XXX is. It will answer: "I have to find it in my cruntimeclass notebook. Well, I have found it. It is such a thing that can be produced according to the cruntimeclass write requirements ."

About dynamic creation, you will have a thorough understanding of the six key technologies of MFC (4)-permanent storage. But now I am more concerned about another issue.

The problem I encountered just now has brought me into deep memories, as if I had returned several years ago. At that time, I held a C ++ program and didn't understand anything at that time, so I went between "dynamic" and "static. Every book clearly says: "The Memory Allocation before the compilation link is static, and the allocation after the program is dynamic !"

Not to mention, I still have some understanding, but I do not even understand it. Because we only know that the compilation link is a language translation process, and the source code is converted into machine commands that computers Know. No matter which compiler is selected, the ultimate goal is to generate an executable file more effectively. Since it is a file, when the program is not running, it is stored in the disk with the. exe extension.

If the compilation link and running are interrupted, when the program is not loaded and running, it is just a file. What is the memory? This does not mean that the vast majority (almost all I see) of books talk about polymorphism and also talk about "runtime binding ", it means that "in these overloaded functions, you can use a series of virtual functions. When compiling, you are not sure which function to call. You will know it at runtime!"

"Nonsense! !" After many years of programming, I realized that the book was very wrong. Think about it. The compiler cannot determine which function will be called, so how can it be determined after the program is run (of course, the functions called based on the judgment and return value are also different ). Can the system be sure that the system is a cold machine, coupled with a processing program, or even more operating systems manipulated by our programs, how does it know what you can't figure out when writing a program on your own until it is compiled? In fact, in function overloading or virtual functions, the compiler has long been renamed by rules, or created a virtual function table to clearly determine the function to be called.

Static and Dynamic Allocation is actually a simple task.

When we load the program, the system will load our program code into the memory.

Before a program runs, it allocates space and initialization for a series of functions or objects, such as global objects or static variables, in the static storage area. During the running of the program, the storage zone is associated with the program itself, and we cannot delete it. For example, we have defined a global variable int x = 100; we cannot try to release it through the delete X; statement. The life and death of the stored area has long been "doomed" by the program and will not be determined by our code.

The same is true for a local object. its lifecycle starts from the left braces and ends with the right braces. During this period, the stack is created, and its life is determined by the program itself.

Dynamic memory allocation is different. It is allocated on the heap, And the heap is a large memory. The memory allocated above may be changed immediately, just like the warlord command.

We can allocate it from the left braces, but the right braces do not mean that the program is "dead"-released. Of course, we can also say "delete warlord 1" at any time to make it instantly available memory.

In some books, dynamic memory allocation uses new or heap-allocated memory, and even has the following features: the allocated object has no name, for example, int * Pi = new Int; (Note: PI is the pointer name. Do not mix it with the object it points to. First assign an integer variable without a name and then fill in the variable to Pi ).

However, I think the reason for dynamic memory allocation is that the heap memory allocation is flexible. It is like a dynamic array, which can be increased, reduced, generated, and killed when the program is running, and all of this is controlled in our program statements.

When I first learned C ++, I always felt that it was very glorious to dynamically allocate memory. Dynamic means automatic. Later I learned that static memory allocation is automatic-automatic allocation and recovery.

Dynamic Allocation is always manual. You need to manually recycle the memory at the end.

Dynamic Allocation will affect the execution efficiency of the program, just like doing one thing. It is better to run the command only when an "object" is required, all "objects" are at your fingertips.

 

 

I have never looked back at "Analysis of the six key technologies of MFC (iii)", because when I write "I", I cannot decide whether to write it down. I don't think there are any friends who want to see what I wrote. I usually have a few posts. I would like to talk to my friends: Understanding the six key technologies is a shortcut for those who want to learn MFC. Otherwise, MFC programming will always be a nightmare for you. But I know this is my personal opinion. Maybe some friends just add controls and add message ing to write a great program.

 

 

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.