views:

11063

answers:

12

In what segment (.BSS, .DATA, other) of an executable file are static variables stored so that they don't have name collision? For example:


foo.c:                         bar.c:
static int foo = 1;            static int foo = 10;
void fooTest() {               void barTest() {
  static int bar = 2;            static int bar = 20;
  foo++;                         foo++;
  bar++;                         bar++;
  printf("%d,%d", foo, bar);     printf("%d, %d", foo, bar);
}                              }

If I compile both files and link it to a main that calls fooTest() and barTest repeatedly, the printf statements increment independently. Makes sense since the foo and bar variables are local to the translation unit.

But where is the storage allocated?

To be clear, the assumption is that you have a toolchain that would output a file in ELF format. Thus, I believe that there has to be some space reserved in the executable file for those static variables.
For discussion purposes, lets assume we use the GCC toolchain.

+5  A: 

The storage location of the data will be implementation dependent.

However, the meaning of static is "internal linkage". Thus, the symbol is internal to the compilation unit (foo.c, bar.c) and cannot be referenced outside that compilation unit. So, there can be no name collisions.

Seb Rose
no. static keyworld has overloaded meanings: in such a case static is storage modifier, not linkage modifier.
ugasoft
ugasoft: the statics outside the function are linkage modifiers, inside are storage modifiers where there can be no collision to start with.
wnoise
+2  A: 

It depends on the platform and compiler that you're using. Some compilers store directly in the code segment. Static variables are always only accessible to the current translation unit and the names are not exported thus the reason name collisions never occur.

trotterdylan
A: 

The answer might very well depend on the compiler, so you probably want to edit your question (I mean, even the notion of segments is not mandated by ISO C nor ISO C++). For instance, on Windows an executable doesn't carry symbol names. One 'foo' would be offset 0x100, the other perhaps 0x2B0, and code from both translation units is compiled knowing the offsets for "their" foo.

MSalters
+2  A: 

I don't believe there will be a collision. Using static at the file level (outside functions) marks the variable as local to the current compilation unit (file). It's never visible outside the current file so never has to have a name.

Using static inside a function is different - the variable is only visible to the function, it's just its value is preserved across calls to that function.

In effect, static does two different things depending on where it is. In oth cases however, it limits the visibility of the variable to prevent namespace clashes,

Having said that, I believe it would be stored in DATA which tends to have initialized variable. The BSS originally stood for byte-set-<something> which held variables which weren't initialized.

paxdiablo
@paxdiablo: you have mentioned two types of static variables. Which one of them does this article (http://en.wikipedia.org/wiki/Data_segment ) refer to?Data segment also holds the global variables (which are exactly opposite in nature to static ones). `So, how does a segment of memory (Data Segment) store variables that can be accessed from everywhere (global variables) and also those which have limited scope (file scope or function scope in case of static variables)?`
Lazer
@eSKay, it haas to do with visibility. There can be things stored in a segment which are local to a compilation unit, others which are fully accessible. One example: think of each comp-unit contributing a block to the DATA segment. It knows where _everything_ is in that block. It also publishes the addresses of those things in the block that it wishes other comp-units to have access to. The linker can resolve those addresses at link time.
paxdiablo
A: 

they're both going to be stored independently, however if you want to make it clear to other developers you might want to wrap them up in namespaces.

Robert Gould
+4  A: 

in the "global and static" area :)

there are several memory area in C++

  • heap
  • free store
  • stack
  • global & static
  • const

see here for detailed answer to your question

ugasoft
A: 

static variable stored in data segment or code segment as mentioned before.
You can be sure that it will not be allocated on stack or heap.
There is no risk for collision since static keyword define the scope of the variable to be a file or function, in case of collision there is a compiler/linker to warn you about.
A nice example

Ilya
+17  A: 

Where your statics go depends on if they are 0 initialized or not. 0 initialized static data goes in .BSS (Block Started by Symbol), non 0 initialized data goes in .DATA

Don Neufeld
By "non-0 initialized" you probably mean "initialized, but with something other than 0". Because there's no such thing as "non initialized" static data in C/C++. Everything static is zero-initialized by default.
AndreyT
@Andrey: That's how I would read that sentence anyway.
Donal Fellows
A: 

Check this out. For more details about gcc check this out.

Iulian Şerbănoiu
+4  A: 

Data declared in a compilation unit will go into the .BSS or the .Data of that files output. Initialised data in BSS, uninitalised in DATA.

The difference between static and global data comes in the inclusion of symbol information in the file. Compilers tend to include the symbol information but only mark the global information as such.

The linker respects this information. The symbol information for the static variables is either discarded or mangled so that static variables can still be referenced in some way (with debug or symbol options). In neither case can the compilation units gets affected as the linker resolves local references first.

itj
+4  A: 

In fact, a variable is tuple (storage, scope, type, address, value):

storage     :   where is it stored, for example data, stack, heap...
scope       :   who can see us, for example global, local...
type        :   what is our type, for example int, int*...
address     :   where are we located
value       :   what is our value

Local scope could mean local to either the translational unit (source file), the function or the block depending on where its defined. To make variable visible to more than one function, it definitely has to be in either DATA or the BSS area (depending on whether its initialized explicitly or not, respectively). Its then scoped accordingly to either all function(s) or function(s) within source file.

yogeesh
+1  A: 

Please go to the link go get more details. http://cmemorymap.blogspot.com/2007/12/data-segment-stackdataheapcode-area.html

kalpesh
I'm sorry, but I find that explanation to be inexact at best. Globals and translation unit scoped variables will be stored in the "executables" data/bss segment. This is totally separate and has actually nothing to do with either the stack of the heap.
Benoit