help please with simple one!
John Love-Jensen
eljay@adobe.com
Fri Aug 10 22:15:00 GMT 2007
Hi Raghu,
> what am I doing wrong.
Change this:
Base *temp = new Child [1024] (3, 2);
...to this...
Child *temp = new Child [1024];
Add in a default constructor for Child, and probably a default constructor
for Base too -- since you cannot use initialization parameters for a new[].
When the delete[] deletes the Base array, the first Child element happens to
coincide enough (on your platform; although I think its not guaranteed to be
portable) in memory with temp[0] that the code actually executes the virtual
destructor and gets to Child::~Child as desired.
When it moves on to the destructor for temp[1], the stride is off because
temp does not really point to an array of Base, it points to an array of
Child... and then you get a bus error when the code attempts to dereference
who-knows-what garbage memory as if it were a virtual functional table
pointer to get to the virtual destructor.
You can use Base* to point to a single Child object.
You cannot use Base* to point to an array of Child objects. (Well, you can
get to the first Child object, but none of the subsequent ones.)
You could change your code to use a std::vector<Base*>. Or use one of the
Boost managed containers and/or smart pointers.
HTH,
--Eljay
More information about the Gcc-help
mailing list