Memory Allocation in VC ++

I work with VC ++ 2005. I overloaded new ones and deleted operators. Everything is fine.

My question is related to some magic VC ++ adding to memory allocation.

When I use C ++ call:

data = new _T [size];

The return (for example) from the global memory allocation is 071f2ea0, but the data is set to 071f2ea4

When overloading delete [], the address 071f2ea0 is called.

One more note: when using something like:

data = new _T;

Both data are returned from the global memory allocation.

I'm sure Microsoft is adding something at the head of memory allocation for use in accounting. My question is: does anyone know the rules that Microsoft uses.

"" , .

, 4 - , . , - , . . .

, 6 , 4

+3
4

:

malloc ( ), , . , . malloc ++.

, , ++ / 4 , . - 4 , .

, :

void* _cdecl operator new(size_t size)
{
    void *ptr = malloc(size);
    return(ptr); 
}

:

object * data = new object [size]

ptr 4 (, )

char *data = new char [size]

ptr, .

, malloc .

0

4 , , , delete [] , .

, /16, . . , + 4 16 .

EDIT: , 50 new, delete []. printf, .

#include <stdio.h>

class MySimpleClass
{
    public:
    ~MySimpleClass() {printf("Hi\n");}
};

int main()
{
    MySimpleClass* arr = new MySimpleClass[50];
    delete [] arr;


    return 0;
}

, , . , V++ 4 .

; Allocation
mov ecx, 36h ; Size of allocation
call    scratch!operator new
test    rax,rax ; Don't write 4 bytes if NULL.
je      scratch!main+0x25
mov     dword ptr [rax],32h ; Store 50 in first 4 bytes
add     rax,4 ; Increment pointer by 4

; Free
lea     rdi,[rax-4] ; Grab previous 4 bytes of allocation
mov     ebx,dword ptr [rdi] ; Store in loop counter
jmp     StartLoop ; Jump to beginning of loop
Loop:
lea     rcx,[scratch!`string' (00000000`ffe11170)] ; 1st param to printf
call    qword ptr [scratch!_imp_printf; Destructor
StartLoop:
sub     ebx,1 ; Decrement loop counter
jns     Loop ; Loop while not negative

, malloc HeapAlloc. ​​. . V++ , , , . - 240 20- , 256- , .

+4

4 . delete [], , .

, , - .

+1

, , .

, "" , , ,... CodeGuru . , msdn documentation.

0

Source: https://habr.com/ru/post/1708387/


All Articles