Re: Is There Any Reason to Even Use VC++ Anymore?



Peter Oliphant wrote:
> > Honestly why use gc at all? Never got why people felt the need for a
> > garbage collector.
> Now, how can the class know whether the instance has further use externally?
> Answer: in general, it can't! A class, is typically unaware of the
> environment it is in, since many classes are designed for re-usability in
> DIFFERENT environments.

Contracts between the calling function and the destination functions
work quite nicely for me.

If I require an object in a function, I use a reference.

If, for some reason, the object needs to be worried about the life of
the object (Such as a "message" being passed), I make that a condition
of the function if it is the end-point and destroy the object.

If, for another reason, the calling function needs to allocate memory,
call a 2nd function, and then is done with the memory, it is up to the
calling function to remove the memory from play.

> An example. Say a class is responsible for displaying the color of an object
> representing a toy ball. It does this by taking a pointer to a ball object
> and displaying its color. Now, here's the problem. The caller could have
> passed a pointer to the instance of the ball itself, or a COPY of this
> instance. In the first case, the instance should not be destroyed. In the
> second case, the instance passed MUST be destroyed. But the class has no
> idea which case is true for any particular call!

If the function is responsible for drawing the ball, it should be up to
the caller function to determine whether or not the allocated object is
through. I mean granted it's around for another cycle or two until you
hit the delete function, but the cost is negligible.

I tend to shun away from using lists of pointers, and generally don't
have that big of a problem with it. I just go with the philosophy of
the allocator owns the pointer, unless it calls another function which
always assumes ownership of the allocated pointer, and that the owner
is responsible for freeing the memory.

Can you give an example where memory would have to be allocated prior
to the function, but the function would require both pass-through and
delete functionality that the calling function could not provide?

.



Relevant Pages

  • Re: Pointer to the out of scope local variables
    ... of a pointer, and jumped to the conclusion that you allocated the memory ... In this particular case the funtion buildPoint would allocate ... If the caller ...
    (microsoft.public.vc.language)
  • Re: Memory management and allocation
    ... > As I'm writing a piece of code that basically acts as a server and ... > memory management is a topic that is quite crucial. ... Or can I just allocate the variable ... Nor is it usually necessary to set the pointer to ...
    (comp.lang.c)
  • Re: Virtual Machine implementation problem, Please help me to spot the bug
    ... I kept on getting error messages. ... You don't allocate enough memory here. ... Again conversions between pointer and integer types. ...
    (comp.lang.c)
  • Re: This is getting really weird.
    ... I thought 4 bytes for reference count and 4 for string length. ... > There should be no memory allocation for that line. ... > manager may allocate more space than requested for its own efficiency. ... > that New returned with a pointer to the string constant. ...
    (alt.comp.lang.borland-delphi)
  • Re: Is this math test too easy?
    ... > communications glitch; one of the more laughable cartoons ... it was loaded into physical memory and, ... > Or one can interpret the character string as one of the values ... A pointer to an integer? ...
    (sci.math)