Memory is the key point of C and C ++ language learning.
Here is my understanding. Let's talk about it. Thank you for making a brick.
To thoroughly understand the memory, you must first understand the memory addressing. That is, the memory module of different memory stick is inserted into the machine, and the specific memory address is.
The first PC, ibm pc xt, has only KB of memory. This is the plan for IBM. The minimum 128 K is the BIOS address. After all, the BIOS is also an assembly language and it also requires a valid address to be correctly run by the CPU.
512k ~ 640 K, which is defined as a port ing address. This part of the address may correspond to the address on a peripheral device, allowing the program to directly access the device. Among them, the most important thing is the video card. The original video card unit is relatively small. For example, the monochrome video card only has 2 k video memory, which is mapped to this address segment.
Well, Bill Gates opened a century-famous yellow box, that is, home computers, with KB of memory. That's it, but now? Your memory is more than 640 kb. It can be seen that great people are also confused.
This is obviously inconvenient, because in the 286 era, the memory has already reached 1 MB in a few years.
This is troublesome. The first K of the 1 m is not usable, and there is another K in the middle. The continuous memory of the good end is cut into two parts, which is painful.
In this case, the CPU is too stupid. For a 16-bit CPU, Intel is in the PC
The XT uses an 8-bit address bus, and all the memory is split into 64 K blocks, called segments. Each program module must be smaller than 64 KB; otherwise, no jump can be made. Previously, DOS Enabled
The binary files executed can be divided into two types: COM and exe. com cannot be larger than 64 K, because there is no segment modification in the file format, so it can only be completed in one segment, 64 K.
In this way, programmers are very inconvenient. To write a program and a slightly larger array, we need to consider segment access. There is no way. The array subscript cannot exceed 64 K.
In addition, people have come up with a very stupid way to access a address above kb ~ 384 K, K, is also split into blocks, each mapped to the same memory area, access different blocks by one Io switch. That would not have imagined a better way to support more than 1 MB of memory.
In this way, programmers have become a worried profession, both to do software programming, but also to pay attention to whether their data is out of the border at any time, it is painful to die. Of course, programmers will not sit down. During this period, they thought a lot of ways, such as using the underlying module to solve the segment switching problem, and providing a layer of continuous virtual address for access.
OK, it's 80386, 32 bits. Now, we have a sigh of relief. This CPU address bus has 32 bits and can directly address 4G memory.
However, the problem is that the PC has become like this. Of course, you can re-develop a computer with continuous addressing and 4G memory. It's nice, but it's not a PC.
This makes people suffer again. It clearly has the ability to conduct continuous addressing and cross-segment boundaries, but it still cannot be done because it must support old programs.
Programmers began to work out a solution. The basic idea was to replace the actual address with a virtual address. Later, they thought that since it was all virtual, could we also Virtualize a disk file into memory, in this way, we can provide 4 GB of memory, which is not even better. The underlying layer uses some algorithms to optimize the efficiency of dynamic conversion.
So, about 93 years ago, there was a very famous C ++ language, Watcom
C ++ was born. This language is the first C ++ language in DOS that supports 4G memory. This is developed by Sybase. It uses a virtual memory management module at the underlying layer and is developed by another company.
It is called DOS/4G. Obviously, it is a solution for DOS programs to use 4G memory.
Do you still remember the legend of doom, C & C, Red Police, and Jin yongqun Xia? When the program runs, it will display a line of "dos/4GW...", which is a game written by Watcom C ++. Because the game industry requires a large array of memory, texture, and sound processing. If the array is used in multiple parts, the programmer is too tired to execute such a large program.
Well, DOS/4GW is the Watcom C ++ version of DOS 4G, because the real module is charged. This version is a simplified version and can only support MB of memory, and does not support virtual disk memory, but it is not charged. However, the application in that era is enough.
I have previously written the Watcom C ++ program. It's really nice. It's no longer a matter of block pointer or something like that.
Later on, GCC came into existence very early. In 99 years, GCC attacked the DOS market and came out with a version called djgpp, which is better than Watcom C ++, I fell in love with it when I used it. At that time, I translated its library function manual and learned it.
However, at this point in time, Windows 95 came out early and Windows 98 also came out. Therefore, DOS programs tend to decline.
As early as the development of Windows 1.1 (the first version of Windows, 1.0 was not released), Microsoft knew that the future operating system had to be friendly to programmers, while the memory was continuously stored, is the largest programmer friendly.
Therefore, from the very beginning, Windows used the underlying memory support technology. When Windows 3.1 was born, in fact, the limitations of segment development in windows were not obvious.
To Windows 95, Microsoft has built a 32-Bit Memory Manager similar to DoS/4GW and directly contains the automatic switching algorithm of virtual memory and physical memory. Therefore, after Windows 95, you can use a large array of 4 GB in theory to develop a program.
Memory disputes have come to an end.
Currently, 32-bit Windows systems generally support 4 GB memory, but the application space is only 2 GB. The address is a low-end address, that is, 0 ~ 2G address, why? Because 2G is occupied by the system, after all, there are so many services in the Windows system that need to run and address space.
Windows uses a page-cutting memory control mechanism. Each application has 2 GB of address space, which is shared by all applications and systems.
Well, 32-bit Linux is similar to Windows.
Therefore, a Windows application can use up to 2 GB of memory.
The preceding ring0 system kernel usually occupies 2 GB of address space for running. Dynamic Link Library DLL, control ocx, in the memory, due to the need to be seen by multiple processes, inter-process reuse, is also occupying the above 2G running.
Here we will talk about hooks. When we want to hook an application, the memory we see is different because our program and the program to be checked are not in the same process space. That is, what it sees in the address unit 20000 may be ff, and what we see may be 00, because this is only the same as the logical address, the physical address belongs to two process spaces.
Therefore, if you want to hook the other party's message, you have a problem. Why did you send it back?
The general practice is to make a DLL as a transfer station, hook enough, call the DLL function, first store it in the high-end memory area, and then our program will get it regularly, or use callback or something.
In short, if cross-process communication is required, the shared memory zone of the two processes must be built on the high-end memory, which is more than 2 GB of space.
As for the application's own 2g, it depends on how the compiler is used. Generally, the low end is the stack space. Our function code calls a function every time, the newly created internal variables in the function are arranged from the low-end to the high-end.
The heap is arranged from the height to the bottom. When the two run into each other, the memory is full and the memory cannot be applied.
The stack is divided into the base stack and floating stack. The base stack is the memory allocated during compilation.
Global variables, const constants, static variables, and function code are all in this part.
A floating stack is a floating stack that is used to dynamically allocate stacks, class member variables, and internal function variables based on the function and object call relationships during running.
Void func (void)
{
Char I = 0;
Char * pbuffer = malloc (10 );
//...
}
Here, the func code is actually in the stack space. 2G minimum
Int I, which is on the floating stack. Above the base station, or at the bottom of 2 GB.
Pbuffer points to the memory, because it is malloc, so it is a heap space, in the high-end of 2 GB.
New and malloc are actually the same. They both belong to malloc, but new supports objects and must automatically call constructors.
But it also shows that the new object is a runtime object, and its internal member variables are actually in the high-end 2 GB heap space.
To write C and C ++ programs, you need to be very sensitive to memory allocation. Keep an eye on the variables you use at any time, such as the base stack during compilation, floating stack during runtime, or heap.
For example, if we want to start a thread, this thread function must be in the base stack, and the compiler will set it, but we want the thread to access a Dynamic Address at runtime, for example, you need to pass a parameter to it.
We can't simply pass the variable address inside a function to him. Because the variable inside the function belongs to the floating stack, the function returns the variable and the floating stack is automatically removed, while the thread starts asynchronous operations, it is a function startup thread. It is very likely that this function has already been returned and the thread has not started running.
Therefore, the internal variables of the function cannot be used to pass parameters to the thread. The heap space can only be used to transmit parameters with an address of malloc. After receiving the parameters, the thread function is free of charge.
This is the only special case that does not comply with the principle of "who allocates and who releases. I call it "Remote Heap parameter passing ".
A lot of beginner thread friends, the thread crashes when it is written out, that is, this is a problem.
However, the program surface seems to be normal, huh, so the memory is very important, because it is basically an implicit bug, it is difficult to see the code.
It's so tired!
==========================================
Research on googleappengine
Http://student.csdn.net/space.php? Uid = 39028 & Do = Blog & id = 4259
QQ: 86696958
If you are interested in my article, click here to add me as a friend:
Http://student.csdn.net/invite.php? U= 39028 & C = 42fcd4a519102d74
Xiao