用 C++ 编写的存储结构和类在哪里?

发布于 2024-12-01 18:46:42 字数 364 浏览 0 评论 0原文

在C#中,类存储在堆中,结构存储在堆栈中。

在 C++ 中,类和结构是否以相同的方式存储(假设我静态创建类和结构,并且类或结构的每个成员都不是由 new 分配的)?

请使用下面的代码片段解释这一点:

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;
}

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 every 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;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

浮生面具三千个 2024-12-08 18:46:42

结构的存储方式与类的存储方式没有(必要的)差异。事实上,C++ 中结构和类之间的唯一区别是结构成员默认是公共的,而类成员默认是私有的。

与任何其他类型的对象一样,类或结构类型的对象具有存储持续时间,该存储持续时间由其创建方式决定。

在函数内部声明的对象的生命周期仅限于封闭块;这通常是通过将其存储在堆栈上来实现的。

在函数外部或使用 static 关键字声明的对象的生命周期可以延伸到程序的整个执行过程中;这可以通过将其存储在数据段中来实现。

new 运算符(或 malloc() 调用)分配的对象将一直存在,直到被删除(或 free())为止。代码>ed);这些对象被分配在“自由存储”中,有时非正式地称为“堆”。

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".

梦与时光遇 2024-12-08 18:46:42

实现类/结构的代码(即实现类型的代码)存储在代码段中的某处。

编辑:
正如OP在评论中澄清的那样,他的问题的答案从下面开始。 Q 中存在歧义,导致了这个答案的开头陈述。

根据您创建对象的方式,如果使用 mallocnew 创建,则它们会在动态存储(freestore)上创建,或者在本地堆栈存储上创建。

此外,这还取决于创建对象的位置。
全局范围的对象和静态对象创建在Data段或BSS段上。

AFAIK C++ 标准没有提到内存段(除了 freestore(又名堆)和本地存储(又名堆栈))。所以实际上休息是一个实现细节。

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.

绳情 2024-12-08 18:46:42

据我所知,不使用“new”运算符初始化的类/结构存储在堆栈上,而使用“new”创建的类/结构存储在堆上。

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

冷月断魂刀 2024-12-08 18:46:42

C++ 具有三种分配类:自动、动态和静态,与对象生命周期相关(分别是作用域、手动和永久)。通常,自动分配的对象被实现为放置在堆栈上,而动态分配的对象则放置在堆上;堆栈和堆一起构成了程序的“堆栈段”。该程序(通常)还有一个“数据段”,这是静态分配的对象所在的位置 - 将静态分配视为在加载时发生,以便在程序开始之前就预留内存。数据段可能包含只读部分,您可能会在其中找到一些字符串常量(因此不要写入它们)。

当然,所有这些都是实现细节,并且这些都不是语言强制要求的。它只是桌面平台上的一种流行实现。

因此,无论某个东西是类、结构还是 int,这都无关紧要,重要的是你如何分配它。

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.

谁对谁错谁最难过 2024-12-08 18:46:42

当类或结构自动创建时,它们存储在堆栈中。当它们动态创建时,它们存储在堆中。

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文