Greetings,
I'd like to ask few questions on new and delete. I'm very much used to using malloc() and free(), and I feel uncomfortable with new/delete... so I'm attempting to overcome that.
--[1]--
If I do the following:
int *i;
i = new int;
or:
int *i;
i = new int[10];
am I right that I only need to do:
delete i;
to deallocate memory?
--[2]--
Say, I have a structure:
typedef struct _test_tag
{
struct _test_tag one;
struct _test_tag two;
// or whatever
} TEST;
and I allocate memory for 1D array:
TEST* hello;
hello = new TEST[10];
do I use delete or delete[]? Turbo C/C++ Complete Reference 2nd Edition (1992) by Shildt writes:
quote:
When you free a dynamically allocated array of objects that have destructor functions , you must use this form of delete:
delete[] pointer_var;
In this case, TEST is a structure and it doesn't have a destructor. Hence
delete hello should be ok. However, in online doc that comes with VC++5, it says:
quote:
If pointer points to an array, place empty brackets before pointer.
According to this, I should do
delete[] hello as it's a 1D array. Those two contradicts each other
if it's seen as an array. Or is
new TEST[10] even seen as an array? or is it equivalent to
(TEST*)malloc( sizeof(TEST)*10 )? That wouldn't be an array. I'm wondering how it should be according to the modern C++ spec.
--[3]--
Would:
int* i;
i = new int[10];
the same as the following?:
int* i;
i = new int[2*5];
and the same as the following?:
int* i;
i = new int[2][5];
or would that mean five 1D-array of 2-ints? If so (and only if so), would I HAVE to do this?:
int* i[5];
i = new int[2][5];
Or can I get away with the former one?
I believe all of above allocate 10 ints. But am I right that the last one requires the use of delete[], while the former ones don't???
--[4]--
I understand that type casting is done automatically if you use new. So, if I do the following:
int* i = new int[
size ];
then
size is the multiple value of ints? So, given that int is 2 bytes, that allocates
size x 2 bytes? Or is it just 2 bytes, and the preceeding 'int' is a form of type-casting? (If it is type casting, then the statement that type cast is done 'automatically' sounds like a bit of a cheat, if you see what I mean...) That allocating
size x 2 bytes is dodgy - so I believe that's wrong. Otherwise the following two:
int* i = new int[2];
int* i = new char[4];
would allocate the same amount of memory, which is 4 bytes. That would be bizarre, so that must be wrong... But then, on the other hand, if type casting is done automatically, then the 2nd one should also work... So here, I'm a little confused because they all sound correct depending on perspective (and the others sound wrong at other times).
--[5]--
Finally, if I can do:
int* i;
i = new int (0);
to allocate memory for i and initialize it to Zero, then can I also do:
int* i[1];
i = new int[1][2] (0);
to have i[0]=0 and i[1]=0? It seems to make sense... but also seems bizarre and ambiguous.
--[X]--
On a different note (not relating to new and delete), if I do:
TEST* hello;
hello = new TEST;
memset( hello, 0, sizeof(TEST) );
would that
memset set all its member pointers to NULL? If NULL=0 by compiler definition, then that seems to make sense (but I don't know that).
-------
Thank you, and please reply where you can, even if you can't reply to all!
Edited by - PugPenguin on June 3, 2001 2:27:00 AM