Does new () add memory for class functions?

class Animal { public: int a; double d; int f(){ return 25;} }; 

Suppose for the above code I am trying to initialize an object by saying new Animal() , does this new() memory for the f() function?

In other words, what's the difference in memory allocation expressions if I had this class instead and made new Animal() ? :

 class Animal { public: int a; double d; }; 
+5
c ++ memory-management object new-operator
source share
2 answers

Not. Functions exist on a text page, so no space is allocated for them.

+3
source share

For a class without virtual functions, the function itself does not occupy the data space. Functions are sections of code that can be executed to manipulate data. Data items must be highlighted.

When you have a virtual class, there is often an extra pointer to the virtual table. Note that vtable is a specific implementation. Although most compilers use them, you cannot count on the fact that it is always there.

I added to this answer on your other question.

+7
source share

All Articles