开发者

Suppose i created three or four object of a class study then memory will be created seperatly or not?

开发者 https://www.devze.com 2023-02-06 20:21 出处:网络
开发者_开发问答Here class study { some data member; some member function; }; main() { study s1, s2, s3;
开发者_开发问答

Here

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

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

It will create three object of class study then memory created seperatly for seperate object or not in c++


Not quite sure what you're asking here but...

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

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

Allocating memory off the stack is fast (just a subtraction from the stack pointer) so to allocate 3 'studys' there is typically just a single assembler instruction to do something like SUB 3*sizeof(study) from SP.


A class is just a user defined datatype. So just like how for a datatype memory is different for different variables similarly for classes and objects also. Of course separate for different object.


need more clarity on your question. Are you asking if they would share a single memory (which is not possible), since they are declared in a single line? Or are you asking whether the memory is allocated yet, since they are not defined yet?

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号