What happens if my own Visual C ++ memory allocator returns a block that is not aligned correctly?

In my Visual C ++ program, I use a custom operator newone that uses malloc()to allocate memory. My user operator newstores additional data in the first 4 bytes of memory and returns an offset pointer as the beginning of the block (program 32 bits):

 void* operator new( size_t size )
 {
     size += sizeof( int );//assume it doesn't overflow
     int* result = static_cast<int*>( malloc( size ) );
     if( result == 0 ) {
         throw std::bad_alloc;
     }
     *result = ... //write extra data
     return result + 1;
 }

Now, if the caller’s code wants to save a 64-bit ( __int64or double) variable , the block will not be correctly aligned for this.

What problems can occur in a 32-bit Windows program?

+3
source share
1

32- , .

/ ( , )

+3

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


All Articles