静态存储(主要是数据段)会导致分段错误吗?
静态
存储是在编译时决定的。然而,考虑一下我们在函数中有很多延迟初始化的场景:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
仅仅因为初始化是惰性的,分配就不是。该标准要求所有静态变量(包括局部变量)在程序启动之前进行零初始化。事实上,静态意味着(在本例中):变量的空间在程序的整个生命周期中都存在。
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.
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
不。您只能得到一个
foo()::a
。这就是重点。您似乎在询问,如果您有太多
static
对象,.data
部分是否会耗尽空间(因此进一步写入可能会导致损坏错误)。嗯,如上所述,在编译时就知道静态存储需要多少空间(也用于函数模板实例化)。每次调用该函数时不会获得更多
foo()::a
,因此没有运行时元素来确定如何需要很大的空间。No. You only get one
foo()::a
. That's kind of the whole point.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 manystatic
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.简短的回答:不,你不会出现段错误。
在下面的示例中,
abc
位于 数据段 中,并且def
和x
位于 BSS 中。此示例打印出“100 100”:函数范围静态对象的初始化仅在第一次调用期间发生。
x
的存储与def
等全局变量相同。模板基本上是相同的,除了如果
foo
由模板参数化,则每个实例化都会有一个x
。Short answer: no, you won't segfault.
In the example below,
abc
is in the Data segment anddef
andx
are in BSS.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 likedef
.Templates are basically the same, except if
foo
was parameterised by templates, there would be onex
for each instantination.