Questions tagged [initialization]

Initialization deals with the task of initializing the contents of your data structure. It's a common practice in statically-typed languages.

Initialization deals with the task of initializing the contents of data structures. This is the initialization of an object, structure, blob of data, or any entity which contains state that must be prepared prior to usage of the entity.

11649 questions
93
votes
9 answers

How to initialize KeyValuePair object the proper way?

I've seen in (amongst others) this question that people wonder how to initialize an instance of KeyValuePair, which expectedly should look like this. KeyValuePair keyValuePair = new KeyValuePair { Key = 1, Value = 2 }; It…
user1672517
92
votes
5 answers

Why does the first element outside of a defined array default to zero?

I'm studying for the final exam for my introduction to C++ class. Our professor gave us this problem for practice: Explain why the code produces the following output: 120 200 16 0 using namespace std; int main() { int x[] = {120, 200, 16}; …
90
votes
6 answers

Now that we have std::array what uses are left for C-style arrays?

std::array is vastly superior to the C arrays. And even if I want to interoperate with legacy code, I can just use std::array::data(). Is there any reason I would ever want an old-school array?
R. Martinho Fernandes
  • 228,013
  • 71
  • 433
  • 510
90
votes
3 answers

Has the new C++11 member initialization feature at declaration made initialization lists obsolete?

With C++11, we now have the ability to initialize class members in a header declaration: class aClass { private: int mInt{100}; public: aClass(); ~aClass(); }; So I'm a bit confused. Traditionally initialization…
Vector
  • 10,879
  • 12
  • 61
  • 101
90
votes
5 answers

C++11: Correct std::array initialization?

If I initialize a std::array as follows, the compiler gives me a warning about missing braces std::array a = {1, 2, 3, 4}; This fixes the problem: std::array a = {{1, 2, 3, 4}}; This is the warning message: missing braces around…
Byzantian
  • 3,208
  • 4
  • 27
  • 31
87
votes
12 answers

Variable might not have been initialized error

When I try to compile this: public static Rand searchCount (int[] x) { int a ; int b ; ... for (int l= 0; l
David
  • 14,569
  • 34
  • 78
  • 107
87
votes
4 answers

What are the signs of crosses initialization?

Consider the following code: #include using namespace std; int main() { int x, y, i; cin >> x >> y >> i; switch(i) { case 1: // int r = x + y; -- OK int r = 1; // Failed to Compile …
Jichao
  • 40,341
  • 47
  • 125
  • 198
87
votes
13 answers

Multiple initialization in C# 'for' loop

How can I (if it is possible at all) initialize multiple variables of different type in a C# for loop? Example: for (MyClass i = 0, int j = 1; j<3; j++,i++)
Lukas
  • 2,232
  • 3
  • 21
  • 34
86
votes
5 answers

How is "int* ptr = int()" value initialization not illegal?

The following code (taken from here): int* ptr = int(); compiles in Visual C++ and value-initializes the pointer. How is that possible? I mean int() yields an object of type int and I can't assign an int to a pointer. How is the code above not…
sharptooth
  • 167,383
  • 100
  • 513
  • 979
84
votes
7 answers

Compiler error: "initializer element is not a compile-time constant"

When compiling this code, I get the error "initializer element is not a compile-time constant". Can anyone explain why? #import "PreferencesController.h" @implementation PreferencesController - (id)init { self = [super init]; if (self) { …
84
votes
4 answers

Initializing an array of zeroes

It is well known that missing initializers for an array of scalars are defaulted to zero. int A[5]; // Entries remain uninitialized int B[5]= { 0 }; // All entries set to zero But is this (below) guaranteed ? int C[5]= { }; // All entries set to…
user1196549
84
votes
5 answers

Are fields initialized before constructor code is run in Java?

Can anyone explain the output of following program? I thought constructors are initialized before instance variables. So I was expecting the output to be "XZYY". class X { Y b = new Y(); X() { System.out.print("X"); } } class Y…
Praveen Kumar
  • 1,624
  • 5
  • 18
  • 21
83
votes
3 answers

Initializing container of unique_ptrs from initializer list fails with GCC 4.7

I am trying to initialise an std::vector> in a way that is equivalent to an example from Bjarne Stroustrup's C++11 FAQ: using namespace std; vector> vs { new string{"Doug"}, new string{"Adams"} }; //…
juanchopanza
  • 223,364
  • 34
  • 402
  • 480
83
votes
10 answers

initialize a const array in a class initializer in C++

I have the following class in C++: class a { const int b[2]; // other stuff follows // and here's the constructor a(void); } The question is, how do I initialize b in the initialization list, given that I can't initialize it inside…
Nathan Fellman
  • 122,701
  • 101
  • 260
  • 319
82
votes
7 answers

How to initialize an array of objects in Java

I want to initialize an array of Player objects for a BlackJack game. I've read a lot about various ways to initialize primitive objects like an array of ints or an array of strings but I cannot take the concept to what I am trying to do here (see…
John Adams
  • 4,773
  • 25
  • 91
  • 131