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

M

mathemagic

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.
 
M

mathemagic

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()"
 
I

Ian Collins

mathemagic said:
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.
 
S

Scott McPhillips [MVP]

mathemagic said:
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.
 
M

mathemagic

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


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.

cool, thanks to both Ian and Scott...

--Aditya.
 
A

Alan Johnson

mathemagic said:
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 ;
}
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top