4

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

shreyasva
  • 13,126
  • 25
  • 78
  • 101

6 Answers6

9

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.

S.Lott
  • 384,516
  • 81
  • 508
  • 779
  • a C book, it said that declaration tells the type of data and definition assigns memory to it – shreyasva Feb 17 '10 at 15:42
  • A declaration _of data_ tells you the type of data; a definition _of data_ assigns memory for it. There are other declarations and definitions, like these. – MSalters Feb 17 '10 at 15:54
  • @user265260: Don't conflate variable declaration with type definition. `class` is like `typedef` and `struct`. It is not like a variable declaration at all. – S.Lott Feb 17 '10 at 15:54
  • @user265260 What "C book" would that be? –  Feb 17 '10 at 16:15
  • "Let us C" by yashwanth kanetkar – shreyasva Feb 17 '10 at 17:09
  • @user265260: Please update your question with these additional facts. DOn't add comments to an answer: update the question. – S.Lott Feb 18 '10 at 11:09
7

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.

Community
  • 1
  • 1
Brian R. Bondy
  • 339,232
  • 124
  • 596
  • 636
  • There are more places than those 2. [GotW #9](http://www.gotw.ca/gotw/009.htm) lists all 5. – MSalters Feb 17 '10 at 15:58
  • @MSalters: Agreed but for the context of this question I think this is enough. And also I said usable memory space, so a lot of the things mentioned in the link are not applicable. – Brian R. Bondy Feb 17 '10 at 15:59
2

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.

Nikolai Fetissov
  • 82,306
  • 11
  • 110
  • 171
1

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;
SurDin
  • 3,281
  • 4
  • 26
  • 28
0

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.

Shayan
  • 568
  • 1
  • 5
  • 7
0

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.

Joe.Wu
  • 23
  • 6