6

I was wondering what the cpp standard says about global initialization. I have found this answer helpful, but there was no mention of a pointer type.

Is there a guarantee that this will work?

char* myptr
int main()
{
    if (myptr == NULL)
    {
        std::cout << "All good!" << std::endl;
    }
}
Community
  • 1
  • 1
Bartlomiej Lewandowski
  • 10,771
  • 14
  • 44
  • 75

2 Answers2

10

Yes, a pointer defined at namespace scope (global namespace in your case) is guaranteed to be initialized to the correct null pointer value of the type.

For the standard references,

3.6.2[basic.start.init]/2 "Variables with static storage duration ... shall be zero-initialized (8.5)"

8.5[dcl.init]/6 "To zero-initialize ... means: if T is a scalar type (3.9), the object is initialized to the value obtained by converting the integer literal 0 (zero) to T;[106]"

106) As specified in 4.10, converting an integer literal whose value is 0 to a pointer type results in a null pointer value.

(emphasis mine)

Cubbi
  • 46,567
  • 13
  • 103
  • 169
  • 1
    @Jefffrey 3.7.1[basic.stc.static]/1 "All variables which do not have dynamic storage duration, do not have thread storage duration, and are not local have static storage duration" – Cubbi Dec 26 '13 at 17:59
  • @Jefffrey when will global variables expire ? – Zaffy Dec 26 '13 at 18:01
  • @Jefffrey Sorry but my cape and tights are still at the dry cleaner. – Captain Obvlious Dec 26 '13 at 18:04
  • *However*, take care! It is possible on a "freestanding" system (as opposed to a "hosted" system, where the libraries and runtime are provided for you) that it won't be zeroed. That's mainly an issue for embedded and kernel software engineers, but it's worth remembering that in those two cases there is a risk. – al45tair Feb 16 '18 at 08:21
5

I would append to the previous post of @Cubbi that according to the same Standard the scalar type is

Arithmetic types (3.9.1), enumeration types, pointer types, pointer to member types (3.9.2), std::nullptr_- t, and cv-qualified versions of these types (3.9.3) are collectively called scalar types

And then

Non-local variables with static storage duration are initialized as a consequence of program initiation.

and

Variables with static storage duration (3.7.1) or thread storage duration (3.7.2) shall be zero-initialized (8.5) before any other initialization takes place.

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
  • @ John Dibling In my opinion it is relevant enough. I made my answer more complete that there will not be such questions as yours. – Vlad from Moscow Dec 26 '13 at 18:10
  • I think this fills in the gap with the first quote. – Bartlomiej Lewandowski Dec 26 '13 at 18:11
  • 1
    @Bartlomiej Lewandowski At first I wanted to give only the definition of the scalar type that it will be clear that pointer is a scalar type. But then when I saw that someone can not understand my idea I added one more quotes.:) – Vlad from Moscow Dec 26 '13 at 18:14
  • @John Dibling, Please read the quote from the first post "To zero-initialize ... means: if T is a scalar type (3.9)," I made explanation that pointer is a scalar type. – Vlad from Moscow Dec 26 '13 at 18:24
  • Ah, now I see. Perhaps I need more coffee. Or maybe I should just take the rest of the day off. – John Dibling Dec 26 '13 at 18:26