please look this code , in my machine , it do not raise error , but I do not understand why can I copy more bytes than VirtualAlloc allocated,is this operation safe ?
PBYTE pNewBuffer = (PBYTE) VirtualAlloc(NULL,3,MEM_COMMIT,PAGE_READWRITE);
BYTE FlagThree[] = {'a', 'b', 'c', 'd','e','f','g'};
CopyMemory(pNewBuffer,FlagThree,sizeof(FlagThree));
I allocate 3 bytes but copy to the memory 7 bytes.
VirtualAlloc rounds your allocation up to the nearest allocation boundary, so although you are requesting 3 bytes you'll actually allocate more as the allocation granularity is 64K.
Because of this you are able to write more that the 3 bytes you requested. However, as mentioned in the comments, this is undefined behaviour and you shouldn't do it.