[comp.lang.c++] this and 0

leo@atcmp.nl (Leo Willems) (06/08/90)

Since C++ 2.0 assignment to this is an anachronism.

Is it still ok to check the this pointer against 0 to figure out if
an object is on the heap or not?
I need to know this, despite the fact that I can define my
own class::operator new(), currently I'm using an ugly workaround.

If so, will this be the case in next releases?

If so, is there still the "all possible execution paths" catch?


Thanks, (I couldn't find it in the 2.0 syntax reference)


 Leo Willems			Internet: leo@atcmp.nl
 AT Computing			UUCP:     mcsun!hp4nl!kunivv1!atcmpe!leo
 P. O. Box 1428				
 6501 BK  Nijmegen		Phone:    +31-80-566880
 The Netherlands		Fax:	  +31-80-555887

pkturner@cup.portal.com (Prescott K Turner) (06/13/90)

leo@atcmp.nl (Leo  Willems) writes:

> Is it still ok to check the this pointer against 0 to figure out if
> an object is on the heap or not?

Cfront 2.0 and Turbo C++ still support assignment to 'this'.  
But what you have in mind, i.e. to test 'this' against 0 in a constructor
without assigning to 'this', never worked.
--
Prescott K. Turner, Jr.
Language Processors, Inc.
959 Concord St., Framingham, MA 01701 USA    (508) 626-0006 x232
UUCP: ...sun!cup.portal.com!pkturner    Internet: pkturner@cup.portal.com

                    

leo@atcmp.nl (Leo Willems) (06/14/90)

From article <30734@cup.portal.com>, by pkturner@cup.portal.com (Prescott K Turner):
> leo@atcmp.nl (Leo  Willems) writes:
> 
>> Is it still ok to check the this pointer against 0 to figure out if
>> an object is on the heap or not?
> 
> Cfront 2.0 and Turbo C++ still support assignment to 'this'.  
> But what you have in mind, i.e. to test 'this' against 0 in a constructor
> without assigning to 'this', never worked.
> --

No, what I had in mind was checking <this> (and having to assign to <this>,
to make the check work) against 0 to decide if my object is made
trough new (or not).
The penalty for the check was that I had to malloc()/free() myself.

The reason I posted this question was, to be more precise: How to find out
if an object is created through new, or is an auto or static (global)?
This check must be done in the class its constructor.
(restriction: I cannot redefine new for the class.)

Up to now nobody gave me a clue to solve that.

Leo

thomasw@hpcupt1.HP.COM (Thomas Wang) (06/14/90)

/ hpcupt1:comp.lang.c++ / pkturner@cup.portal.com (Prescott K Turner) /  9:20 pm  Jun 12, 1990 /
>leo@atcmp.nl (Leo  Willems) writes:

>> Is it still ok to check the this pointer against 0 to figure out if
>> an object is on the heap or not?

>Cfront 2.0 and Turbo C++ still support assignment to 'this'.  
>But what you have in mind, i.e. to test 'this' against 0 in a constructor
>without assigning to 'this', never worked.

This is what I use currently, until a better way appears.

// explain function
// Is this object allocated from the stack?
// This code is machine dependent.  It assumes stack have a higher address
// than heap space.
char base_ref_t::is_stack_object()
{
  char x;
  return ((unsigned int) this) > ((unsigned int) &x);
}

>Prescott K. Turner, Jr.
>UUCP: ...sun!cup.portal.com!pkturner    Internet: pkturner@cup.portal.com