Mail Archives: djgpp/1999/03/11/11:09:30
rpolzer AT gmx DOT de asked:
> Can I do the following: (in C++)
>
> struct PACKET {
> // ... some data
> char P_DATA[0];
^^^
> };
Typo, I guess. (By the way, if you want to use C++ anyway,
the string class may be helpful.)
> PACKET *p;
>
> p = (PACKET *) new char [i + sizeof(PACKET)]; //this should work
If you mean "*" instead of "+", I guess it should, but
actually all you have to write is
p = new PACKET [i];
to allocate space for i objects of type PACKET (and to call
the default constructor for each of them); new automatically
takes care of the size calculation and returns a pointer of
the correct type.
> But how to delete p?
>
> delete p; // memory leak?
> delete [] p; // does THAT work?
> delete [] (char *) p //or that?
Number 2. Golden rule: if you allocate with [], use delete[],
if not, just delete:
foo* pf = new foo;
// ... stuff using pf ...
delete pf;
bar* pb = new bar [42];
// ... stuff using pb ...
delete[] pb;
/HW
- Raw text -