开发者

Where are stored structs and classes written in C++?

开发者 https://www.devze.com 2023-03-30 21:36 出处:网络
In C# classes are stored in heap, and structs are stored in stack. Does in C++ classes and strucs are stored in the same way (assuming I create my classes and structs statically, and ev开发者_StackO

In C# classes are stored in heap, and structs are stored in stack.

Does in C++ classes and strucs are stored in the same way (assuming I create my classes and structs statically, and ev开发者_StackOverflow中文版ery member of class or struct is not allocated by new) ?

Please explain this using snippet of code below:

class B
{
int b;
}

class C
{
int c;
}

class A
{
B b;
C c;
int x;
}

struct SB
{
int sb;
}

struct SC
{
int sc;
}

struct SA
{
SB sb;
SC sc;
int x;
}

void main()
{
A a1;
A *a2 = new A;

SA sa1;
SA *sa2 = new SA;
}


There is no (necessary) difference in how structs are stored vs. how classes are stored. In fact, the only difference between structs and classes in C++ is that struct members are public by default, and class members are private by default.

Like any other kind of object, an object of class or struct type has a storage duration which is determined by how it's created.

An object declared inside a function has its lifetime limited to the enclosing block; this is typically implemented by storing it on the stack.

An object declared outside a function, or with the static keyword, has a lifetime that extends over the entire execution of the program; this might be implemented by storing it in the data segment.

An object allocated by a new operator (or malloc() call) exists until it's deleted (or free()ed); such objects are allocated in the "free store", sometimes informally referred to as "the heap".


The code that implements Classes/Structures (i.e., the code that implements the types) are stored somewhere in the code segment.

EDIT:
As OP clarify's in the comments, The answer to his Question, starts from below here. There was an ambiguity in the Q, which lead to the opening statement of this answer.

Depending on how you create your objects, they are created on dynamic storage(freestore) if created with malloc,new or are created on local stack storage.

Also, it depends on where objects are created.
Globally scoped objects & static objects are created on Data segment or BSS segment.

AFAIK the C++ standard does not mention of the memory segments(except freestore(aka heap) and local storage(aka stack)). So actually rest is an implementation detail.


afaik, classes / structs initialized without using the "new" operator are stored on the stack, and those created using "new" are on the heap.


C++ has three allocation classes: automatic, dynamic, and static, connected to object lifetime (respectively scoped, manual and permanent). Typically, automatically allocated objects are implemented as being placed on the stack, while dynamically allocated objects go onto the heap; together, stack and heap comprise the "stack segment" of the program. The program also (typically) has a "data segment", which is where statically allocated objects go -- think of static allocation as happening at load time, so that memory is set aside before the program proper even begins. The data segment may contain read-only parts, which is where you might find some of your string constants (so don't write to them).

All these are implementation details, of course, and none of this is mandated by the language. It's just a popular implementation on desktop platforms.

So whether something is a class or a struct or an int, that's irrelevant, what matters is how you allocate it.


When classes or structs are created automatically, they are stored on the stack. When they are created dynamically, they are stored in the heap.

0

精彩评论

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

关注公众号