Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Mysterious new[] and delete[] problem!

Mysterious new[] and delete[] problem!

Scheduled Pinned Locked Moved C / C++ / MFC
data-structuresperformancehelpquestion
3 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • E Offline
    E Offline
    Erik Hammar
    wrote on last edited by
    #1

    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

    S 1 Reply Last reply
    0
    • E Erik Hammar

      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

      S Offline
      S Offline
      Serge Krynine
      wrote on last edited by
      #2

      >> 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

      E 1 Reply Last reply
      0
      • S Serge Krynine

        >> 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

        E Offline
        E Offline
        Erik Hammar
        wrote on last edited by
        #3

        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

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups