0

The static keyword, as far as I know, does two things:

  1. It allocates the variable on the heap rather than the stack. Storage
  2. It marks the lifetime of the variable as long as the lifetime of its parent process. Scope

So, it is used if:

  1. The variable is so large that it would overflow the stack. Storage use
  2. The variable needs to be available for the lifetime of the process for the function. Scope use

But, what if the variable is so large, but doesn't need to be available all the time, and keeping it all the time in the heap would be memory expensive ?

What shall I do if I face that situation ? I'm not sure I totally understand the purpose of the static keyword.

Amr Ayman
  • 1,129
  • 1
  • 8
  • 24
  • Allocate it using `malloc`/`new` and `free`/`delete` it once its use it over? – Spikatrix Apr 03 '15 at 14:29
  • #1 is wrong. It allocates the variable in the data section rather than on the stack (on most certainly not on the heap). – barak manos Apr 03 '15 at 14:31
  • @barakmanos , Or .BSS segment if it is zero initialized – Spikatrix Apr 03 '15 at 14:32
  • @CoolGuy: Yes, but I didn't want to add too many details in this case. – barak manos Apr 03 '15 at 14:33
  • Your assumptions about storage are problematic, and you're entirely missing the consideration of lifetime. – Kerrek SB Apr 03 '15 at 14:35
  • @barakmanos: I'm not an assembly programmer, so memory as I see it is either the stack or the heap. – Amr Ayman Apr 03 '15 at 14:35
  • Really? So where do you think the global variables in your program are allocated - stack or heap? (hint: neither). BTW, you don't need to be an "assembly programmer" (whatever that means) in order to know and understand all this. – barak manos Apr 03 '15 at 14:36
  • @CoolGuy: I meant I use static for **both** purposes *together*. – Amr Ayman Apr 03 '15 at 14:37
  • Note on "the variable is so large that it would overflow the stack". You can always increase the size of the stack instead of declaring the variable `static`. In any case, if you don't want to do that, and you prefer to use `static`, then you'll have to increase the size of the data section instead. In either case, you need to allocate more memory space to one of the segments in your executable. There might be considerations in favor (and against) of each case, so there is no one definitely-better choice. – barak manos Apr 03 '15 at 14:45

2 Answers2

1

The static keyword in c++ is more or less related to persistent storage as you describe it, but with several nuances according to the specific context:

--static variable in global scope.

--static variable in local function scope.

--static class member.

--static class method.

I suggest you look up at all of these cases in some tutorial.

A conceptual point that I believe you are misunderstanding is that static per se has nothing to do with size related storage efficiency. If you need to handle large data, you do this using dynamic allocation/deallocation (new/delete). In other words, this is a memory management issue, and the various techniques to deal with this have to do with constructors, destructors, smart pointers, etc...

Emerald Weapon
  • 2,392
  • 18
  • 29
0

The keyword static can probably be seen as somewhat "overloaded".

The following usage-options are all viable:

  • Static local variables
  • Static global variables
  • Static member variables
  • Static global functions
  • Static member functions

Variables:

In terms of runtime, all types of static variables are essentially the same. They all reside in the data-section of the program, and their addresses remain constant throughout the execution of the program. So the only difference between them is during compilation, in the scope of declaration:

  • Static local variable: recognized by the compiler only in the scope of the function
  • Static global variable: recognized by the compiler only in the scope of the file
  • Static member variable: recognized by the compiler only in the scope of the class

Functions:

In terms of runtime, all types of functions (static and non-static) are essentially the same. They all reside in the code-section of the program, and their addresses remain constant throughout the execution of the program. So the only difference between them is during compilation, in the scope of declaration:

  • Static global function: recognized by the compiler only in the scope of the file
  • Static member function: recognized by the compiler only in the scope of the class
barak manos
  • 29,648
  • 10
  • 62
  • 114
  • A static variable initialized to 0 does it go to .bss segment or initialized segment in the data segment? Even if we have uninitialized static variable it will go to .bss segment where it is initialized to 0 – Gopi Apr 03 '15 at 14:43
  • @Gopi: I prefer not to "overload" OP with details, which (in the context of the question at hand) might be considered "too much information". It's true that the section for global/static variables is divided into 'zero-initialized' and 'all the rest', but I felt that it would "overload" the answer beyond what seemed to be necessary in order to explain the general idea" of `static`. – barak manos Apr 03 '15 at 14:47
  • So, a static variable is always initialized to zero ? – Amr Ayman Apr 03 '15 at 14:50
  • @AmrAyman: If I'm not mistaken, the standard dictates that uninitialized static variables (local **and** global) are initialized to zero. I'm not sure about non-static global variables (you'll have to check the docs), but non-static local variables are definitely left uninitialized. – barak manos Apr 03 '15 at 14:52
  • @barakmanos I wanted to know the answer to my question :) Had a doubt where the initialized static variable will go .bss or the other part of the data segment? – Gopi Apr 03 '15 at 14:52
  • @Gopi: Oh... Sorry... It's either that, or the other way round. I usually just consider the whole section for global and static variables as `data section`, but it is true that this section is divided into two segments - one for variables initialized to non-zero values and one for all the rest (which are initialized to zero whether it is explicitly done by the programmer or not). I think the latter is called BSS. – barak manos Apr 03 '15 at 14:54