0

I am still a little fuzzy about static variables in C after reading some stuff:

according to this PDF file (bottom of 1st page) , a static variable is saved in the "initialised data" segment of my executable.

On the other hand, I quote my book* on C language:

On a system that allocates a relatively small run-time stack, one might wish to declare large arrays as static variables in function main. Then these arrays will not use up stack space.

Well from 1st link it does not look like as if a static variable (even an array) would ever use up stack space. So what is the deal?

*book is: "problem solving & program design in C", by Jeri R. Hanly & Elliot B. Koffman, 3rd edition

Theolodis
  • 4,977
  • 3
  • 34
  • 53
nass
  • 1,453
  • 1
  • 23
  • 38
  • http://stackoverflow.com/questions/93039/where-are-static-variables-stored-in-c-c – Mikołaj Mularczyk Oct 02 '14 at 08:38
  • Both links say the same thing - static variables are not allocated on the stack. The second link suggests that if you have a small stack, then you may want to declare large arrays as `static`, in order to avoid having them allocated on the stack. A more interesting question (in case you want to ask it) is - why would we ever declare arrays as non-static in that case? – barak manos Oct 02 '14 at 09:11

1 Answers1

3

Both of your sources state the same fact: static variables are not in the stack segment.

What your cited source states: static variables are not in the stack segment.

What your book says is: if you have a small stack, make variables static.

They are however still in the data segment of your program, not in the code segment. The initial data segment that your first source does cite, is, as the corresponding image shows, outside of the stack and heap segment.

Theolodis
  • 4,977
  • 3
  • 34
  • 53
  • ok and what is going to happen if I omit `static` from variables in main? Will I end up with a larger `stack` then? how would that be possible since it looks like the variable would never be stored in stack space? – nass Oct 02 '14 at 12:45
  • @nass if you omit the `static`, they will be in the stack segment. What makes you believe, they wouldn't? – Theolodis Oct 02 '14 at 12:49