Objects in memory

Discussion in 'C++' started by rafal_@op.pl, Nov 2, 2007.

  1. Guest

    class C1
    {
    int a;
    void f();
    }
    class C2:public C1
    {
    int b;
    void f();
    }

    Memory:
    --------------------------
    Data of object C1
    int a;
    --------------------------
    --------------------------
    Data of object C2
    int b;
    --------------------------
    where are addresses of methods ?
    , Nov 2, 2007
    #1
    1. Advertising

  2. benben Guest

    wrote:
    > class C1
    > {
    > int a;
    > void f();
    > }
    > class C2:public C1
    > {
    > int b;
    > void f();
    > }


    class definition must be terminated by a semicolon.

    >
    > Memory:
    > --------------------------
    > Data of object C1
    > int a;
    > --------------------------
    > --------------------------
    > Data of object C2
    > int b;
    > --------------------------
    > where are addresses of methods ?
    >


    Short answer: you need to do some research on memory management.

    Slightly more elaborate answer:

    When the compiler produces a program out of the source containing
    classes C1 and C2, it saves the target code of C1::f and C2::f. When
    the program is loaded by an operating system this piece of target code
    is mapped into a piece of memory called static memory.

    Only when the program instantiate an object of either C1 or C2, the
    internal data C1::a and C2::a will be stored in memory. If the object is
    global, namespace, static, then it will be put in the static memory. If
    the object is a local object, it will be stored in the program stack.

    Regards,
    Ben
    benben, Nov 2, 2007
    #2
    1. Advertising

  3. Hi,

    Whenever a Application is executed, it gets memory space inside Main
    Memory, which is devided into several parts logically, from which two
    parts are "Data Segment", "Code Segment".
    So all the variables risides inside Data Segment.

    And ALL METHODS RESIDES INSIDE CODE SEGMENT.
    ---------------------------------------------------------X-----x-------
    X-----------------------------------------------------------
    further queries are invited...





    wrote:

    > class C1
    > {
    > int a;
    > void f();
    > }
    > class C2:public C1
    > {
    > int b;
    > void f();
    > }
    >
    > Memory:
    > --------------------------
    > Data of object C1
    > int a;
    > --------------------------
    > --------------------------
    > Data of object C2
    > int b;
    > --------------------------
    > where are addresses of methods ?
    bhupendra yadav, Nov 2, 2007
    #3
  4. wrote:
    > ...
    > Memory:
    > --------------------------
    > Data of object C1
    > int a;
    > --------------------------
    > --------------------------
    > Data of object C2
    > int b;
    > --------------------------
    > where are addresses of methods ?
    > ...


    There are no "addresses of methods" there. What makes you think they
    should be there in the first place?

    --
    Best regards,
    Andrey Tarasevich
    Andrey Tarasevich, Nov 2, 2007
    #4
  5. Ian Collins Guest

    Andrey Tarasevich wrote:
    > wrote:
    >> ...
    >> Memory:
    >> --------------------------
    >> Data of object C1
    >> int a;
    >> --------------------------
    >> --------------------------
    >> Data of object C2
    >> int b;
    >> --------------------------
    >> where are addresses of methods ?
    >> ...

    >
    > There are no "addresses of methods" there. What makes you think they
    > should be there in the first place?
    >

    Class methods are functions and functions have an address.

    To the OP, the location of objects in memory isn't defined by the
    standard, if you want to know the address of a member function, print it!

    > --
    > Best regards,
    > Andrey Tarasevich
    >

    You sig is missing the space after the "--".

    --
    Ian Collins.
    Ian Collins, Nov 2, 2007
    #5
  6. Ian Collins wrote:
    >>> ...
    >>> Memory:
    >>> --------------------------
    >>> Data of object C1
    >>> int a;
    >>> --------------------------
    >>> --------------------------
    >>> Data of object C2
    >>> int b;
    >>> --------------------------
    >>> where are addresses of methods ?
    >>> ...

    >>
    >> There are no "addresses of methods" there. What makes you think they
    >> should be there in the first place?
    >>

    > Class methods are functions and functions have an address...


    Just because something "has an address" doesn't mean that a question of
    "where" is applicable to that address. In the context of the OP's
    question it is clear [enough] that the "where" question he's asking is
    applicable to lvalues only (since it is clear that his "where" actually
    refers to memory locations). Nothing in the OP's post suggests the
    existence of addresses of the methods as lvalues, hence my questions.

    --
    Best regards,
    Andrey Tarasevich
    Andrey Tarasevich, Nov 2, 2007
    #6
  7. On Nov 2, 8:01 am, wrote:
    > class C1
    > {
    > int a;
    > void f();}
    >
    > class C2:public C1
    > {
    > int b;
    > void f();
    >
    > }
    >
    > Memory:
    > --------------------------
    > Data of object C1
    > int a;
    > --------------------------
    > --------------------------
    > Data of object C2
    > int b;
    > --------------------------
    > where are addresses of methods ?


    In most cases, methods are equivalent to C functions with the an
    addition this pointer passed to them.

    The following articles explain C++ constructs in terms of C code:

    http://www.eventhelix.com/RealtimeMantra/basics/ComparingCPPAndCPerformance.htm

    http://www.eventhelix.com/RealtimeMantra/basics/ComparingCPPAndCPerformance2.htm

    --
    EventStudio - http://www.Eventhelix.com/Eventstudio/
    Sequence diagram based systems engineering tool
    EventHelix.com, Nov 3, 2007
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. bigbinc
    Replies:
    3
    Views:
    395
    Michael Borgwardt
    Nov 18, 2003
  2. Simon Elliott

    Inheritance of objects within objects

    Simon Elliott, Dec 10, 2004, in forum: C++
    Replies:
    2
    Views:
    330
    Simon Elliott
    Dec 10, 2004
  3. =?Utf-8?B?c3RldmVuIHNjYWlmZQ==?=

    form objects into class objects

    =?Utf-8?B?c3RldmVuIHNjYWlmZQ==?=, Jul 5, 2006, in forum: ASP .Net
    Replies:
    1
    Views:
    429
    =?Utf-8?B?c3RldmVuIHNjYWlmZQ==?=
    Jul 6, 2006
  4. Replies:
    4
    Views:
    569
    Joseph Kesselman
    Aug 10, 2006
  5. 7stud
    Replies:
    11
    Views:
    682
    Dennis Lee Bieber
    Mar 20, 2007
Loading...

Share This Page