开发者

Can static storage (mostly data segment) cause segmentation fault?

开发者 https://www.devze.com 2023-03-19 09:34 出处:网络
static storage is decided at compilation time. However, consider the scenario where we have lot of lazy initialization in functions:

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 segmentati开发者_开发知识库on fault at runtime while the code is executing ? (more likely to happen when lot of static data inside template methods).


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) 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


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.


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.

0

精彩评论

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

关注公众号