0
class Test
{
   void func(){int i;}
};

int main()
{
   cout<<sizeof(Test)<<endl; //gives 1
}

Why doesn't sizeof(Test) consider the size of the function. Where does the function really stored with its body? Couldn't find any link in Google explaining this.

Edit: It would be good if any good link is provided except wiki regarding the sections inside a class.

Sreeraj Chundayil
  • 5,548
  • 3
  • 29
  • 68

3 Answers3

11

If it did consider the size of the function, sizeof would be useless for its intended purpose. For example, you might use sizeof(Test) to know how much memory to allocate to hold an instance of the object.

But that instance doesn't contain a copy of the function since the function is identical for each instance. The need to store precisely one copy of the function in case it's needed isn't affected by the number of objects that exist.

David Schwartz
  • 179,497
  • 17
  • 214
  • 278
  • Consider I have only one object, and I want to find the sizeof that object. So here it should give the complete sizeof the object,because it is the size of the memory the object takes in RAM? – Sreeraj Chundayil Dec 10 '14 at 08:45
  • 1
    Why does the function have to be placed in RAM? It could be placed in ROM. And then what .. the result of `sizeof` should change? What happens if you delete the first object, the one that contains the function? That's just not a reasonable way to do things. Plus, where would it get the function from to put it in the object? If it didn't have it, it's screwed. If it had it, why does it need to put it anywhere else? – David Schwartz Dec 10 '14 at 08:45
  • @InQusitive Functions can be executed from ROM, or RAM which has been configured to look like ROM, which is typical in modern OSes. – Potatoswatter Dec 10 '14 at 08:48
  • I wasn't asking about the RAM,ROM part. Just read the first sentence. – Sreeraj Chundayil Dec 10 '14 at 08:50
  • @InQusitive No, because the function needs to be stored even if no objects exist. Otherwise, what would the implementation do if an object was created? Where would it get the function from? The need to store precisely one copy of the function in case it's needed isn't affected by the number of objects that exist. – David Schwartz Dec 10 '14 at 08:51
  • Is there any link which can provide information regarding the sections inside class? – Sreeraj Chundayil Dec 10 '14 at 08:52
  • 1
    @InQusitive, think not in terms of how much space is taken up by the code and data. Think in terms of the _incremental_ cost. The _code_ is there no matter what, so it's a sunk cost. Each additional object of that class will only add its data portion which, for your example, would be zero were it not for the fact that it must be at least one (to handle arrays intelligently among other things). – paxdiablo Dec 10 '14 at 08:57
2

sizeof returns the size of an object of this type.

An object consists of two parts: first the data, second the type information (the class). The first part only contains the data (and possibly some type information), this you can see as an actual object. The class part is stored separately (loaded from the executable and once per class so shared over possibly many objects). The function (and more) are stored in the class part.

Thirler
  • 20,239
  • 14
  • 63
  • 92
  • 1
    This doesn't even mention functions, and type information isn't necessarily stored anywhere in C++. – Potatoswatter Dec 10 '14 at 08:45
  • 1
    The last sentence mentioned that the functions are stored in the class part. Type information is compiler defined (and can indeed not be present in the object). – Thirler Dec 10 '14 at 08:54
  • Then that suggests the function (and the representation of the class) is part of the object, which it's not. Functions, vtables, and any similar stuff can be mixed together with those of other classes and divided among various linker sections. The "class part" is not a cohesive unit. This particular function is inline, and it's never used in a way that requires the linker to process it at all. On the other hand, the "class part" or "type information" of an object is typically a single pointer to a vtable structure. – Potatoswatter Dec 10 '14 at 09:16
2

The function body is code. Code is stored in the code segment of your program's memory. It doesn't add up to the size of the class' objects, therefore you end up with the minimum size of an instance of an empty class in your example, which is 1.

Note that the sizeof operator evaluates to the size in bytes of the object representation of the type you pass.

For reference:

Community
  • 1
  • 1
moooeeeep
  • 31,622
  • 22
  • 98
  • 187