Re: help please with simple one!

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Thank you very much. You explanation was very helpful. so it just
looks like I got lucky on a 64bit platform.

On 8/10/07, John Love-Jensen <eljay@xxxxxxxxx> wrote:
> 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
>
>


-- 

Regards
Raghu

[Index of Archives]     [Linux C Programming]     [Linux Kernel]     [eCos]     [Fedora Development]     [Fedora Announce]     [Autoconf]     [The DWARVES Debugging Tools]     [Yosemite Campsites]     [Yosemite News]     [Linux GCC]

  Powered by Linux