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

Re: [OT] C++ question re. dyn. mem.

On Tuesday 05 August 2003 14:02, Pigeon wrote:
> On Tue, Aug 05, 2003 at 03:40:42AM +0200, Sebastian Kapfer wrote:
> > On Mon, 04 Aug 2003 05:00:12 +0200, MJM wrote:
> > > // change the way it is accessed to prove a point int * p_b = (int *)
> > > p_a;
> > > // p_a and p_b point to the same block of dyn. allocated memory;
> >
> > Do they? Watch out for inheritance, user-defined casting operators and
> > other funny stuff. C++ adds a few new meanings to the () casting syntax.
> > In general, your assumption is _wrong_.

Sorry for getting back so late.  By assumption are you referring to the 
pointers pointing to the same thing, or that deleting with p_b will correctly 
return what was allocated to p_a?  I am convinced that deleting with p_b is 
undefined.  I believe it can be managed for safety of the system but the 
practice leads to code that is confusing to read.  I think from reading other 
comments that the safest approach is to delete with the same pointer type 
that was used in the allocation.

> >
> > > // return the memory to free storage using the second pointer delete
> > > p_b;
> > >
> > > I think the free store will be maintained properly because there is a
> > > control block attached to the allocated block of storage that defines
> > > the length of the allocated memory.
> >
> > No. You have to delete the original pointer (with the original type).
> > Everything else is undefined behaviour, i.e. it could work, it could leak
> > memory (completely or partly), it could crash, or even print "42". It
> > might even work sometimes, and fail mysteriously at other times.
> Interesting. I figured that the actual allocation/deallocation would
> end up being handled by a call to malloc() / free(), and free() only
> cares about having the correct value for the (void *) pointer.
> Would it not be the case, though, in the barebones example given, that
> MJM's '_wrong_ assumption' would be correct, and 'it' would work, even
> if in a more general case it might not?

I think this can be true in some implementations.
> Or would it have been better for C++ to have been named D to stop people
> wanting to think like this?

I don't follow the logic there :-)  I don't "want" to think like this. The 
reason I asked this question is because I've been doing some rapid protyping 
in a pressure situation.  I coded a version of the example above 
inadvertently.  In calmer moments I realized what I had done.  I was worried 
about memory leaks.  I read about alloc and free in the old K&R C book and 
found how there was a hidden header on each allocated block. That block could 
well contain enough information to effect a proper freeing of the block 
regardless of how it was returned.  I don't think that the programmer can or 
should rely on such a  contrivance since it is not documented officially.

I have since modified the code to use a template where differant types are 
handled in an explicit manner.
Mike Mueller

Reply to: