Skip to main content
105 votes
7 answers
19k views

When is using an uninitialized variable undefined behavior?

If I have: unsigned int x; x -= x; it's clear that x should be zero after this expression, but everywhere I look, they say the behavior of this code is undefined, not merely the value of x (until ...
user541686's user avatar
  • 210k
177 votes
9 answers
183k views

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?
rxmnnxfpvg's user avatar
  • 30.9k
310 votes
9 answers
85k views

Is there a difference between copy-initialization and direct-initialization?

Suppose I have this function: void my_test() { A a1 = A_factory_func(); A a2(A_factory_func()); double b1 = 0.5; double b2(0.5); A c1; A c2 = A(); A c3(A()); } In each ...
rlbond's user avatar
  • 67.6k
274 votes
9 answers
173k views

Why should I prefer to use member initializer lists?

I'm partial to using member initializer lists for my constructors, but I've long since forgotten the reasons behind this. Do you use member initializer lists in your constructors? If so, why? If not, ...
oz10's user avatar
  • 158k
1121 votes
8 answers
143k views

Do the parentheses after the type name make a difference with new?

If 'Test' is an ordinary class, is there any difference between: Test* test = new Test; and Test* test = new Test();
David Read's user avatar
  • 11.2k
1184 votes
27 answers
2.3m views

How to initialize all members of an array to the same value?

I have a large array in C (not C++ if that makes a difference). I want to initialize all members of the same value. I could swear I once knew a simple way to do this. I could use memset() in my case, ...
Matt's user avatar
  • 87.9k
89 votes
12 answers
522k views

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<x.length; l++) { if (x[l] == 0) a++ ; else ...
David's user avatar
  • 14.9k
25 votes
5 answers
29k views

What should happen when printing an uninitialized variable?

I've checked myself, I wrote a program like this int main() { int i; cout << i; return 0; } I ran the program a few times and the result was same all the time, zero. I've tried it in C and ...
omidh's user avatar
  • 2,802
402 votes
13 answers
134k views

What is Double Brace initialization in Java?

What is Double Brace initialization syntax ({{ ... }}) in Java?
Saurabh Gokhale's user avatar
644 votes
18 answers
693k views

How to initialize private static data members in a header file

What is the best way to initialize a private, static data member in C++? I tried this in my header file, but it gives me weird linker errors: class foo { private: static int i; }; int ...
Jason Baker's user avatar
35 votes
1 answer
18k views

Using special auto start servlet to initialize on startup and share application data

I need to get some configuration and connect to external resources/objects/systems somewhere and store it in application scope. I can see two ways to setup my application: Overriding the init() in ...
riz's user avatar
  • 385
244 votes
8 answers
420k views

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 ...
tomlogic's user avatar
  • 11.7k
637 votes
17 answers
1.3m views

How to initialize a struct in accordance with C programming language standards

I want to initialize a struct element, split in declaration and initialization. This is what I have: typedef struct MY_TYPE { bool flag; short int value; double stuff; } MY_TYPE; void function(...
cringe's user avatar
  • 13.9k
642 votes
5 answers
333k views

What are the advantages of list initialization (using curly braces)?

MyClass a1 {a}; // clearer and less error-prone than the other three MyClass a2 = {a}; MyClass a3 = a; MyClass a4(a); Why?
Oleksiy's user avatar
  • 39.7k
915 votes
15 answers
154k views

Efficiency of Java "Double Brace Initialization"?

In Hidden Features of Java the top answer mentions Double Brace Initialization, with a very enticing syntax: Set<String> flavors = new HashSet<String>() {{ add("vanilla"); add("...
Jim Ferrans's user avatar

15 30 50 per page
1
2 3 4 5
116