Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is a static char[] buffer initialised

Tags:

c++

I have this at file scope:

static char foo[256];

Is the memory initialised to zero on all platforms and build configurations? (i.e. is it standard C++).

like image 875
P45 Imminent Avatar asked Sep 11 '14 09:09

P45 Imminent


People also ask

What is a char buffer in C?

In this program, the char array 'buffer' is that place. It is used to hold a sequence of digits until a space is encountered, at which point that sequence of digits is turned into an actual number, and the space is re-used for the next sequence of digits.

How do you initialize an empty char array?

You can initialize a one-dimensional character array by specifying: A brace-enclosed comma-separated list of constants, each of which can be contained in a character. A string constant (braces surrounding the constant are optional)

How do you initialize a char array in C++?

In C++, when you initialize character arrays, a trailing '\0' (zero of type char) is appended to the string initializer. You cannot initialize a character array with more initializers than there are array elements. In ISO C, space for the trailing '\0' can be omitted in this type of information.


1 Answers

Is the memory initialised to zero on all platforms and build configurations?

Yes, all non-local variables are zero-initialised.

(i.e. is it C++ standard)

Yes. C++11 3.6.2 specifies how non-local variables are initialised. In particular:

  1. Variables with static storage duration or thread storage duration shall be zero-initialized before any other initialization takes place.
like image 145
Mike Seymour Avatar answered Oct 12 '22 23:10

Mike Seymour