schmid@jellosub.luftfahrt.uni-stuttgart.de (Georg Schmid) (10/09/90)
Just a simple question:
One of my c++ books (Bruce Eckel, Using C++) states that one
can determine the type (heap- or stack-based) of an instance
at runtime by looking at 'this' in the constructor:
"... code is secretly generated in a constructor to differ-
entiate between objects created on the stack (the value of
'this' when the constructor is called is non-zero) and objects
created on the free store (the value of 'this' when the con-
structor is called is zero)..."
I tested this with the following little program on an Apollo
with Apollos (Glockenspiel) C++ 1.2.2:
#include <stdio.h>
class Something
{
public:
Something();
};
Something::Something()
{
printf("This is %lX\n", this);
}
int main(int, char**)
{
Something a;
Something* b = new Something;
}
The output is:
This is 3B3C7556
This is 10C00
Am I wrong, is Eckel wrong or is the compiler wrong ?
Any hints appreciated.
-- georg
________________________________________________________________________
Georg Schmid, ISD University of Stuttgart, Germany
email: schmid@asterix.luftfahrt.uni-stuttgart.de (129.69.110.2)
voice: +49 711-685-2053
fax: +49 711-685-3706
________________________________________________________________________bjaspan@athena.mit.edu (Barr3y Jaspan) (10/10/90)
|> "... code is secretly generated in a constructor to differ- |> entiate between objects created on the stack (the value of |> 'this' when the constructor is called is non-zero) and objects |> created on the free store (the value of 'this' when the con- |> structor is called is zero)..." Ack! Retch! Perhaps he is right about a particular compiler on a particular machine, but that does *not* sound like something you can depend on across machines and implementations. "Code is secretly generated" for a reason, namely, to hide an implementation detail. (BTW, I can tell you that the compilers I've worked with (g++ and cfront 2.0) have an implicit argument to member functions that conveys the information you mentioned. The compiler should not allow you to access it, however.) Barr3y Jaspan, bjaspan@athena.mit.edu
brucee@alice.att.com (Bruce Ellis) (10/11/90)
Geroge Schmid says:
> One of my c++ books (Bruce Eckel, Using C++) states that one
etc etc
Don't buy this book. I flipped through it in a book store and
decided it was probably pretty decent. I bought it. I read it.
It contains a lot of bad explanations and bad examples.
An early example program 'detabs' a file my replacing tabs
religously with 8 spaces. Not a good solution.
Don't buy this book.
------
This is not necessarily the view of my employer.roman@hri.com (Roman Budzianowski) (10/11/90)
|> "... code is secretly generated in a constructor to differ- |> entiate between objects created on the stack (the value of |> 'this' when the constructor is called is non-zero) and objects |> created on the free store (the value of 'this' when the con- |> structor is called is zero)..." Ack! Retch! You don't have to look at anything secret to determine if an object is on the stack. Compare the value of 'this' to some value you know is on the stack. The comparison is machine dependent and nonportable.