Memory Layout/Alignment issues/sizeof for a class in C++

Discussion in 'C++' started by mathemagic, Feb 11, 2007.

  1. mathemagic

    mathemagic Guest

    Hello All,

    I apologise if this has been covered by an earlier post.

    I just wanted to know how the functions of a class are tied to the
    class.

    ie, in C structs, since u can have only data members,
    sizeof(some_C_struct) gives u the cumulative size of each of the
    individual members plus whatever bytes were used for alignment padding
    (the rules for which of course are different for Windows, Linux, 32-
    bit, and 64-bit machines, but thats a different story).

    Now say I have a class with some 10 instances, and the behaviour of
    the class is defined by some functions.

    Obviously, there is only one copy of the functions used by all the
    objects.

    But, whenever a function is called for a particular instance of a
    class, the magical 'this' pointer is inserted as the first argument
    and the function is hence called for that particular object, so,
    voila, u get per-instance behaviour, but where is the 'this' pointer
    stored in the first place? Why is it not part of the object? I first
    thought that each object would at least store the function pointer for
    the functions, but sizeof doesnt show this to be true. So, how are the
    functions tied to each of the objects?

    To help u understand where am coming from and where am getting at:

    struct some_C_struct {
    int i;
    char c;
    }struc;

    so sizeof(struc) = 8 because of the first 4 bytes for the int, the
    next byte for the char and the remaning 3 bytes of empty padding to
    make it 8byte aligned.

    Now,

    class some_Cpp_class {
    int i;
    char c;

    public:
    some_Cpp_class() {
    /*do something to initialize*/
    }

    some_other_member_function() {
    }

    }obj1;

    Now, sizeof(obj1) for this too gives 8, assuming the same rules as
    earlier. So where is the this pointer stored? How is it then tied to
    this object? What about the function pointers? And anything else that
    I might have missed out?

    Thanks,

    --Aditya.
     
    mathemagic, Feb 11, 2007
    #1
    1. Advertising

  2. mathemagic

    mathemagic Guest

    Re: Memory Layout/Alignment issues/sizeof for a class in C++

    On Feb 10, 8:59 pm, "mathemagic" <> wrote:
    > Hello All,
    >
    > I apologise if this has been covered by an earlier post.
    >
    > I just wanted to know how the functions of a class are tied to the
    > class.
    >
    > ie, in C structs, since u can have only data members,
    > sizeof(some_C_struct) gives u the cumulative size of each of the
    > individual members plus whatever bytes were used for alignment padding
    > (the rules for which of course are different for Windows, Linux, 32-
    > bit, and 64-bit machines, but thats a different story).
    >
    > Now say I have a class with some 10 instances, and the behaviour of
    > the class is defined by some functions.
    >
    > Obviously, there is only one copy of the functions used by all the
    > objects.
    >
    > But, whenever a function is called for a particular instance of a
    > class, the magical 'this' pointer is inserted as the first argument
    > and the function is hence called for that particular object, so,
    > voila, u get per-instance behaviour, but where is the 'this' pointer
    > stored in the first place? Why is it not part of the object? I first
    > thought that each object would at least store the function pointer for
    > the functions, but sizeof doesnt show this to be true. So, how are the
    > functions tied to each of the objects?
    >
    > To help u understand where am coming from and where am getting at:
    >
    > struct some_C_struct {
    > int i;
    > char c;
    >
    > }struc;
    >
    > so sizeof(struc) = 8 because of the first 4 bytes for the int, the
    > next byte for the char and the remaning 3 bytes of empty padding to
    > make it 8byte aligned.
    >
    > Now,
    >
    > class some_Cpp_class {
    > int i;
    > char c;
    >
    > public:
    > some_Cpp_class() {
    > /*do something to initialize*/
    >
    > }
    >
    > some_other_member_function() {
    >
    > }
    > }obj1;
    >
    > Now, sizeof(obj1) for this too gives 8, assuming the same rules as
    > earlier. So where is the this pointer stored? How is it then tied to
    > this object? What about the function pointers? And anything else that
    > I might have missed out?
    >
    > Thanks,
    >
    > --Aditya.


    sorry that shud be "void some_other_member_function()"
     
    mathemagic, Feb 11, 2007
    #2
    1. Advertising

  3. mathemagic

    Ian Collins Guest

    mathemagic wrote:
    >
    > Obviously, there is only one copy of the functions used by all the
    > objects.
    >
    > But, whenever a function is called for a particular instance of a
    > class, the magical 'this' pointer is inserted as the first argument
    > and the function is hence called for that particular object, so,
    > voila, u get per-instance behaviour, but where is the 'this' pointer
    > stored in the first place? Why is it not part of the object? I first
    > thought that each object would at least store the function pointer for
    > the functions, but sizeof doesnt show this to be true. So, how are the
    > functions tied to each of the objects?
    >

    Please don't use sill abbreviations like 'u', it's a distraction that
    makes your message hard to parse.

    You have answered your own question, to quote "Obviously, there is only
    one copy of the functions used by all the objects." so the object
    instance has to be passed to the function. The this pointer *is* the
    object.

    --
    Ian Collins.
     
    Ian Collins, Feb 11, 2007
    #3
  4. mathemagic wrote:
    > But, whenever a function is called for a particular instance of a
    > class, the magical 'this' pointer is inserted as the first argument
    > and the function is hence called for that particular object, so,
    > voila, u get per-instance behaviour, but where is the 'this' pointer
    > stored in the first place? Why is it not part of the object?


    It is passed as a parameter to each function. You may think of it as
    being stored as a stack variable for the scope of the function.


    I first
    > thought that each object would at least store the function pointer for
    > the functions, but sizeof doesnt show this to be true. So, how are the
    > functions tied to each of the objects?


    What function pointer? No function pointer is used to call a class
    function. It is a plain old call, just like C, just like assembly language.

    --
    Scott McPhillips [VC++ MVP]
     
    Scott McPhillips [MVP], Feb 11, 2007
    #4
  5. mathemagic

    mathemagic Guest

    Re: Memory Layout/Alignment issues/sizeof for a class in C++

    On Feb 11, 12:16 am, "Scott McPhillips [MVP]" <org-dot-mvps-at-
    scottmcp> wrote:
    > mathemagic wrote:
    > > But, whenever a function is called for a particular instance of a
    > > class, the magical 'this' pointer is inserted as the first argument
    > > and the function is hence called for that particular object, so,
    > > voila, u get per-instance behaviour, but where is the 'this' pointer
    > > stored in the first place? Why is it not part of the object?

    >
    > It is passed as a parameter to each function. You may think of it as
    > being stored as a stack variable for the scope of the function.
    >
    > I first
    >
    > > thought that each object would at least store the function pointer for
    > > the functions, but sizeof doesnt show this to be true. So, how are the
    > > functions tied to each of the objects?

    >
    > What function pointer? No function pointer is used to call a class
    > function. It is a plain old call, just like C, just like assembly language.
    >
    > --
    > Scott McPhillips [VC++ MVP]


    cool, thanks to both Ian and Scott...

    --Aditya.
     
    mathemagic, Feb 11, 2007
    #5
  6. mathemagic

    Alan Johnson Guest

    mathemagic wrote:
    > Hello All,
    >
    > I apologise if this has been covered by an earlier post.
    >
    > I just wanted to know how the functions of a class are tied to the
    > class.
    >
    > ie, in C structs, since u can have only data members,
    > sizeof(some_C_struct) gives u the cumulative size of each of the
    > individual members plus whatever bytes were used for alignment padding
    > (the rules for which of course are different for Windows, Linux, 32-
    > bit, and 64-bit machines, but thats a different story).
    >
    > Now say I have a class with some 10 instances, and the behaviour of
    > the class is defined by some functions.
    >
    > Obviously, there is only one copy of the functions used by all the
    > objects.
    >
    > But, whenever a function is called for a particular instance of a
    > class, the magical 'this' pointer is inserted as the first argument
    > and the function is hence called for that particular object, so,
    > voila, u get per-instance behaviour, but where is the 'this' pointer
    > stored in the first place? Why is it not part of the object? I first
    > thought that each object would at least store the function pointer for
    > the functions, but sizeof doesnt show this to be true. So, how are the
    > functions tied to each of the objects?
    >
    > To help u understand where am coming from and where am getting at:
    >
    > struct some_C_struct {
    > int i;
    > char c;
    > }struc;
    >
    > so sizeof(struc) = 8 because of the first 4 bytes for the int, the
    > next byte for the char and the remaning 3 bytes of empty padding to
    > make it 8byte aligned.
    >
    > Now,
    >
    > class some_Cpp_class {
    > int i;
    > char c;
    >
    > public:
    > some_Cpp_class() {
    > /*do something to initialize*/
    > }
    >
    > some_other_member_function() {
    > }
    >
    > }obj1;
    >
    > Now, sizeof(obj1) for this too gives 8, assuming the same rules as
    > earlier. So where is the this pointer stored? How is it then tied to
    > this object? What about the function pointers? And anything else that
    > I might have missed out?
    >
    > Thanks,
    >
    > --Aditya.
    >


    Identify where the "this" pointer is stored in the following C program.
    Roughly speaking, C++ just adds syntactic sugar to do this for you
    automatically.

    typedef struct B_
    {
    int x ;
    int y ;
    } B ;

    void B_constructor(B * this, int x, int y)
    {
    this->x = x ;
    this->y = y ;
    }

    void B_increment(B * this)
    {
    ++this->x ;
    ++this->y ;
    }

    int main()
    {
    // In C++, the following lines would be: B b(42, 45) ;
    B b ;
    B_constructor(&b, 42, 45) ;

    // In C++, the following line would be: b.increment() ;
    B_increment(&b) ;

    return 0 ;
    }


    --
    Alan Johnson
     
    Alan Johnson, Feb 11, 2007
    #6
    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. Derek
    Replies:
    7
    Views:
    24,422
    Ron Natalie
    Oct 14, 2004
  2. Replies:
    1
    Views:
    649
    John Timney \(MVP\)
    Jun 19, 2006
  3. Trevor

    sizeof(str) or sizeof(str) - 1 ?

    Trevor, Apr 3, 2004, in forum: C Programming
    Replies:
    9
    Views:
    664
    CBFalconer
    Apr 10, 2004
  4. GRenard
    Replies:
    2
    Views:
    446
    Salt_Peter
    Nov 3, 2006
  5. Nikolai Weibull

    sizeof(Class)/sizeof(Object)

    Nikolai Weibull, Dec 31, 2004, in forum: Ruby
    Replies:
    2
    Views:
    158
    Robert Klemme
    Dec 31, 2004
Loading...

Share This Page