- variation: new MyClass(arg1) // most common
- variation: new MyClass() // better form, calling the no-arg ctor
- variation: new MyClass //bare word, can leave fields uninitialized
- variation: new (nothrow) MyClass(…) // returns 0 upon failure
- variation: placement new
- variation: array-new // no argument allowed!
Popular IV topic. P41 [[more effective c++]] has an excellent summary:
- to BOTH allocate (on heap) and call constructor, use regular q(new)
- to allocate Without construction, use q(operator new)
- You can also use malloc. See https://stackoverflow.com/questions/8959635/malloc-placement-new-vs-new
- to call constructor on heap storage already allocated, use placement-new, which invokes ctor
The book has examples of Case 2 and Case 3.
Note it’s common to directly call constructor on stack and in global area, but on the heap, placement-new is the only way.
Placement-new is a popular interview topic (Jump, DRW and more …), rarely used in common projects.
- The vector “shell” can be on stack or heap or static memory, as you wish.
- The continuous array + the payload objects are always allocated on the heap, so that the vector can grow or deallocate them.
Deallocation is explained in https://stackoverflow.com/questions/13944886/is-stdvector-memory-freed-upon-a-clear
P9 [[c++game development primer]] has a short implementation without using heap. The memory pool comes from a large array of chars. The allocator keeps track of allocated chunks but doesn’t reuse reclaimed chunks.
It showcases the header associated with each allocated chunk. This feature is also part of a real heap allocator.
reinterpret_cast is used repeatedly.
I find the chapter fairly practical and up to date, better than the specific techniques in [[effC++]]
This domain is full of bite-sized knowledge pearls — my advantage. Here are some examples:
http://stackoverflow.com/questions/11108328/double-alignment Wug’s answer echoes Ashish’s comment that char fields should be grouped together.
[[Pro .net performance]] P92 has 2 pages on C/c++ free-list. Concise, in-depth.
Search – for a matching block
Fragmentation – many too-small blocks
Per-thread private free-list? Worsens fragmentation
__Tan Bin (+65)6530 1386 OC Centre #17__
I now think there are various overhead with DMA
* search the free list for a suitable chunk
* If an allocation is needed where the heap is almost used up, glibc must grab more memory from the kernel, then hand out a slice of it.
Linux alloca() and variable-length-array both avoids some of the overhead. See P267 [[linux sys programming]].
If a low-latency module does a ton of malloc(), then alloca() might outperforms malloc() easily. We should benchmark.
The op-new and op-new operators both return an address. If you received the address without knowing which new operator was used, then it’s impossible to know how to delete it. As [[effC++]] points out, deleting the wrong way is UndefinedBehavior.
On a similar note, if your function receives a char*, you would have no idea whether it’s a single char or a string.
Note there's NOT a 2nd parameter to indicate length of the array.