Memory size of a method

M

mojumbo

I am passing around a structure, say:

tsBob
{
int a;
int b;
int bobMethod(bool lb);

};

I would like to make sure the structure is aligned along a 64-bit
boundary (it's being thrown around the system). I assumed it was the
size of a pointer (32-bits) but when I googled & wrote a little driver
(See below) it was only 1 byte. Since I can't believe this does
anyone have an answer?

int main(int argc, char* argv[])
{
tsBob aBob;

cout << "Size of bobMethod is " << sizeof(aBob.bobMethod) << endl;

return 0;

}
 
R

Richard Herring

In message
I am passing around a structure, say:

tsBob
{
int a;
int b;
int bobMethod(bool lb);

};

I would like to make sure the structure is aligned along a 64-bit
boundary (it's being thrown around the system). I assumed it was the
size of a pointer (32-bits)

Why do you assume a member function needs to occupy any memory at all
within individual instances of the class?
 
M

Matthias Buelow

mojumbo said:
I am passing around a structure, say:

tsBob
{
int a;
int b;
int bobMethod(bool lb);

};

I would like to make sure the structure is aligned along a 64-bit
boundary (it's being thrown around the system).

Function members do not contribute to a structure/class's size.
If you call aBob->bobMethod(true), the compiler essentially
underhandedly rewrites the expression into something similar to
bobMethod(aBob, true) in an unambiguous way.
I assumed it was the
size of a pointer (32-bits)

A function is not a function pointer.
 
J

James Kanze

I am passing around a structure, say:
tsBob
{
int a;
int b;
int bobMethod(bool lb);
};
I would like to make sure the structure is aligned along a 64-bit
boundary (it's being thrown around the system).

Generally speaking, it's the compiler's job to ensure proper
alignment, not yours.
I assumed it was the size of a pointer (32-bits) but when I
googled & wrote a little driver (See below) it was only 1
byte. Since I can't believe this does anyone have an answer?
int main(int argc, char* argv[])
{
tsBob aBob;
cout << "Size of bobMethod is " << sizeof(aBob.bobMethod) << endl;
return 0;
}

This shouldn't compile; in C++, functions don't have any size
(or at least, you can't use sizeof on them). And of course,
something like 'aBob.bobMethod' is only a legal expression if it
is an operand to a function call operator, i.e.
aBob.bobMethod().
 

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

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,059
Latest member
cryptoseoagencies

Latest Threads

Top