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
262
votes
9 answers

How to set default values in Go structs

There are multiple answers/techniques to the below question: How to set default values to golang structs? How to initialize structs in golang I have a couple of answers but further discussion is required.
Prateek
  • 6,644
  • 6
  • 22
  • 26
255
votes
2 answers

How to initialize array to 0 in C?

I need a big null array in C as a global. Is there any way to do this besides typing out char ZEROARRAY[1024] = {0, 0, 0, /* ... 1021 more times... */ }; ?
Claudiu
  • 224,032
  • 165
  • 485
  • 680
244
votes
6 answers

How to declare an ArrayList with values?

ArrayList or List declaration in Java has questioned and answered how to declare an empty ArrayList but how do I declare an ArrayList with values? I've tried the following but it returns a syntax error: import java.io.IOException; import…
alvas
  • 115,346
  • 109
  • 446
  • 738
243
votes
5 answers

How can I make the memberwise initialiser public, by default, for structs in Swift?

I have a Swift framework that defines a struct: public struct CollectionTO { var index: Order var title: String var description: String } However, I can't seem to use the implicit memberwise initialiser from another project that imports…
bandejapaisa
  • 26,576
  • 13
  • 94
  • 112
234
votes
8 answers

Error "initializer element is not constant" when trying to initialize variable with const

I get an error on line 6 (initialize my_foo to foo_init) of the following program and I'm not sure I understand why. typedef struct foo_t { int a, b, c; } foo_t; const foo_t foo_init = { 1, 2, 3 }; foo_t my_foo = foo_init; int main() { …
tomlogic
  • 11,489
  • 3
  • 33
  • 59
230
votes
11 answers

Create a list with initial capacity in Python

Code like this often happens: l = [] while foo: # baz l.append(bar) # qux This is really slow if you're about to append thousands of elements to your list, as the list will have to be constantly resized to fit the new elements. In Java,…
Claudiu
  • 224,032
  • 165
  • 485
  • 680
216
votes
4 answers

Initializing C# auto-properties

I'm used to writing classes like this: public class foo { private string mBar = "bar"; public string Bar { get { return mBar; } set { mBar = value; } } //... other methods, no constructor ... } Converting Bar to an auto-property…
dlamblin
  • 43,965
  • 20
  • 101
  • 140
210
votes
2 answers

What do the following phrases mean in C++: zero-, default- and value-initialization?

What do the following phrases mean in C++: zero-initialization, default-initialization, and value-initialization What should a C++ developer know about them?
Bill
  • 11,595
  • 6
  • 44
  • 52
208
votes
12 answers

How can I use an array of function pointers?

How should I use array of function pointers in C? How can I initialize them?
john
202
votes
7 answers

Does the default constructor initialize built-in types?

Does the default constructor (created by the compiler) initialize built-in-types?
201
votes
8 answers

How do C++ class members get initialized if I don't do it explicitly?

Suppose I have a class with private memebers ptr, name, pname, rname, crname and age. What happens if I don't initialize them myself? Here is an example: class Example { private: int *ptr; string name; string *pname; …
bodacydo
  • 75,521
  • 93
  • 229
  • 319
201
votes
10 answers

How to initialise memory with new operator in C++?

I'm just beginning to get into C++ and I want to pick up some good habits. If I have just allocated an array of type int with the new operator, how can I initialise them all to 0 without looping through them all myself? Should I just use memset? Is…
dreamlax
  • 93,976
  • 29
  • 161
  • 209
191
votes
13 answers

Initialize a byte array to a certain value, other than the default null?

I'm busy rewriting an old project that was done in C++, to C#. My task is to rewrite the program so that it functions as close to the original as possible. During a bunch of file-handling the previous developer who wrote this program creates a…
DeVil
  • 1,999
  • 2
  • 11
  • 14
175
votes
16 answers

How to initialize a List to a given size (as opposed to capacity)?

.NET offers a generic list container whose performance is almost identical (see Performance of Arrays vs. Lists question). However they are quite different in initialization. Arrays are very easy to initialize with a default value, and by…
Boaz
  • 25,331
  • 21
  • 69
  • 77
173
votes
9 answers

What happens to a declared, uninitialized variable in C? Does it have a value?

If in C I write: int num; Before I assign anything to num, is the value of num indeterminate?
atp
  • 30,132
  • 47
  • 125
  • 187