"Dmytro Bablinyuk" <dm**************@rftechnology.com.auschrieb im
Newsbeitrag news:ed**********@news-02.connect.com.au...
>I came across several possible ways of allocating memory for objects, for
example:
1. malloc(sizeof(T)*3)/free - raw memory
2. new T[3]/delete[] - buffer would be initialized to default-constructed
T objects.
3. operator new(sizeof(T)*3)/operator delete - raw memory
What the best way of allocating memory for simple types and objects?
For objects the "new T[3]" looks like the best way since it initializes
the array, but what about simple types?
Right now for "int *" I am using malloc, but would it be better if I use
"operator new" for instance? What the advantages?
Always do what is easy to learn and, even more important, easy to remember.
For non-POD types new[]/delete[] (or new/delete for single instance) is the
only reasonable way, and this also works for POD types. So there is no
reason to use malloc at all. What if you change the type in the future.
Would you like to spend hours replacing all those mallocs and frees, and
later days to find the one place you missed? And will you know in a year
when to use delete and when free in a long forgotton program? Why do you
want to carry around two big hammers when you only need one to hit your
thumb?
Heinz