From Free (P) to Delete [] P

zhaozj2021-02-08  247

There have been a friend who has mentioned such a problem. How much is the survival cycle of Malloc dynamic allocation ?? At that time, I replied it directly. Of course, before calling Free, I wanted to think about this problem, before free. That time, but free has only one pointer parameter, how does it know how many spaces want to release?

INT * PINT = (int *) Malloc (10 * sizeof (int));

...... .;

Free (p);

Here Free is how to release 10 int size space?? Since FREE only needs a parameter-pointer type, then this address (Malloc returns) must have made special processing. So I asked some online friends, I have some results:

Char * p = malloc (size):

1. Actually assigned a size 4 size memory, char * p = memory head address. 2. * (INT *) P) = size; // put the size in the start of the allocated memory. 3. Return (Void *) (P 4); // Returns the part after the storage is stored. Free (p); 1. CHAR * Q = (char *) P - 4; 2. Int size = * ((int *) q); // Find Size ... 3. Release memory by operating system or Manage C / C stack of memory yourself.

Here, there should be some OS management problems, not my strength can, but we can know that Malloc does implement some special processing. Notice home is. Let's take a look at a C code below:

INT * p = new int [10];

Delete [] P;

At a glance, you can see the same function as the C code to be completed above. There are the same problem, why delete

Can release allocated objects without specifying a dynamically assigned array size, is it a new operation, and the returned address is some hands and feet: Yes, the processing made by .NEW, the same method, That is, each memory area returned by New is an additional DWORD, then hide the number of elements into DWORD. (Not all compilers use this method, I only tried VC6 and BCC55 compiler, they all This method is adopted. However, << Depth Explore C Object Model >> Just says an additional word ). In order to verify this statement, I wrote the following code for testing.

#include

Class Complex

{

PUBLIC:

Complex (int = 0, int = 0) {cout << "complex ()" << endl;}

~ complex () {cout << "~ complex ()" << Endl;

Private:

INT I, J;

}

int main ()

{

Complex * array = new complex [10];

Long * t = (long *) ((char *) -4);

COUT << * t << endl; // (1)

// * t = 20; // (2)

delete [] array;

Return 0;

}

Where (1) outputs the dimension of the array array 10. Here it is, the number of dynamically assigned complex objects is to return to the array address before a DWORD (four bytes). Now the problem is solved, we already know new What is processed, ^ _ ^, but the problem is coming, the compiler will not cause us to worry about it, it is indeed, as long as we modify the DWord content, the delete does not release allocated The memory space is there. (^ _ ^. You try to put it out of the comment in front of (2), and you will not intentionally don't think of the output)

Conclusion: The C compiler has done too much about us, causing C complicated, some things, let the compiler have changed, we don't know your code, if you understand, learn other (for example Com, ATL, etc. Friends who have helped, refer to Teacher Hou's << Depth Exploration C

Object Model >>.

转载请注明原文地址:https://www.9cbs.com/read-2750.html

New Post(0)