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
170
votes
14 answers

Convenient C++ struct initialisation

I'm trying to find a convenient way to initialise 'pod' C++ structs. Now, consider the following struct: struct FooBar { int foo; float bar; }; // just to make all examples work in C and C++: typedef struct FooBar FooBar; If I want to…
bitmask
  • 32,434
  • 14
  • 99
  • 159
169
votes
8 answers

What is the default initialization of an array in Java?

So I'm declaring and initializing an int array: static final int UN = 0; int[] arr = new int[size]; for (int i = 0; i < size; i++) { arr[i] = UN; } Say I do this instead... int[] arr = new int[5]; System.out.println(arr[0]); ... 0 will print…
Hristo
  • 45,559
  • 65
  • 163
  • 230
167
votes
4 answers

Initializing a struct to 0

If I have a struct like this: typedef struct { unsigned char c1; unsigned char c2; } myStruct; What would be the easiest way to initialize this struct to 0? Would the following suffice? myStruct _m1 = {0}; or Would I need to explicitly…
Daan Timmer
  • 14,771
  • 6
  • 34
  • 66
166
votes
8 answers

Declare and Initialize String Array in VBA

This should work according to another stack overflow post but its not: Dim arrWsNames As String() = {"Value1", "Value2"} Can anyone let me know what is wrong?
Kairan
  • 5,342
  • 27
  • 65
  • 104
154
votes
7 answers

How can I initialize a String array with length 0 in Java?

The Java Docs for the method String[] java.io.File.list(FilenameFilter filter) includes this in the returns description: The array will be empty if the directory is empty or if no names were accepted by the filter. How do I do a similar thing and…
Ron Tuffin
  • 53,859
  • 24
  • 66
  • 78
152
votes
6 answers

C char array initialization: what happens if there are less characters in the string literal than the array size?

I'm not sure what will be in the char array after initialization in the following ways. 1.char buf[10] = ""; 2. char buf[10] = " "; 3. char buf[10] = "a"; For case 2, I think buf[0] should be ' ', buf[1] should be '\0', and from buf[2] to…
lkkeepmoving
  • 2,323
  • 5
  • 25
  • 31
150
votes
12 answers

What is the difference between convenience init vs init in swift, explicit examples better

I am having troubles to understand the difference between both, or the purpose of the convenience init.
Chino Pan
  • 1,658
  • 2
  • 9
  • 9
150
votes
5 answers

Why does C++11 not support designated initializer lists as C99?

Consider: struct Person { int height; int weight; int age; }; int main() { Person p { .age = 18 }; } The code above is legal in C99, but not legal in C++11. What was the c++11 standard committee's rationale for excluding support…
xmllmx
  • 39,765
  • 26
  • 162
  • 323
149
votes
12 answers

How do I initialize a const data member?

#include using namespace std; class T1 { const int t = 100; public: T1() { cout << "T1 constructor: " << t << endl; } }; When I am trying to initialize the const data member t with 100. But it's giving me the following…
Chaitanya
  • 3,399
  • 6
  • 29
  • 47
146
votes
6 answers

Why convenience keyword is even needed in Swift?

Since Swift supports method and initializer overloading, you can put multiple init alongside each other and use whichever you deem convenient: class Person { var name:String init(name: String) { self.name = name } init() { …
Desmond Hume
  • 8,037
  • 14
  • 65
  • 112
146
votes
5 answers

C++ where to initialize static const

I have a class class foo { public: foo(); foo( int ); private: static const string s; }; Where is the best place to initialize the string s in the source file?
Thomas
  • 2,939
  • 6
  • 32
  • 29
145
votes
13 answers

How to emulate C array initialization "int arr[] = { e1, e2, e3, ... }" behaviour with std::array?

(Note: This question is about not having to specify the number of elements and still allow nested types to be directly initialized.) This question discusses the uses left for a C array like int arr[20];. On his answer, @James Kanze shows one of the…
Xeo
  • 129,499
  • 52
  • 291
  • 397
145
votes
5 answers

Default initialization of std::array?

With C++11 std::array, do I have the guarantee that the syntax std::array x; will default-initialize all the elements of the array ? EDIT: if not, is there a syntax that will work on all arrays (including zero-sized arrays) to initialize all…
Vincent
  • 57,703
  • 61
  • 205
  • 388
141
votes
11 answers

What is the correct way to start a mongod service on linux / OS X?

I've installed mongodb and have been able to run it, work with it, do simple DB read / write type stuff. Now I'm trying to set up my Mac to run mongod as a service. I get "Command not found" in response to: init mongod start In response to: ~:…
Alex C
  • 16,624
  • 18
  • 66
  • 98
141
votes
4 answers

C++ zero initialization - Why is `b` in this program uninitialized, but `a` is initialized?

According to the accepted (and only) answer for this Stack Overflow question, Defining the constructor with MyTest() = default; will instead zero-initialize the object. Then why does the following, #include struct foo { foo() =…
Duck Dodgers
  • 3,409
  • 8
  • 29
  • 43