473,406 Members | 2,698 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

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

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.

Feb 11 '07 #1
5 2382
On Feb 10, 8:59 pm, "mathemagic" <email...@gmail.comwrote:
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()"

Feb 11 '07 #2
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.
Feb 11 '07 #3
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]

Feb 11 '07 #4
On Feb 11, 12:16 am, "Scott McPhillips [MVP]" <org-dot-mvps-at-
scottmcpwrote:
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.

Feb 11 '07 #5
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
Feb 11 '07 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

14
by: J. Campbell | last post by:
I posted a question some time back about accessing a char array as an array of words. In order not to overrun the char array, I padded it with enough 0x00 bytes to ensure that when accessed as...
2
by: Paul_Huang | last post by:
OK, I tried it with a piece of sample code to test the memory padding and alignment and get some weird results. I would appreciate if anybody can help to give a explain. Below is my sample code:...
13
by: Kutty Banerjee | last post by:
Hi, I ve got the following piece of code which does the role of allocating aligned memory (not sure what aligned memory allocation is either). void * _align_calloc(size_t bytes, unsigned long...
5
by: mikegw | last post by:
Hello all. I am currently using an implementation of sysV shared memory. The entire shared memory is allocated is one continuous block of which I get the pointer to the head, everything should...
6
by: Everton da Silva Marques | last post by:
Hi, I need to allocate, using malloc(), a single memory block to hold two structures and a variable length string. Is it safe (portable, alignment-wise) to sum up the sizeof's of those...
6
by: lovecreatesbeauty | last post by:
Hello experts, 1. Does C guarantee the data layout of the memory allocated by malloc function on the heap. I mean, for example, if I allocate a array of 100 elements of structure, can I always...
11
by: Henryk | last post by:
I have something like class Params { public: const static char nOne = 1; const static int nTwo = 2; const static char nThree = 3; }; This is just a wrapper for globally used parameters in...
1
by: Chris Thomasson | last post by:
I found some time to work a little more on my C++ allocator project. Here is some of the basic alignment code that I am thinking about using: ---------------- #include <cstdio> #include...
4
by: hugo.arregui | last post by:
Hi! I have two struts like that: struct { int num; int num2; struct b arrayOfB; } a;
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.