hello

do the member functions of a class occupy memory or just the member variables occupy memory?

Recommended Answers

All 7 Replies

The information embedded within the class method has to be stored in memory somewhere, but whether it's attributable the to size of the class or not I don't know. You can try the test yourself. Declare a class with a plain old data type and a second one with the same POD and a method. Then, use the sizeof() operator to get the size of an object from each class. I suspect the size will be the same, but.....

>whether it's attributable the to size of the class or not I don't know
Only actual data (explicit data members, hidden data members, and padding) adds to the size of an object in memory. Member functions are stored elsewhere, and not duplicated for each object.

Hi,
I am Rammohan from Bangalore and working as a Technical lead in big IT firm .
Solution for your answer is follows:

I haven't misled you on that point; there is another scope called class scope, which applies to all member variables of a class. Variables with class scope occupy separate memory locations for each object; i.e., each object has its own separate set of member variables distinct from the member variables of any other objects.

Regards,
Rammohan Alampally,
<snip false signature>

Member functions add nothing to the size of a class object. However if a class has virtual functions, its objects contain some additional (with fixed size) info (so called vtbl pointer) and so sizeof(class_with_virtuals) > sizeof(same_class_without_virtuals).
It's interesting to remind: sizeof(class_or_object) must be greater than 0.
Look at:

class NoVirtuals
{
public: void f() {}
}; // sizeof(NoVirtuals) == 1 (no data at all)

class WithVirtuals
{
public: virtual void f() {}
}; // sizeof(WithVirtuals) == 4 (32-bit VC++)

Be careful: sizeof(class_object) is not a sum of the class data member sizeofs.

I'd assume this is because of the padding factor and vtable pointers like you mentioned before.

Yes, padding for right alignment not only between data members, for example:

struct Exam
{
    double tutor;
    char student;
};
// sizeof(Exam) == 16

(more delicate Standard requirements).

>I'd assume this is because of the padding factor
>and vtable pointers like you mentioned before.
It's because of a lot of things. Padding and alignment of data members are a factor, how the compiler implements runtime polymorphism, how the compiler handles virtual base classes, and any compiler optimizations.

Case in point for that last item, what would you expect the output to be for this program?

#include <iostream>

class foo {};
class bar: public virtual foo {};

int main()
{
  std::cout<< sizeof ( foo ) <<'\n';
  std::cout<< sizeof ( bar ) <<'\n';
}

Depending on whether the compiler implements a special case for empty virtual bases, and assuming a typical virtual implementation with 4 byte pointers and alignment on a word boundary, the output could be either 1 and 4 (most likely) or 1 and 8.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.