Mysterious new[] and delete[] problem!
-
Hello everybody! I have a very annoying problem here.. the thing is, I am optimizing my application with SSE `intrinsics' in some places, and the data used by such instructions must be 16-byte aligned, or the program crashes. So I have this class that contains the SSE data member, which is aligned properly WITHIN the class. I have also overloaded the `new' operator to properly handle a single object by allocating memory with void * operator new( unsigned int cb ) { void *res = _aligned_malloc( cb, 16 ); return res; } The problem comes with dynamic arrays; The compiler seems to add some _extra_ info before the actual array data, without aligning the array data properly after it. I understand that the extra information is the array length or something like that. Is there any other function I can override, that gets called _after_ operator new[] to save such information myself - aligned? Thanks anyone who helps me out.. I am having bad dreams about unaligned memory all the time. X| /Erik
-
Hello everybody! I have a very annoying problem here.. the thing is, I am optimizing my application with SSE `intrinsics' in some places, and the data used by such instructions must be 16-byte aligned, or the program crashes. So I have this class that contains the SSE data member, which is aligned properly WITHIN the class. I have also overloaded the `new' operator to properly handle a single object by allocating memory with void * operator new( unsigned int cb ) { void *res = _aligned_malloc( cb, 16 ); return res; } The problem comes with dynamic arrays; The compiler seems to add some _extra_ info before the actual array data, without aligning the array data properly after it. I understand that the extra information is the array length or something like that. Is there any other function I can override, that gets called _after_ operator new[] to save such information myself - aligned? Thanks anyone who helps me out.. I am having bad dreams about unaligned memory all the time. X| /Erik
>> The compiler seems to add some _extra_ info before the actual array data it adds the size of the array so that the 'operator delete[]()' *knows* how many objects it must delete. Looks like you need to overload the 'operator delete[]()' so you can implement your own memory management system; and do not fogort to implement the corresponding 'operator delete()' and 'operator delete[]()' Serge
-
>> The compiler seems to add some _extra_ info before the actual array data it adds the size of the array so that the 'operator delete[]()' *knows* how many objects it must delete. Looks like you need to overload the 'operator delete[]()' so you can implement your own memory management system; and do not fogort to implement the corresponding 'operator delete()' and 'operator delete[]()' Serge
Hello, and thanks for your reply! I don't think it's the delete[] operator that is the problem right now. I print the pointer that I obtain with _aligned_malloc to the screen (in my overloaded operator new[]) and then print the pointer obtained by _using_ the the new[] operator, the latter being unaligned and pointing to my actual data... so something happens between the aligned allocation in the overloaded new[] function and my assignment of the new pointer; e.g: Pointer with _aligned_malloc = 0x00a40030 (aligned on 16-byte boundary). Pointer after assigning the final object with new[] = 0x00a40034 (not aligned). Even worse, the compiler seems to add extra extra info there in debug mode - no simple workaround, such as adding and subtracting 4 bytes to the address. So I need to be able to completely handle this on my own for my program to work... hasn't anybody programmed with SSE instructions and come across this problem? :(( The delete[] operator will be a later problem, correct allocation is the most important thing right now, so that the program doesn't crasch. :) Thanks