Suppose I created three or four objects for studying a class, then will the memory be created separately or not?

Here

class study
{  
   some data member;  
   some member function;
};

main()
{
    study s1, s2, s3;
}

He will create three objects for studying the class, then he will create memory separately for a separate object or not in C ++

+3
source share
3 answers

Not quite sure what you are asking here, but ...

Obviously, s1, s2 and s3 will occupy different memory areas.

However, the allocated memory for them is on the stack, so there will be no calls malloc()/new()to actually “allocate” the memory.

Allocating memory from the stack is fast (just subtracting it from the stack pointer), so for allocating 3 'studys' there is usually only one assembler instruction to do something like SUB 3*sizeof(study)SP.

+2

- . , . , .

0

I need more clarity on your subject. You ask if they will share the same memory (which is not possible) since they are declared on the same line? Or do you ask if memory has been allocated yet since they are not yet defined?

0
source

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


All Articles