当在函数内部静态构造对象时,它会在堆上还是在堆栈上分配?

发布于 2024-08-22 08:26:18 字数 70 浏览 5 评论 0原文

如果我有以下代码:

for (...) { 一个一个; a

会分配在堆上还是栈上?

if i have the following code:

for (...)
{
A a;
}

would a be allocated on the heap or on the stack?

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

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

发布评论

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

评论(7

二手情话 2024-08-29 08:26:18

当你说:

for (...) { A a; }

变量 a 不是静态构造的。那将是:

for (...) { static A a; }

事实上,在您的代码中, a 是一个自动对象,在堆栈​​上创建。然而,这并不意味着没有发生动态分配。如果 A 看起来像这样:

struct A {
   A() { p = new char[100]; }
   char *p;
};

那么当你说:

for (...) { A a; }

ap 的存储是在堆栈上创建的,但 ap 指向的存储是动态创建的。

When you say:

for (...) { A a; }

the variable a is NOT being constructed statically. That would be:

for (...) { static A a; }

In fact in your code, a is an automatic object, created on the stack. However, that doesn't mean that no dynamic allocation is taking place. If A looked like this:

struct A {
   A() { p = new char[100]; }
   char *p;
};

then when you say :

for (...) { A a; }

the storage for a.p is created on the stack, but the storage that a.p points at is created dynamically.

年少掌心 2024-08-29 08:26:18

在堆栈上。

仅在执行 new 时才在堆上分配内存(如果您正在执行 C 风格的操作,则在执行 malloc 及其朋友时分配内存,但在 C++ 中不应这样做)。

On the stack.

Memory is only allocated on the heap when doing new (or malloc and its friends if you are doing things C-style, which you shouldn't in C++).

溺孤伤于心 2024-08-29 08:26:18

您没有在代码中声明静态变量 - 它是本地范围的变量,因此它将最终出现在堆栈上。

You are not declaring a static variable in your code - it is a locally scoped variable and thus it will end up on the stack.

°如果伤别离去 2024-08-29 08:26:18

那不是静态变量。 A 也在堆栈上分配。
仅当显式newed 和deleteed 时,才会在堆上分配变量。

That's not a static variable. Also A is allocated on the stack.
Variables are allocated on the heap only when explicitly newed and deleteed.

友谊不毕业 2024-08-29 08:26:18

A 将完全在堆栈上分配。当然,A 可以在其构造期间从堆中分配内存。

A will be allocated entirely on the stack. A may, of course, allocate memory from the heap during its construction.

双手揣兜 2024-08-29 08:26:18

静态变量和常量变量放置在堆栈的特殊区域中。

Static and const variables are placed on stack in special area.

撧情箌佬 2024-08-29 08:26:18

该变量是在堆栈上创建的。在你的代码中:

for (...) { A a; }

......然后在任何“for”循环结束时,变量会自动销毁(因为它超出了范围),正如尼尔所说:

Neil Butterworth:事实上,在您的代码中,a 是一个自动对象...

但是,如果 a 对象在其自己的生命周期中进行了一些动态分配(IOW,在堆上),那么请注意自己释放内存,或者在 A 的析构函数中,或者在外部。 C 示例:

struct A {
  A(char *ptr);
  ~A();

private:
  char *p;
  int len;
};

A::A(char *ptr)
{
  len = strlen(ptr);
  p = (char *) malloc(len+1);
  if(!p) {
    exit(1);
  }
  strcpy(p, ptr);
}

A::~A()
{
  free(p);
}

如果不调用 free 过程,p 变量不会自动释放。

再见(对我的英语感到抱歉)

PS:我想说“静态”这个词在这种情况下受到如此严厉的批评,它并不像他们 jkp 和 jldupont 那么糟糕......

jkp :““静态”这个词有点误导,它意味着使用 static 关键字......

jldupont :“您没有声明静态变量......

等等...

jkp 和 jldupont 100% 正确,但在意大利语技术语言中,一些 C++ 程序员使用“静态创建”和“< em>静态构建”来标识将在堆栈上创建的变量

当您定义静态变量时,换句话说,

static A a;

相同的程序员用来调用“静态变量”和“ 变量声明为静态”。

the variable is created on the stack. In your code :

for (...) { A a; }

...And then at the end of any "for" cycle, the variable is destroyed automatically (because it goes out of scope), as Neil said:

Neil Butterworth : In fact in your code, a is an automatic object...

But if the a object makes some dynamic allocations (IOW, on the heap) during his own life cycle, then be aware to free the memory by yourself, or in the destructor of A, or outside. C example :

struct A {
  A(char *ptr);
  ~A();

private:
  char *p;
  int len;
};

A::A(char *ptr)
{
  len = strlen(ptr);
  p = (char *) malloc(len+1);
  if(!p) {
    exit(1);
  }
  strcpy(p, ptr);
}

A::~A()
{
  free(p);
}

The p variable is not freed automatically if you don't call the free procedure.

Bye (and sorry for my english)

PS : i would like to say that the word "statically" so severly criticized in this context, it is not so bad as they jkp and jldupont ...

jkp : "The word "statically" is a bit misleading there, it implies usage of the static keyword...."

jldupont : "You are not declaring a static variable...."

and so on...

jkp and jldupont are 100% right but in italian technical language some C++ programmers use the word "created statically" and "built statically" to identify a variable will be created on the stack

When you define a static variable instead, in other words

static A a;

The same programmers use to call that "static variable" and "variable declared as static".

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