shreyasva
shreyasva

Reputation: 13446

Class definition and memory allocation

If definition stands for assigning memory. How come a class definition in C++ has no memory assigned until an object is instantiated.

Upvotes: 4

Views: 19106

Answers (6)

Joe.Wu
Joe.Wu

Reputation: 23

Class infomation is stored in symbol table. As following code:

#include<iostream>
#include <typeinfo>
using namespace std;
class Base {
public:
    Base() { }
    virtual void test(){ }
};
class Derived: public Base {
};

int main() {
    // print Derived's info
    Base* b = new Derived;
    const char* str = typeid(*b).name();
    cout<<str<<"'s address = "<<hex<<"0x"<<(long)str<<endl;

    // print Base's info
    str = typeid(Base).name();
    cout<<str<<"'s address = "<<hex<<"0x"<<(long)str<<endl;
    return 0;
}

Compile this file with g++ and run:

7Derived's address = 0x400e68
4Base's address = 0x400e98

Executable file is "a.out", and use "readelf -a a.out" command to get further information:

Symbol table '.symtab' contains 98 entries:
   Num:    Value          Size Type    Bind   Vis      Ndx Name
    ......
    79: 0000000000400e68     9 OBJECT  WEAK   DEFAULT   15 _ZTS7Derived
    80: 0000000000400e98     6 OBJECT  WEAK   DEFAULT   15 _ZTS4Base
    ......

So, class definition occupies some memory, and these information can be accessed by virtual table.

Upvotes: 0

S.Lott
S.Lott

Reputation: 391818

C++ Class definitions do not assign memory. class is like typedef and struct. Where did you get the idea that "definition stands for assigning memory"? Can you provide a quote or reference?

C++ Object creation (via new) assigns memory.

Upvotes: 9

SurDin
SurDin

Reputation: 3341

This is true up to a point. All classes and stucts in C/C++ have 2 places with "names" in them.

Class <Name>
{
     ...
}<Vars>;

What you do is define <Vars> variables of Class <Name>. All the Vars will have memory allocated for them, but what you usually do, is omit the <Vars> part, and you get an empty definition of variables, like writing

int;

Upvotes: 1

Brian R. Bondy
Brian R. Bondy

Reputation: 347216

The class definition gets compiled down into code. That code is part of the process image. The process image does get loaded into RAM (and hence uses up memory) by your operating system, but it is not part of your process' usable memory space.

When you create an object of your class, you are using memory in your process' usable memory space. The process' usable memory space is composed of memory at one of 2 places. The stack or the heap.

No memory is taken up for class definitions on the stack nor heap. When you create an object of a class it will always go on either the stack or heap.

Upvotes: 8

Nikolai Fetissov
Nikolai Fetissov

Reputation: 84149

Class declaration tells the compiler and runtime how much memory to allocate for each class instance, but only when requested. Class definition produces the executable code for class behavior.

Upvotes: 2

Shayan
Shayan

Reputation: 568

If you define is as a pointer to a class, C++ does not automatically allocate memory to the object. In C++ the memory management has to be done in your code which has benefits and drawbacks depending on the use case of your application.

Class* test;

The above will not allocate memory, it defaults to pointing to nothing.

Class test;

The above will be usable, but it will have local scope.

Upvotes: 0

Related Questions