Re: Segfault with delete[] operator & virtually derived classes

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

 



JP Mercury wrote:
Nathan, Eljay, Lyle-

As far as I know, I can do new B[10]; and store it in any pointer type.
For example:

char *ptr = (char *) new B[10];
C *ptr = (C *) new C[10];
sure, but don't expect the compiler to invoke the correct functions when
you operate on such pointers.


No problem unless I try to dereference ptr, then I will get the wrong data.
But I take the true size of one array element into account and always cast
back to a A * before dereferencing an array element. I don't see this as lying
to the compiler, just a clever use of casting and walking an array.

Whatever. casts are for broken bones and broken code :) The compiler is there to manage the type system and do all that tedious bookkeeping for you. If you wish to do that manually, that's your call. You'll get fragile code and problems like this one.

I used to do a lot of video coding and we did that all the time, casting video
memory between char * and long * and what not. The memory remains the same, we
just look at it in a different way.

ah, but in C++ you have objects, and objects are different from plain old data.

But let's say I take your suggestion and implement an array of pointers. How
do I allocate the memory for 10 Bs in one block? I don't want 10 separate
instances of B, I want a contiguous block of 10 Bs. I could create a block of
10 Bs:

Why do you want a contiguous block of B's? It sounds like you're developing either a memory manager or an object manager, or a bit of both. But it's really unclear as to what your actually trying to do.

If you want a polymorphic array of objects, THE way to do it in C++ is
with an array (or other container) of pointers to objects.

Anyway, it's clear this is not a gcc problem but a C++ problem.  A C++ forum
would be better to help you.

nathan

--
Nathan Sidwell    ::   http://www.codesourcery.com   ::     CodeSourcery LLC
nathan@xxxxxxxxxxxxxxxx    ::     http://www.planetfall.pwp.blueyonder.co.uk


[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