2

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

Puppy
  • 144,682
  • 38
  • 256
  • 465
iammilind
  • 68,093
  • 33
  • 169
  • 336
  • Pick a language. C and C++ are distinct. – Lightness Races in Orbit Jul 12 '11 at 09:38
  • @Tomalak, This is applicable to both of them. – iammilind Jul 12 '11 at 09:38
  • You're asking for specific semantics of a language. You have assumed that the answer is the same for both, but meta-data about answers have no place in questions. – Lightness Races in Orbit Jul 12 '11 at 09:39
  • 2
    @Tomalak His example is clearly well within the common subset of C and C++. As part of the common subset, one would hope that the answer would be the same in both cases. – James Kanze Jul 12 '11 at 09:43
  • @James: Perhaps I'm reacting too strongly but I find that almost every time a question is tagged both `c` and `c++` the OP either has a fundamental misunderstanding of what they _are_, or has made an assumption about what the correct answer will relate to. _We_ know that the answer lies within the common subset, but the _question_ doesn't know that. – Lightness Races in Orbit Jul 12 '11 at 09:48
  • It's applicable to any language that has static data. – Puppy Jul 12 '11 at 10:15
  • @DeadMG, removing C/C++ will not help to get larger audience to this question. If you wish you can add `programming-language` tab. – iammilind Jul 12 '11 at 10:18
  • 1
    @iammilind: Larger audience is irrelevant. What matters is that it is tagged correctly, and neither C or C++ have any notion of a data segment, if such a thing even exists anymore, it is a platform specific thing and all compilers for all languages will need it. – Puppy Jul 12 '11 at 10:46
  • @Tomalak In general, you're right, but the nature of the question, and the way it was posed, makes me think that this might be the exception. (Or he wants an answer for both languages.) – James Kanze Jul 12 '11 at 10:49
  • 1
    @DeadMG: But they _do_ define the semantics of a static-duration object. – Lightness Races in Orbit Jul 12 '11 at 10:55

4 Answers4

5

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.

James Kanze
  • 150,581
  • 18
  • 184
  • 329
  • +1, but if the space is allocated for all the `static` variable then how about `template`s. How they will be allocated the space. – iammilind Jul 12 '11 at 09:49
  • @iammilind - No difference. The compiler knows how many templates are instantiated and can add up the space needed. It is all determined at compile time. – Bo Persson Jul 12 '11 at 09:53
  • @iammilind: Templates don't change anything. Templates are instantiated at build-time, and each time it happens you get a function out of it. Just like your `foo()`. – Lightness Races in Orbit Jul 12 '11 at 09:58
  • @iammilind There are no templates in an executable. When a template is instantiated, it becomes a function or a class, just like any other function or class (almost). – James Kanze Jul 12 '11 at 10:51
3

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

2

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.

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
0

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.

spraff
  • 32,570
  • 22
  • 121
  • 229