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
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
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...
Lookup "Placement new"
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?
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.
It's clear now, thanks !
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:
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); for(int i = 0; i < maxHandles; ++i, mem += sizeof(Handle)) { new (mem) Handle; } Handle *handles = static_cast<Handle*>(ptr);
Also, if the destructor is non-trivial, the destruction: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);
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
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"
shouldn't this be in the FAQ?
Code:#include <cmath> #include <complex> bool euler_flip(bool value) { return std::pow ( std::complex<float>(std::exp(1.0)), std::complex<float>(0, 1) * std::complex<float>(std::atan(1.0) *(1 << (value + 2))) ).real() < 0; }