Why new malloc and free and open array can delete off?

If that why free and delete (or of course the underlying call free) can release an array of space.

Write test code:

#include <iostream>
#define N 16
using namespace std;
int main()
{
    int* p = (int*) (malloc(sizeof(int) * N));
    for (int i = 0; i < N; ++i) {
        p[i] = i;
    }
    free(p);
    return 0;
}

Running and positioning at p memory locations:

 

 Ah, what is in front of the array fdfdfdfd that? There are few figures in front again, unsure, try again.

N into 32:

 

 N into 64:

 

 N changed to 128:

 

 Well, that 01 might be the front indicates that this is a new open memory or indication of heap memory and the like, and open array size has nothing to do, excluded. 9a is the final surface, and independent of the array size.

The remaining middle of this figure:

40---N=16

80---N=32

0001---N=64

0002---N=128

It should be 4 refers 4 int. 4 * 4 = 16

8 refers to 8 int, 8 * 4 = 32

0x10 (decimal 16) means 16 * 4 = 64

0x20 (decimal 32) means 32 * 4 = 128

As to why these values ​​turn it, I did not get to know. Normal X86 are little-endian machines, decimal 1-> Hex 00,000,001. Byte reverse order in turn is 01 million, it may be evident from the above diagram. However, examples of the foregoing are actually bitwise reverse directly, rather than the reverse order of bytes. There can comment about the friends told me about it.

 

Another knowledge: a new open free heap space can be released, but does not call the appropriate destructor. So, if a built-in type, free completion and delete the same effects (including int, double, array , etc.). If you are self-built type (including stl, as well as internal or pointer), it is unable to complete the release.

Guess you like

Origin www.cnblogs.com/FdWzy/p/12460048.html