Loading ...
Sorry, an error occurred while loading the content.
 

Re: [XP] book on oop and what's going on under the hood

Expand Messages
  • Ron Jeffries
    ... Typically an /instance/ is a structure, allocated contiguously in memory, typically on the heap. I ll discuss methods below. A class is an instance as
    Message 1 of 9 , Jul 4, 2004
      On Sunday, July 4, 2004, at 2:01:29 AM, eorbill wrote:

      > I'd like to find a book that talks about object oriented programming from
      > a machine level. That is, how does object oriented code get stored in
      > memory? An object is a type, also like a data structure. How does an
      > object get stored in memory?

      > What is actually going on 'under the hood' so to speak?

      > What is a class, in regards to memory, IO mechanisms, behavior?

      Typically an /instance/ is a structure, allocated contiguously in memory,
      typically on the heap. I'll discuss methods below.

      A class is an instance as well. There has to be a place to store the static
      variables (class variables as opposed to instance variables), and a way to
      point to the static class methods. The class object is, in general, a
      singleton instance of a subclass of some uberclass, often named Metaclass.
      There is some trickiness around what class Metaclass is an instance of, in
      languages that really care about being completely OO. (Smalltalk, for
      example.)

      > What is a method, in regards to how it compares to a function? How does
      > it act on an object, that is how is it bound to the object in memory?

      A method is just a function (or procedure) that takes the object as a
      hidden parameter. In class Foo we write a method bar(int mumble), and the
      procedure is implemented with two parameters, the first hidden:

      procedure bar ( Foo this, int mumble)

      Thereafter, inside the procedure, "this" refers to the instance to whom the
      message was sent.

      Typically the addresses of all the methods of an object are stored in an
      array, often called "vtable", and the method calls are reduced by the
      compiler to a branch through that vector.

      There's lots of rigmarole around subclasses and superclasses, resolving
      methods by their name, and so on. But the essence of the idea is that the
      object is a structure in memory, containing a pointer to a table of the
      methods it understands.

      > Does anyone know of a book that discusses these things?

      My upstairs library only has one book of interest, /Smalltalk, the Language
      and its Implementation/. I think that is likely out of print.

      Abelson and Sussman, /The Structure and Interpretation of Computer
      Programs/ might have some material, but that one isn't falling directly to
      my hand so I'm not sure.

      There's some interesting material a ways down in this article:
      http://www.cs.usfca.edu/~parrt/course/652/lectures/language.impl.overview.html .

      I hope others come up with some refs that are escaping me at the moment.

      Ron Jeffries
      www.XProgramming.com
      Computers are useless. They can only give you answers. -- Picasso
    • yahoogroups@jhrothjr.com
      From: eorbill Sent: Sunday, July 04, 2004 2:01 AM Subject: [XP] book on oop and what s going on
      Message 2 of 9 , Jul 4, 2004
        From: "eorbill" <mattfrechette.at.yahoo.com@...>
        Sent: Sunday, July 04, 2004 2:01 AM
        Subject: [XP] book on oop and what's going on under the hood


        > I'd like to find a book that talks about object oriented programming
        > from a machine
        > level. That is, how does object oriented code get stored in memory? An
        > object is a
        > type, also like a data structure. How does an object get stored in
        > memory?

        Different languages (and different versions of the same language)
        do it differently - there is no "one way" of doing it.

        The best simple implementation I know of is Python -
        expecially the material in these reference manuals shipped
        with the full installation: "Python/C API" and
        "Extending and Embedding".

        Many other languages optimize out the class structures,
        C++ is a good example of this. There's a lot of material
        on the net about C++ method dispatching - look for
        'vtable'.

        >
        > What is actually going on 'under the hood' so to speak?
        >
        > What is a class, in regards to memory, IO mechanisms, behavior?
        > What is a method, in regards to how it compares to a function? How
        > does it act on an
        > object, that is how is it bound to the object in memory?

        It's easier to understand this in terms of a function pointer
        than a raw function.
        Then a method is simply a struct that contains a function
        pointer and an instance pointer. For a bound method, the
        instance pointer is filled in, for an unbound method, the
        instance pointer is Nill (Null, None, etc...)

        > Does anyone know of a book that discusses these things?

        I don't know of a good survey of implementation
        techniques.

        John Roth
      • Stephen Swope
        ... You may find Stan Lippman s book, _The C++ Object Model_, to be useful. ===== -- Steve Swope technical manager, retail data warehouse Reynolds & Reynolds
        Message 3 of 9 , Jul 4, 2004
          > Does anyone know of a book that discusses these things?

          You may find Stan Lippman's book, _The C++ Object Model_, to be useful.



          =====
          --
          Steve Swope
          technical manager, retail data warehouse
          Reynolds & Reynolds
          http://www.reyrey.com




          __________________________________
          Do you Yahoo!?
          New and Improved Yahoo! Mail - 100MB free storage!
          http://promotions.yahoo.com/new_mail
        • Tony Byrne
          Hello Ron, RJ A method is just a function (or procedure) that takes the object as a RJ hidden parameter. In class Foo we write a method bar(int mumble), and
          Message 4 of 9 , Jul 4, 2004
            Hello Ron,

            RJ> A method is just a function (or procedure) that takes the object as a
            RJ> hidden parameter. In class Foo we write a method bar(int mumble), and the
            RJ> procedure is implemented with two parameters, the first hidden:

            RJ> procedure bar ( Foo this, int mumble)

            RJ> Thereafter, inside the procedure, "this" refers to the instance to whom the
            RJ> message was sent.

            FWIW, Perl has an interesting twist on this. Although as above, the
            object is not explicitly mentioned in the parameter list, it actually
            becomes the first parameter in the list and is accessed like any other
            parameter by the method. For example, say I have an instance of
            clas Foo referenced by $fooInstance. A Perl method call on
            $fooInstance looking like:

            $fooInstance->bar($mumble);

            would effectively translate to this:

            &Foo::bar($fooInstance, $mumble);

            which is very similar to Ron's example. However, the method might actually
            be implemented like this:

            sub bar
            {
            $self = shift; # $self is Perl idiom for 'this'. Note how you
            # must explicitly retrieve the instance from the parameters.

            $mumble = shift; # retrieves $mumble;

            # do stuff.
            }

            Not exactly mind blowing stuff, I'll admit. However, seeing the
            object as a not-so-hidden parameter to a method, was a light bulb
            moment for me when I was first getting my head around OO.

            Regards,

            Tony.

            --
            Tony Byrne
          • Toivo Lainevool
            ... If your you interested in Java, the Java Virtual Machine Spec is the definitive source. Its available online at: http://java.sun.com/docs/books/vmspec/ --
            Message 5 of 9 , Jul 4, 2004
              eorbill wrote:

              >I'd like to find a book that talks about object oriented programming
              >from a machine level.
              >
              If your you interested in Java, the Java Virtual Machine Spec is the
              definitive source. Its available online at:
              http://java.sun.com/docs/books/vmspec/

              --

              Toivo Lainevool
              http://www.XMLPatterns.com - Develop effective DTDs and XML Schema documents for your XML using structural design patterns.
            • Tony Nassar
              Thanks for the Perl example; I didn t know it worked that way! On the other hand, how else would it work... I second the recommendation of Stanley Lippman s
              Message 6 of 9 , Jul 5, 2004
                Thanks for the Perl example; I didn't know it worked that way! On the other hand, how else would it work...

                I second the recommendation of Stanley Lippman's Inside the C++ Object Model; ESPECIALLY if you're a converted C programmer (as I was), and you find yourself unable to trust the C++ object model. If you (or colleagues) just refuse to believe that C++ is as "fast" as C (whatever that means), definitely read this book. I found it invaluable for teaching C++: I now knew *why* things worked a certain why, and not some other way, and could explain / justify them. Knowing how things worked under the hood helped me write more idiomatic code, as odd as that may seem.

                > -----Original Message-----
                > From: Tony Byrne [mailto:yahoogroups@...]
                > Sent: Sunday, July 04, 2004 11:56 AM
                >
                > FWIW, Perl has an interesting twist on this. Although as above, the
                > object is not explicitly mentioned in the parameter list, it actually
                > becomes the first parameter in the list and is accessed like any other
                > parameter by the method. For example, say I have an instance of
                > clas Foo referenced by $fooInstance. A Perl method call on
                > $fooInstance looking like:
                >
                > $fooInstance->bar($mumble);
                >
                > would effectively translate to this:
                >
                > &Foo::bar($fooInstance, $mumble);
                >
                > which is very similar to Ron's example. However, the method
                > might actually
                > be implemented like this:
                >
                > sub bar
                > {
                > $self = shift; # $self is Perl idiom for 'this'.
                > Note how you
                > # must explicitly retrieve the
                > instance from the parameters.
                >
                > $mumble = shift; # retrieves $mumble;
                >
                > # do stuff.
                > }
              • Colin Strasser
                ... One that reads very easily is /A Little Ruby, A Lot of Objects/: http://www.visibleworkings.com/little-ruby/ The author only finished three chapters, but
                Message 7 of 9 , Jul 6, 2004
                  eorbill wrote:

                  >I'd like to find a book that talks about object oriented programming
                  >from a machine
                  >level. ...
                  >
                  >Does anyone know of a book that discusses these things?
                  >
                  >
                  >

                  One that reads very easily is /A Little Ruby, A Lot of Objects/:

                  http://www.visibleworkings.com/little-ruby/

                  The author only finished three chapters, but they're worth reading. The
                  book even has its own Yahoo! Group at
                  http://groups.yahoo.com/group/little-ruby/ . Perhaps with enough
                  encouragement, the author will resume where he left off.

                  -Colin
                Your message has been successfully submitted and would be delivered to recipients shortly.