Hello group..!
I have this struct:
struct FooData
{
unsigned short *foolist;
int NoOfFoos;
};
I want to dynamicly allocate X numbers of my FooData struct.
I'm using the new command..!
But what about my *foolist?
If I want to read data into my *foolist I have to use malloc or what...?
You may choose to use std::malloc() if you wish, but I would recommend
the use of a new expression for a number of reasons.
First, if you use new expressions consistently, you never have to
remember if you used new or called malloc() when the time comes to
release the resource.
Second, while calling malloc() will work without problem for POD types
(plain old data, that is a structure (class or struct) that has only
compiler-supplied contructors and destructors), as soon as you add
non-trivial construction or destruction you will either have to search
for and change the malloc()/free() calls to new and delete expressions
or cope with bug reports.
Third, if you centralize allocation of a particular resource through
one mechanism, it is easier to instrument that single mech for
debugging or to tweak it for performance if necessary. The only
reasonable numbers are zero, one, and infinity.
Finally, a new expression will throw an exception on failure, whereas
calling the malloc() function requires explicitly checking the
returned pointer for an out-of-band value and propagating failure
notice appropriately. Since this tends to be inconvenient, it often
doesn't get done. Again with the bug reports.
You are free to choose, but choose wisely.