This is the mail archive of the
gcc@gcc.gnu.org
mailing list for the GCC project.
Bad C++ code (was Re: C++ Garbage Collecter)
Per Bothner wrote:
have a pointer to the base object somewhere.
>
> There is one problem I can think of: Allocating a temporary object
> is only used *local* to a function:
>
> char *x = (char*) gcmalloc(100);
> ... do stuff with x ...
> /* at this point x is dead */
>
> Here you are the risk that x might be prematurely collected. The
> work-around is easy: Don't do that. Instead, use non gc-managed
> memory for objects only used internally in a single function:
>
> class malloced {
> void *p;
> malloced(void *ptr) { p = ptr; }
> ~malloced() { free(p); }
> }
>
> char *x = (char*) malloc(100);
> malloced(x);
except that malloced(x) will be deleted here (before the next line)
What you wanted to say was
malloced x0 = x;
> ... do stuff with x ...
> /* malloced's finalizer will free x. */
--
Kevin Atkinson
kevinatk@home.com
http://metalab.unc.edu/kevina/