1,727 questions
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 ...
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?
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 ...
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, ...
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();
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, ...
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 ...
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 ...
402
votes
13
answers
134k
views
What is Double Brace initialization in Java?
What is Double Brace initialization syntax ({{ ... }}) in Java?
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 ...
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 ...
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 ...
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(...
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?
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("...