静态存储(主要是数据段)会导致分段错误吗?

发布于 2024-11-19 17:46:29 字数 357 浏览 7 评论 0原文

静态存储是在编译时决定的。然而,考虑一下我们在函数中有很多延迟初始化的场景:

void foo ()
{
  static int a[1000];
}

我在这里不是讨论编码实践,而是技术方面。随着许多诸如foo()之类的其他函数被执行,这些许多静态变量将被引入数据段。

编译器在为数据段分配空间时是否也会在帐户中进行延迟初始化。如果“否”,那么在代码执行时会导致运行时分段错误吗? (当 template 方法中有大量static 数据时更有可能发生)。

static storage is decided at compilation time. However, consider the scenario where we have lot of lazy initialization in functions:

void foo ()
{
  static int a[1000];
}

I am not discussing the coding practice here, but the technical aspect. As many such other functions like foo() are executed, those many static variables will be introduced on data segment.

Will compiler take the lazy initialization also in the account while allocating space for data segment. If 'No' then, will it cause segmentation fault at runtime while the code is executing ? (more likely to happen when lot of static data inside template methods).

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

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

发布评论

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

评论(4

谷夏 2024-11-26 17:46:29

仅仅因为初始化是惰性的,分配就不是。该标准要求所有静态变量(包括局部变量)在程序启动之前进行零初始化。事实上,静态意味着(在本例中):变量的空间在程序的整个生命周期中都存在。

Just because the initialization is lazy, the allocation isn't. The standard requires all static variables (including local variables) to be zero initialized before the start of program. And in fact, static means just that (in this case): the space for the variable is present for the entire lifetime of the program.

梦初启 2024-11-26 17:46:29

1)一件事不会有“很多”变量。函数/方法范围内的静态变量非常类似于全局变量。

2)不存在惰性初始化,因为它很可能在应用程序启动期间与所有其他全局变量一起初始化。

3) 我认为没有任何原因

了解有关 Static(C++) 的更多信息

编辑:删除了关于归零的声明

1) there wont be "many" variables for one thing. a static variable in function/method scope is very much like a global variable.

2) there is no lazy init as is most likely initialzed during app start-up, along with all other global variables.

3) i see no reason for a fault

Read more about Static(C++)

EDIT: removed statement about zero'ing out

滿滿的愛 2024-11-26 17:46:29

随着许多这样的 foo() 被执行,这些许多静态变量将被引入数据段。

不。您只能得到一个 foo()::a。这就是重点。

编译器是否也会在帐户中进行延迟初始化
为数据段分配空间。如果“否”,那么会导致
代码执行时运行时出现分段错误? (更多的
当模板方法中有大量静态数据时可能会发生)。

您似乎在询问,如果您有太多 static 对象,.data 部分是否会耗尽空间(因此进一步写入可能会导致损坏错误)。

嗯,如上所述,在编译时就知道静态存储需要多少空间(也用于函数模板实例化)。每次调用该函数时不会获得更多foo()::a,因此没有运行时元素来确定如何需要很大的空间。

As many such foo()s are executed, those many static variables will be introduced on data segment.

No. You only get one foo()::a. That's kind of the whole point.

Will compiler take the lazy initialization also in the account while
allocating space for data segment. If 'No' then, will it cause
segmentation fault at runtime while the code is executing ? (more
likely to happen when lot of static data inside template methods).

You appear to be asking whether the .data section will run out of space (and thus further writes to it may cause corruption errors) if you have too many static objects.

Well, as noted above, it's known at compile-time how much space you'll need for static storage (for function template instantiations too). You do not get more foo()::a every time you call the function, so there is no run-time element to determining how much space will be required.

空城仅有旧梦在 2024-11-26 17:46:29

简短的回答:不,你不会出现段错误。

在下面的示例中,abc 位于 数据段 中,并且 defx 位于 BSS 中。

#include <iostream>

int abc = 123;
int def;

int foo (int i) {
    static int x (i);
    return x;
}

int main () {
    std :: cout << foo (100) << " " << foo (200);
}

此示例打印出“100 100”:函数范围静态对象的初始化仅在第一次调用期间发生。

x 的存储与 def 等全局变量相同。

模板基本上是相同的,除了如果 foo 由模板参数化,则每个实例化都会有一个 x

Short answer: no, you won't segfault.

In the example below, abc is in the Data segment and def and x are in BSS.

#include <iostream>

int abc = 123;
int def;

int foo (int i) {
    static int x (i);
    return x;
}

int main () {
    std :: cout << foo (100) << " " << foo (200);
}

This example prints out "100 100": initialisastion of function-scope static objects happens during the first invocation only.

Storage for x is the same as if it was a global variable like def.

Templates are basically the same, except if foo was parameterised by templates, there would be one x for each instantination.

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