C++ - value of uninitialized vector

2019-01-11 15:58发布

I understand from the answer to this question that values of global/static uninitialized int will be 0. The answer to this one says that for vectors, the default constructor for the object type will be called.

I am unable to figure out - what happens when I have vector<int> v(10) in a local function. What is the default constructor for int? What if I have vector<int> v(10) declared globally?

What I am seeing is that vector<int> v(10) in a local function is resulting in variables being 0 - but I am not sure if that is just because of my compiler or is the fixed expected behaviour.

6条回答
SAY GOODBYE
2楼-- · 2019-01-11 16:31

It is not undefined behaviour, a vector automatically initialises all its elements. You can select a different default if you want.

The constructor is:

vector( size_type, T t = T() )

and for int, the default type (returned by int()) is 0.

In a local function this:

int x;

is not guaranteed to initialise the variable to 0.

int x = int();

would do so.

int x();

sadly does neither but declares a function.

查看更多
戒情不戒烟
3楼-- · 2019-01-11 16:31

By default, vector elements are zero-initialized and not default-initialized. Those are two different but related concepts:

  • zero-initialization is what is done for static objects not having an explicit initialization and what is done for a member given in the initialized list with an initializer of (). For basic types, the value used is 0 converted to the type.

  • default-initialization is what is done for not explicitly initialized non static variables and members. For basic types it stay uninitialized.

(And C++0X introduces value-initialization which is still different).

查看更多
啃猪蹄的小仙女
4楼-- · 2019-01-11 16:33

The zero initialization is specified in the standard as default zero initialization/value initialization for builtin types, primarily to support just this type of case in template use.

Note that this behavior is different from a local variable such as int x; which leaves the value uninitialized (as in the C language that behavior is inherited from).

查看更多
疯言疯语
5楼-- · 2019-01-11 16:37

The constructor you are using actually takes two arguments, the second of which is optional. Its declaration looks like this:

explicit vector(size_type n, const T& value = T())

The first argument is the number of elements to create in the vector initially; the second argument is the value to copy into each of those elements.

For any object type T, T() is called "value initialization." For numeric types, it gives you 0. For a class type with a default constructor, it gives you an object that has been default constructed using that constructor.

For more details on the "magic parentheses," I'd recommend reading Michael Burr's excellent answer to the question "Do the parentheses after the type name make a difference with new?" It discusses value initialization when used with new specifically, but for the most part is applicable to value initialization wherever else it can be used.

查看更多
我欲成王,谁敢阻挡
6楼-- · 2019-01-11 16:43

The default initialization for an int type is to initialize it to 0.

This is true of most (if not all) primitive types: char will initialize to (char)0 (or '\0' if you prefer), float will initialize to 0.0f, and any pointer initializes to NULL. For other types, the parameterless constructor is invoked.

In general, the default initialization should happen pretty much whenever you aren't able to specify a constructor (or choose not to).

查看更多
爷的心禁止访问
7楼-- · 2019-01-11 16:44

default value of int is zero. So vector<int> v(10) will initialize all 10 elements with zero!

int main() {
        std::vector<int> v(10);
        for ( int i = 0 ; i < 10 ; i++ )
           std::cout << v[i] << std::endl;
        return 0;
}

Output:

0
0
0
0
0
0
0
0
0
0

Run at ideone: http://ideone.com/IbcyH

查看更多
登录 后发表回答