Jump to content
  • Advertisement
Sign in to follow this  
Silly_con

Sizeof an instance and friend methods

This topic is 5455 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

If you have a class with much methods defined, the size of the instanced objects grow up or only affect the size the variables ? if the size is affected by functions, friend functions contribute too to this size ? How can acces to the private members of a class inside a friend method ? (I cant)
class C {
  private: 
    int n;
  public:
    friend void func(int a);
};

void func(int a) {
  C::n = a;
}

Share this post


Link to post
Share on other sites
Advertisement
The number of non-virtual functions, friend or otherwise, has no impact on the size of the class. Classes with any number of virtual functions add a vtbl pointer to the class, the size therefore increases by 'sizeof (void *)'. Additionally, a single vtbl needs to be created so there's a one off overhead that is dependant on the number of virtual functions.

In your friend function, you need to supply an instance of the object, there is no 'this' pointer passed to friends.


friend void func(C &c, int a)
{
c.n = a;
}

Skizz

Share this post


Link to post
Share on other sites
Non-virtual functions don't affect sizeof at all.
Adding a virtual function to a class increases its size by 1 pointer (pointer to v-table).
A friend function needs to take an instance of the class (or a pointer to an instance, or reference) to act upon (unless you only want to change a static member variable).

void func(C& cInstanceRef, int a)
{
cInstanceRef.n = a;
}

EDIT: Spotted the deliberate mistake.

Share this post


Link to post
Share on other sites
You need an instance of the class


void func(int a) {
C c;
c.n = a;
}


The member data is all that affects the size of the objects that the class creates. Functions are defined in one place and don't make the object any heavier or lighter.

Share this post


Link to post
Share on other sites
Quote:
Original post by Paradigm Shifter
Non-virtual functions don't affect sizeof at all.
Adding a virtual function to a class increases its size by 1 pointer (pointer to v-table).


That's slightly incorrect. Consider a class like this:

class A
{
char c;
};

Then sizeof(A) should usually return 1, since sizeof(char) is defined to be 1 in the C++ standard.

If you add virtual functions like this:

class B
{
char c;
virtual void func();
};

then a hidden void* (for the vtable) is prepended. Because of alignment considerations, the compiler has to add padding bytes to the class. That's because someone might create an array of B's and because of pointer arithmetic.
For 32 bit system this would mean, that sizeof(B) becomes 8, and not 5.

Share this post


Link to post
Share on other sites
I'd just like to point out that what nmi just said is incorrect in some ways. sizeof (char) might always return 1 as defined in the C/C++ ansi standard, but it is one storage unit, which might be one byte, two bytes or whatever, where a byte is 8 bits (sizeof doesn't measure bytes but storage units!)
Also, compilers don't have to pad structures/classes so the example given can use 5 bytes per instance. The following was compiled using MSVC V6:

??=include<iostream>
#pragma pack (1)
class A ??<public: virtual void f (void) ??<??> char a; ??>;
char s??(??)="sizeof A = ";
int main (int argc, char *argv ??(??))??<std::cout << s << sizeof (A);return 0;??>

and running the resulting executable displays "sizeof A = 5". It is perfectly OK to create an array of this type, the compiler would probably generate the following:

// i = array_of_A [j].a
mov eax,j
lea edx,[eax+eax*4+array_of_A]
movsx eax,byte ptr [edx+offset(a)]


Skizz

Share this post


Link to post
Share on other sites
Quote:
Original post by Skizz
I'd just like to point out that what nmi just said is incorrect in some ways. sizeof (char) might always return 1 as defined in the C/C++ ansi standard, but it is one storage unit, which might be one byte, two bytes or whatever, where a byte is 8 bits (sizeof doesn't measure bytes but storage units!)

Wrong. See 5.3.3 - 1 of the ISO 14882-1998 standard. sizeof will return the size in bytes.
Quote:

Also, compilers don't have to pad structures/classes so the example given can use 5 bytes per instance. The following was compiled using MSVC V6:

??=include<iostream>
#pragma pack (1)
class A ??<public: virtual void f (void) ??<??> char a; ??>;
char s??(??)="sizeof A = ";
int main (int argc, char *argv ??(??))??<std::cout << s << sizeof (A);return 0;??>

and running the resulting executable displays "sizeof A = 5". It is perfectly OK to create an array of this type, the compiler would probably generate the following:

// i = array_of_A [j].a
mov eax,j
lea edx,[eax+eax*4+array_of_A]
movsx eax,byte ptr [edx+offset(a)]


Skizz

While that is true, you should never rely on your 'knowledge' as to how large something is. Always use sizeof.

Share this post


Link to post
Share on other sites
Quote:
Original post by Washu
Wrong. See 5.3.3 - 1 of the ISO 14882-1998 standard. sizeof will return the size in bytes.

While it's wrong, I agree with it completely. ;) I've used this example on here before. A TMS320C30 processor has sizeof(char)==sizeof(short)==sizeof(long)==sizeof(float)==sizeof(double)==sizeof(void*)==1. Each address is a unique 32 bit value. Offsetting the address by 1 doesn't give you 3/4 of one long and 1/4 of another. It gives you an entirely different set of memory.

In this case, sizeof() and offsetof() use addressable units, not bytes as most people think of them... Unless you want to say this architecture has 32 bit bytes, and that 8 bits is an octet (SNMP speak for byte)... And that's just what the spec says. A byte can hold the alphabet, but the exact number of bits is not defined, and each byte has a unique address.

In other words, "BYTE" in C++ speak means "addressable unit" to most people. Most people think of BYTE as 8 bits. That's the usual defintion. The size of the unit that changes from machine to machine has always been called a "word", except in C/C++, where a word often means 16 bit, but not necessarily. I'm sure you've heard of odd systems that have had a 12-bit word or a 13-bit word. I've never heard anyone say one of these systems had a 12-bit byte, and I've often heard that a byte is 8 bits.

So, you're both right as far as I'm concerned. The C++ spec calls for bytes, but specifies bytes are addressable units. C++'s spec. just bastardizes the old usage of the terms "byte" and "word". Blame Bjarne, or K&R.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!