Overloading new for alignment

This is a discussion on Overloading new for alignment within the Windows Programming forums, part of the Platform Specific Boards category; Hi. I'm trying to overload my new and delete operators so that they can be created aligned in memory. Basically ...

  1. #1
    Codebot
    Join Date
    Jun 2004
    Location
    Toronto
    Posts
    195

    Overloading new for alignment

    Hi. I'm trying to overload my new and delete operators so that they can be created aligned in memory. Basically what I have is:

    Code:
    void *operator new(size_t size) throw()
    {
    	return _aligned_malloc(size, 16);
    }
    
    void operator delete(void *ptr) throw()
    {
    	_aligned_free(ptr);
    }
    I was wondering if this would stil support object unwinding if I use my version of delete? Also, I am having a hard time compiling because this implementation fights with the new/delete declared in other headers. How would one avoid this type of linker error?
    Founder and avid member of the Internationsl Typo Associateion

  2. #2
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,669
    >> I was wondering if this would stil support object unwinding
    Yeah, compiler takes care of that for you.

    >> How would one avoid this type of linker error?
    I've run across these kinds of issues in the past...
    What environment are you building in? IDE/Compiler, SDK version, ect... For example: "VC 6.0 with SDK it was installed with".

    What are the link errors? Can you provide a simple app that demonstrates the link errors?

    gg

  3. #3
    Codebot
    Join Date
    Jun 2004
    Location
    Toronto
    Posts
    195
    I'm using VC 9.0 (2008) and I'm programming a DirectX application. I did a bit of digging and one of the DirectX files overloads the new operator, so my code conflicts with it.

    Error is like this:
    error LNK2005: "void * __cdecl operator new(unsigned int)" (??2@YAPAXI@Z) already defined in LIBCMTD.lib

    Any suggestions?
    Last edited by Mastadex; 05-27-2008 at 10:21 AM.
    Founder and avid member of the Internationsl Typo Associateion

  4. #4
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,669
    Well, let's backup a bit... Why do you want the global new operator to return memory on a 16 byte boundary?

    >> DirectX files overloads the new operator, so my code conflicts with it.
    >> ... already defined in LIBCMTD.lib
    The linker error doesn't show a conflict with DirectX - but here's what I would check first. Make sure that any static libs that you are linking against use the same CRT-runtime settings as the main project. So LIBCMTD.lib is used when building with the Debug-Multithreaded CRT. So everything you link against should be using the Debug-Multithreaded CRT as well.

    You can add "/verbose:lib" to the linkers command line to see what libs are being pulled in. Which will also tell you if multiple CRT libs are being used (I think).

    gg

  5. #5
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,596
    AFAIK MSVC already does alignment for you. You can specify the alignment boundary in the options.

    The Direct3D code is probably overloading new to the debug version of new which will track allocations for you.

  6. #6
    Codebot
    Join Date
    Jun 2004
    Location
    Toronto
    Posts
    195
    Quote Originally Posted by Bubba View Post
    AFAIK MSVC already does alignment for you. You can specify the alignment boundary in the options.
    I don't think it does, IMO. I've seen addresses that were not aligned. I know adding __declspec(align(16)) before your class/variable will guarantee that non-dynamically allocated memory is aligned to 16 bits. AFAIK, dynamically allocated classes and variables with that prefix are NOT aligned. Hence I'm trying to wrap _aligned_malloc() in a new operator.

    I heard that adding a #pragma push(16) might align everything correctly, but I've yet to see it work properly.

    But anyway, thats beside the point. I changed the calling convention of my project from __fastcall to __cdecl and everything works now. Thats odd that it forces me to use this convention.
    Last edited by Mastadex; 05-27-2008 at 07:48 PM.
    Founder and avid member of the Internationsl Typo Associateion

  7. #7
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Have you actually examined the alignment of your data? I believe MS alignment is at least 16 bytes on all allocations - it may even be aligned to 32 or 64 bytes at all times.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Overloading operators
    By ugmusicbiz in forum C++ Programming
    Replies: 2
    Last Post: 02-13-2009, 12:41 PM
  2. unary operator overloading and classes
    By coletek in forum C++ Programming
    Replies: 9
    Last Post: 01-10-2009, 01:14 AM
  3. overloading operator problems
    By almich in forum C++ Programming
    Replies: 2
    Last Post: 07-26-2004, 04:10 PM
  4. operator overloading
    By blue_gene in forum C++ Programming
    Replies: 6
    Last Post: 04-29-2004, 04:06 PM
  5. overloading
    By theLukerBoy in forum C++ Programming
    Replies: 6
    Last Post: 11-04-2002, 07:49 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21