Hi Friends,
What will be solution in the fallowing code
Class A{}
void func(){}
printf("%d,%d",sizeof(A),sizeof(func));
Thanks in advance
Prashant Dubey
Hi Friends,
What will be solution in the fallowing code
Class A{}
void func(){}
printf("%d,%d",sizeof(A),sizeof(func));
Thanks in advance
Prashant Dubey
Why don't you just test it out? The function will be probably be promoted to a pointer.
You are taking the size of a function which cannot be done. You need to preceede the name with a &
to take the size of a pointer to that function. You also need to cast the sizeof
value to be of type int
, which is what printf
expects for the d
specifier
printf("%d,%d", (int)sizeof(A), (int)sizeof(&func));
As for the concrete values ---- It's not known what they are beyond that are greater or equal to 1
. It depends on the compiler.
Size of an empty class is non zero(most probably 1), so as to have two objects of the class at different addresses.
http://www2.research.att.com/~bs/bs_faq2.html#sizeof-empty explains it better
class A{};
void func(){}
std::cout<<sizeof(A)<<std::endl<<sizeof(&func));// prints 1 and 4 on my 32 bit system
By sizeof(func)
, you probably mean sizeof(&func)
, which is the size of a function pointer in bytes.
As far as what the size of an A
object is, the standard only says:
Complete objects and member subobjects of class type shall have nonzero size.
which is qualified by footnote 94:
Base class subobjects are not so constrained.
The reason for the footnote is to allow for a compiler optimization known as empty member optimization, which is commonly used in template libraries to access routines of a class type (e.g. allocator routines of an allocator class type) without needing a member object of that class type.