Please help on understanding special C++ syntax used

This is a discussion on Please help on understanding special C++ syntax used within the C++ Programming forums, part of the General Programming Boards category; Can somebody help to understand this special syntax below or give an alternative syntax for it void* ptr = btAlignedAlloc(sizeof(Handle)*maxHandles,16); ...

  1. #1
    Registered User
    Join Date
    Jul 2008
    Posts
    3

    Please help on understanding special C++ syntax used

    Can somebody help to understand this special syntax below or give an alternative syntax for it

    void* ptr = btAlignedAlloc(sizeof(Handle)*maxHandles,16);
    m_pHandles = new(ptr) Handle[maxHandles];

    IThanks,
    Nout

  2. #2
    Registered User
    Join Date
    Oct 2006
    Location
    UK/Norway
    Posts
    485
    void* ptr = btAlignedAlloc(sizeof(Handle)*maxHandles,16);
    its the same as new/malloc, but here the bits are aligned to 16bit

    m_pHandles = new(ptr) Handle[maxHandles];
    Takes the memory in ptr and gives you an array of Handle objects

    Not 100% sure...

  3. #3
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Lookup "Placement new"

  4. #4
    Registered User
    Join Date
    Jul 2008
    Posts
    3
    Sorry, but i'm not much skilled in C++ yet and do not understand the placement new thread
    Can you rewrite the syntax into Eg. Borland C so I can better understand it?

  5. #5
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    http://www.google.com/search?q=placement+new

    First hit explains it. You may not be skilled in C++, but you should be familiar with google.

  6. #6
    Registered User
    Join Date
    Jul 2008
    Posts
    3
    It's clear now, thanks !

  7. #7
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    The code is erroneous, by the way. It allocates enough memory for n Handle objects, and assumes that suffices for an array of n Handle objects. However, this assumption isn't necessarily correct, as the array form of new may use additional memory to store management information.

    Furthermore, if the array form does this, it would store this information at the start of the array, and unless this information is 16 bytes long, it would destroy your alignment.

    All this leads to a pretty mess. The next standard will have alignment directives to avoid such issues, but in the current one, the way I see it, the only portable way to do this is to call non-array placement new for every single array element yourself. This would look like this:
    Code:
    void* ptr = btAlignedAlloc(sizeof(Handle)*maxHandles,16);
    char *mem = static_cast<char*>(ptr);
    for(int i = 0; i < maxHandles; ++i, mem += sizeof(Handle)) {
      new (mem) Handle;
    }
    Handle *handles = static_cast<Handle*>(ptr);
    This is under the assumption that Handle has a nothrow constructor. If it doesn't, this gets a lot more complicated:
    Code:
    void* ptr = btAlignedAlloc(sizeof(Handle)*maxHandles,16);
    char *mem = static_cast<char*>(ptr);
    int i = 0;
    try {
      for(; i < maxHandles; ++i, mem += sizeof(Handle)) {
        new (mem) Handle;
      }
    } catch(...) {
      Handle *handles = static_cast<Handle*>(ptr);
      for(i = i - 1; i >= 0; --i) {
        handles[i].~Handle();
      }
      btFree(ptr);
      throw;
    }
    Handle *handles = static_cast<Handle*>(ptr);
    Also, if the destructor is non-trivial, the destruction:
    Code:
    for(int i = maxHandles - 1; i  >= 0; --i) {
      handles[i].~Handle();
    }
    btFree(handles);
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  8. #8
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by CornedBee View Post
    The code is erroneous, by the way. It allocates enough memory for n Handle objects, and assumes that suffices for an array of n Handle objects. However, this assumption isn't necessarily correct, as the array form of new may use additional memory to store management information.
    Thanks! I've never needed to use placement new, but I probably wouldn't have thought of that...

  9. #9
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,292
    Thanks for that CornedBee. Ironically I read that thinking that calling placement new for each item instead was an interesting idea, only to remember shortly afterwards that I did exactly that a few weeks ago!
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  10. #10
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,648
    shouldn't this be in the FAQ?
    Very basic "thin" socket API:
    ip.hpp

  11. #11
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by Sebastiani View Post
    shouldn't this be in the FAQ?
    Yes it should (although I don't really think it's really a "Frequently" asked question), because I'll probably forget all about it when/if I ever do need to use placement new.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. more then 100errors in header
    By hallo007 in forum Windows Programming
    Replies: 20
    Last Post: 05-13-2007, 08:26 AM
  2. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 04:45 PM
  3. Using VC Toolkit 2003
    By Noobwaker in forum Windows Programming
    Replies: 8
    Last Post: 03-13-2006, 06:33 AM
  4. Connecting to a mysql server and querying problem
    By Diod in forum C++ Programming
    Replies: 8
    Last Post: 02-13-2006, 09:33 AM
  5. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM

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