Modeling model instances of model and class

Suppose we create an instance of a class on the stack. I understand that the compiler gives it a certain amount of memory depending on the types and number of fields in this instance. But I am confused about instance methods. I guess they have their own stack frame.

What? I do not understand:

  • Where is the stack stack of the instance method? Are they located inside the instance stack frame or are they stored elsewhere?
  • Is there only one stack of the instance method stack created for many instances of the class
  • if so, then what if two objects of the same class simultaneously call the same function from different threads?
+6
source share
4 answers

Like regular functions, C ++ has several pieces of memory associated with member functions. First, there are actual assembly instructions that make up a member function that usually fits in a code segment and should not be a concern. Secondly, every time a member function is called, additional stack space is reserved for all local variables ("automatic objects") inside this call, which is cleared when the call returns. I must specifically indicate that functions do not have fixed pre-allocated memory for their stack space - if a function is recursive, for example, you may have several stack frames active for this function at the same time. Most likely, as many stack frames as necessary.

When you declare a local class type variable in C ++, you only get memory for the object itself. No additional memory is allocated for storing member functions of this object - as mentioned above, the memory of member functions is either placed in a data segment or allocated as necessary when you call member functions. In particular, if you call a member function on the object you declared, then the program will allocate a new stack stack for this member function, call the function, and clear memory when the function returns. There is no additional “premium” paid for member functions; they actually do not affect the size of the object (although having one or more virtual functions in your class can add a one-time cost to the size of your object).

Of course, it all depends on the implementation; an implementation, in principle, could allocate additional space for storing member functions inside an object, but as far as I know, no standard C ++ implementations will do this. (If you know what a vtable is, an object may have a vtable pointer, but not all vtable entries).

Hope this helps!

+9
source

1. Where is the stack stack of the instance method? Are they located inside the instance stack frame or are they stored elsewhere?

There is no such thing as an instance stack frame. Stack frames are created in the call stack of the actual thread execution.

2. Is there only one method stack stack created for many instances of the class

See my answer for 1. .. Yes, there are different call stacks per thread.

3. , if yes, then what if two objects of the same class simultaneously call the same function from different threads?

As already mentioned, different stack frames are created for each thread. There are no call stack packets per instance. These are just different implicitly passed this pointers that distinguish between available instances.

+5
source

The method is essentially a “piece” of codes of operations (machine operations) located in the code section (read-only) of the executable image.

Thus, methods have nothing to do with the stack (in terms of the memory in which they reside).

However, they access the stack when they perform operations on local variables.

A class instance does not "contain" class methods, but only class attributes (variables that you define in the class), and possibly a pointer to the class V-table (if you define one or more virtual functions in the class or in one of its base classes).


While the method works with non-statistical local variables or non-stationary member variables, it is thread safe, since these variables are allocated on the stack each time the method is called, and each thread has its own stack (its separate area inside the entire stack, to be more precise).

Once a method works with variables that are not allocated on the stack (static local variables, static member variables, static global variables, or non-static global variables), it becomes unsafe and should be treated as such (usually with the appropriate OS resources, such as semaphores, mutexes, etc.).

+4
source

Also, keep in mind that a built-in function / method call may not create a stack frame at all.

And the che compiler can embed a funcition call for optimization.

0
source

Source: https://habr.com/ru/post/972617/


All Articles