views:

245

answers:

6

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

+4  A: 

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
a C book, it said that declaration tells the type of data and definition assigns memory to it
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
@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
@user265260 What "C book" would that be?
anon
"Let us C" by yashwanth kanetkar
@user265260: Please update your question with these additional facts. DOn't add comments to an answer: update the question.
S.Lott
+2  A: 

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.

Brian R. Bondy
There are more places than those 2. [GotW #9](http://www.gotw.ca/gotw/009.htm) lists all 5.
MSalters
@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
A: 

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
+1  A: 

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 N Fetissov
I think you are the only one who groked what the OP has misunderstood!
Matthieu M.
A: 

A class definition has nothing to do with memory assignment. It is kind of a blueprint for the construction process where an instance of the specified type is created.

class A
{
  unsigned char number;
};

class A contains a member with the size of 1 Byte. If you create an instance of that class 1 Byte of memory will be needed.

Holger Kretzschmar
A: 

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