3

My csapp book says that if global and static variables are initialized, than they are contained in .data section in ELF relocatable object file.

So my question is that if some foo.c code contains

int a;
int main()
{
    a = 3;
}`

and example.c contains,

int b = 3;
int main()
{
...
}

is it only b that considered to be initialized? In other words, does initialization mean declaration and definition in same line?

Jinwoo Park
  • 99
  • 1
  • 10
  • Global "uninitialized" variables typically end up in a "bss" segment, which will be initialized to zero. – Some programmer dude Nov 11 '19 at 09:21
  • https://stackoverflow.com/questions/1169858/global-memory-management-in-c-in-stack-or-heap This might help. – Amal John Nov 11 '19 at 09:22
  • All definitions are declarations in C and C++, and a definition of a variable may optionally include an initialiser. If no initialiser is provided for a global/static, then the default is zero-initialisation (or, in C++, for a struct/class type, calling an appropriate default constructor if one exists). What that translates to in terms of where variables are located in memory, or represented in a segment of an object or executable file, depends on the implementation. – Peter Nov 11 '19 at 09:33
  • Related, if not a dup: https://stackoverflow.com/q/58791133/6699433 – klutt Nov 11 '19 at 10:10
  • Here's a summary with examples: https://electronics.stackexchange.com/a/237759/6102 – Lundin Nov 11 '19 at 10:11
  • @Someprogrammerdude: Can you provide a source for the statement that “And technically, explicit initialization only happens on definition of variables”? The C standard does not appear to define “initialization” thusly, and it uses “uninitialized” in a different way, as in C 2018 6.3.2.1 2: “… that object is uninitialized (not declared with an initializer and no assignment to it has been performed prior to use)…” Certainly initializers provide an initial value, but, as the quoted passage shows, an initial value can be provided in an assignment statement. – Eric Postpischil Nov 12 '19 at 11:57

5 Answers5

3

It means exactly what it says. Initialized static storage duration objects will have their init values set before the main function is called. Not initialized will be zeroed. The second part of the statement is actually implementation dependant, and implementation has the full freedom of the way it will be archived.

When you declare the variable without the keyword extern you always define it as well

0___________
  • 60,014
  • 4
  • 34
  • 74
3

Both are considered initialized

They get zero initialized or constant initalized (in short: if the right hand side is a compile time constant expression).

If permitted, Constant initialization takes place first (see Constant initialization for the list of those situations). In practice, constant initialization is usually performed at compile time, and pre-calculated object representations are stored as part of the program image. If the compiler doesn't do that, it still has to guarantee that this initialization happens before any dynamic initialization.

For all other non-local static and thread-local variables, Zero initialization takes place. In practice, variables that are going to be zero-initialized are placed in the .bss segment of the program image, which occupies no space on disk, and is zeroed out by the OS when loading the program.

To sum up, if the implementation cannot constant initialize it, then it must first zero initialize and then initialize it before any dynamic initialization happends.

darune
  • 10,480
  • 2
  • 24
  • 62
1

I will answer this question in general and complete way and not with respect to any programming language

There is a hell lot of confusion between declaration, definition, and initialization. Sometimes they all look similar and sometimes completely different.

Before understanding the differences, It is very important to be aware of two things:

The difference between declaration, definition, and initialization varies from one programming language to other. Each programming has its own way of doing these three things.The “thing” which you are defining, declaring or initializing also affects the difference between the three of them. That “thing” can be a variable, a class or a function. All of them have different meanings of definitions, declaration, and initialization. Once we are aware of the above two things, most of the doubts get cleared and we stop seeking exact differences because it’s not there.

In general terms ( irrespective of any language or “thing”)

The declaration means we are saying to a computer that this “thing” (it can be a variable, a function or a class) exists but we don’t know where. In the future, we may tell but right now it just exists somewhere. In simple words, we don’t allocate memory while declaring. We can declare that “thing” many times. The definition means we are saying to the computer that this “thing” needs memory and it needs to be located somewhere. In simple words, defining means we have allocated memory for it. We can define something only once The initialization means whatever our “thing “ is, we are giving it an initial value. That “thing” must be in some memory location and if we keep that location empty, it may be a house for bugs and errors. Initialization is not always necessary but it’s important.

Many people assume that declaration + definition = Initialization .

It's not wrong, but it’s not correct in all places. Its correct only for variables that too in a language like C ++ or maybe C.

In python, there is no concept of the declaration . We don’t need to declare anything in it.

The general meaning of the three is valid everywhere but the way that is performed varies from language to language and the “thing”.

Hope it helps :)

VJAYSLN
  • 473
  • 4
  • 12
1

In the snippet:

int a;
int main()
{
    a = 3;
}

a is not initialized; it is assigned. Assignment is a run-time execution of code. For example, should main be called multiple times (which is not, but any user function could), then a is set to 3 each time the function is called.

You second snippet is initializaion of the globalvariable b and it will be placed in the .data segment.

Paul Ogilvie
  • 25,048
  • 4
  • 23
  • 41
  • 1
    “Assignment is a run-time execution of code” is not a good description. In `void foo(int a); { int x = 2*a; … }`, `x` is given the value `2*a` at run-time, perhaps multiple times, when `foo` is executed, but this is initialization, not assignment. – Eric Postpischil Nov 11 '19 at 12:57
  • @EricPostpischil, `int x = 2*a;` is initialization. `x = 2*a;` is assignment. But I get your suggestion. – Paul Ogilvie Nov 11 '19 at 13:37
0
  • Variables with static storage duration that are initialized to zero end up in .bss.
  • Variables with static storage duration that are initialized with a non-zero value end up in .data.

NOTE: the C standard guarantees that if the programmer doesn't explicitly initialize a variable with static storage duration, such as static int a;, it is then initialized to zero implicitly1). Therefore a ends up in .bss.

Examples here.


1) C11 6.7.9

If an object that has static or thread storage duration is not initialized explicitly, then:

  • if it has arithmetic type, it is initialized to (positive or unsigned) zero;
Lundin
  • 195,001
  • 40
  • 254
  • 396