6

As static variable declaration without any value assigned goes in BSS section of code. where it will be automatically initialed to zero.

Question: suppose, if declare static pointer without any assignment, then whether should it have zero value for address or value?

Manku
  • 431
  • 3
  • 9
  • 16
  • Note, dup is only tagged C. – Shafik Yaghmour Feb 23 '14 at 04:39
  • Looks like you are a bit confused about pointer variable and its value. Just remember, **pointer variable is a variable itself**, which means it has a value, but in this case you can use its value as an address. – Lee Duhem Feb 23 '14 at 04:47

2 Answers2

15

In C a static pointer will be initialized to null, the draft C99 standard section 6.7.8 Initialization paragraph 10 says:

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

and included the following bullet:

— if it has pointer type, it is initialized to a null pointer;

So no storage is allocated for it, it is a null pointer. Also note it is an implementation defined behavior where static variables are stored.

The relevant section for the C++ draft standard would be section 8.5 Initializers paragraph 13 which says (emphasis mine):

If no initializer is specified for an object, the object is default-initialized; if no initialization is performed, an object with automatic or dynamic storage duration has indeterminate value. [ Note: Objects with static or thread storage duration are zero-initialized, see 3.6.2. —end note ]

zero-initialize is covered in paragraph 6 which says:

To zero-initialize an object or reference of type T means:

and has the following bullet:

— if T is a scalar type (3.9), the object is set to the value 0 (zero), taken as an integral constant expression, converted to T;103

where footnote 103 says (emphasis mine):

As specified in 4.10, converting an integral constant expression whose value is 0 to a pointer type results in a null pointer value.

Shafik Yaghmour
  • 154,301
  • 39
  • 440
  • 740
0

The pointer will point to address zero (NULL on most systems). You will still need to point it somewhere valid (e. g. from malloc) before using it as a pointer.

DoxyLover
  • 3,366
  • 1
  • 15
  • 19
  • 3
    No, it will be initialized to the NULL pointer value, whether or not its representation is zero. For pointers, the constant 0 transforms to the NULL pointer value, _not_ the bit pattern of all zeros. – John Meacham Feb 23 '14 at 04:36