Why were you downvoted??? you were spot on in everything you mentioned.
oh and yeah i completely forgot about 2[var] syntax. I never use it since its confusing.
What do you mean overloading new? I'm not sure i've come accross that use of new
Also monkeyboi other things about memory management:
- When using new, as stated before, it goes to the heap. If you allocate too many items, you will run out of memory unless you deallocate them. When you continually allocate memory but never free it, this is called a Memory Leak
- To deallocate, you use the delete key word.
say I put SomeClass* x = new SomeClass(); to deallocate it just use delete x; (keep in mind this calls the destructor for SomeClass and frees the memory that x was pointing to, allowing later allocations to use that memory)
but if you allocate an array you have to do something else; in the case of char* ca = new char[3]; you will have to use delete [] ca;
Overloading new... I don't recommend what I'm doing here (since it's stupid) but it's a quick example:
[source lang="cpp"]static char sBuffer[512];
void * operator new (size_t sz)
{
return (void *)sBuffer;
}[/source]
That won't return the pointer from the heap, but rather will return sBuffer.
In regards to placement new:
[source lang="cpp"]char buffer[512];
new (buffer) Object;[/source]
That also will not use the heap, but will construct the object within buffer.
For placement new, do not ever call delete. Call the destructor manually. It will crash/cause a black hole as it is not a valid pointer to data on the heap.
[source lang="cpp"]char buffer[512];
new (buffer) Object;
((Object *)buffer)->~Object();[/source]
Although technically beyond the scope of mid-level C++, he should also have familiarity with the C allocators (malloc, calloc, realloc, ...) and deallocator (free).
They function relatively the same as new (with a few caveats) except that they do not call constructors/destructors. The pointers may also not be interchanged between malloc and delete, for instance.
alloca, if it's present, will allocate the memory on the stack (by decrementing the stack pointer, generally). I don't usually recommend it as it's not safe - a failure for alloca to allocate results in a stack overflow.