Home > Blockchain >  No memory is allocated while creating the class, then where variables in the class saved?
No memory is allocated while creating the class, then where variables in the class saved?

Time:07-05

In object oriented programming, there is concept of class and objects. We define a class and then create its instance (object). Consider the below C example:

class Car{
  public:
   string model;
   bool petrol;
   int wheels = 4;
}

int main(){
   Car A;
   cout << A.wheels;

   return 0;
}

Now I know no memory was allocated to the class Car until the object A was created. Now I am swamped in the confusion that if no memory was allocated to the Car, then at the time of creating object A, how object A will know that wheels is equal to 4 ? I mean it should be saved somewhere in the memory.

Please ignore mistakes as it is a question from beginner's side :)

CodePudding user response:

Your C compiler generates executable code to default-initialize the class member to 4. The "saved somewhere in memory" you're referring to is compiler-generated executable code. Actual, executable code that constructs the object, and sets its class member to 4.

CodePudding user response:

There are 2 types of storage at work here.

The information about Car is stored in memory. That is the code in its methods, its layout, including the literal value 4 which initializes wheels. This exists in the binary executable file, and exists in memory at all times your application is running.

But when you say "no memory allocated.." you're thinking about memory of an instance of Car, where memory is allocated each time you create a new instance.

CodePudding user response:

Local variables in functions have automatic storage duration, which usually means they are allocated in the stack frame of the function.

If you compile your code and look at the generated assembly you will see the SP being adjusted to make space for the Car and model and wheels getting initialized. You will also see std::string::~string() being called at the end of main as part of what is left of the default destructor of Car.

I'm actually disappointed none of the compilers (gcc, clang, icc) figure out that you only access A.wheels and that A.wheels is 4 and that the std::string destructor has no side effect since they do figure out the constructor is a NOP and allocated nothing for an empty string.

I would hope the optimizer to optimize away the Car completely and only leave std::cout << 4;.

CodePudding user response:

Some programming languages have the notion of a "class object". That's an object that holds data about what's in a class: its members, its member functions, and its bases. C doesn't do that. The information about what's in a class is in the executable code that the compiler generates.

In the code in the question, when an object of type Car is created, the code that the compiler generates allocates a block of memory large enough for a Car object. The code "knows" where in that block the storage for model is, where the storage for petrol is, and where the storage for wheels is. Since the wheels member has an initializer, the code that the compiler generates also sets the value in the storage for wheels to 4.

  • Related