Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Memory Layout/Alignment issues/sizeof for a class in C++

Reply
Thread Tools

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

 
 
mathemagic
Guest
Posts: n/a
 
      02-11-2007
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.

 
Reply With Quote
 
 
 
 
mathemagic
Guest
Posts: n/a
 
      02-11-2007
On Feb 10, 8:59 pm, "mathemagic" <(E-Mail Removed)> 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()"

 
Reply With Quote
 
 
 
 
Ian Collins
Guest
Posts: n/a
 
      02-11-2007
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.
 
Reply With Quote
 
Scott McPhillips [MVP]
Guest
Posts: n/a
 
      02-11-2007
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]

 
Reply With Quote
 
mathemagic
Guest
Posts: n/a
 
      02-11-2007
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.

 
Reply With Quote
 
Alan Johnson
Guest
Posts: n/a
 
      02-11-2007
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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Class A contains class B, class B points to class A Joseph Turian C++ 5 12-30-2005 03:24 PM
Nested Class, Member Class, Inner Class, Local Class, Anonymous Class E11 Java 1 10-12-2005 03:34 PM
A parameterized class (i.e. template class / class template) is not a class? christopher diggins C++ 16 05-04-2005 12:26 AM
Differences between Sony Memory Stick & memory Stick Pro vs Memory Stick Duo? zxcvar Digital Photography 3 11-28-2004 10:48 PM
instanciate a class in a jar file with class.forname, while my main class is in another jar cyril Java 2 08-25-2004 06:55 AM



Advertisments