Questions tagged [variable-initialization]

In computer programming, initialization is the assignment of an initial value for a data object or variable.

In computer programming, initialization is the assignment of an initial value for a data object or variable. The manner in which initialization is performed depends on programming language, as well as type, storage class, etc., of an object to be initialized. Programming constructs which perform initialization are typically called initializers and initializer lists. Initialization is distinct from (and preceded by) declaration, although the two can sometimes be conflated in practice. The complement of initialization is finalization, which is primarily used for objects, but not variables.

119 questions
6
votes
1 answer

What are the differences in the following variable initialization styles in MySQL?

I'm fairly new to queries which involve variable declaration in MySQL. I have seen various styles and I'm not fully clear of what these actually do. I've questions about what these actually do. 1) set @row:=0; SELECT name, @row:=@row + 1 AS…
nawfal
  • 70,104
  • 56
  • 326
  • 368
5
votes
1 answer

Would the initialization value be computed at compile time or runtime?

if i have a function that uses the rand() function as its initialization value, would that value be found when the program compiles, or when the function is run? say: int function(int init = rand()){ return init; } if it is found at compile time,…
marg
  • 51
  • 1
5
votes
4 answers

Does an int in Objective-C have a default value of 1?

I have this simple line of code: int x; x automatically has the value of 1. I don't set it to anything but when I debug, it shows that x is 1. Does an int have a default value of 1?!
aryaxt
  • 76,198
  • 92
  • 293
  • 442
5
votes
2 answers

Are variables considered defined while calculation of their initialization value?

This is my small program: enum Type { b = 1, c = 2 }; int main() { Type b = b; std::cout << b << std::endl; return 0; } Which outputs 0. Can I conclude that the above definition consists of this sequential steps? Declaration…
5
votes
1 answer

Advice on Javascript variable initialization

Is it ok in Javascript to declare multiple variables as below? var foo = bar = "Some value";
adrield
  • 629
  • 6
  • 19
4
votes
3 answers

Variable initialization (pointer and value)

Foo f1 = Foo(); // (1) Ok Foo f2 = Foo; // (2) Compiler error Foo *p1 = new Foo(); // (3) Ok Foo *p2 = new Foo; // (4) Ok. Why?? I was wondering why there exists two ways to initialize pointers. It looks a little inconsistent. Is…
Loom
  • 9,768
  • 22
  • 60
  • 112
4
votes
4 answers

Initializing all variables in C in one line and uninitialized value

In C language, is int x, y, z = 0; the same as this? int x = 0; int y = 0; int z = 0; Also, if I just say int a;, it seems that the value of a is zero even if it is uninitialized, but not undefined as described in What will be the value of…
Ka Wa Yip
  • 2,546
  • 3
  • 22
  • 35
4
votes
2 answers

Is it possible to init a variable in the while condition body for Kotlin?

In the code below: var verticesCount: Int // to read a vertices count for graph // Reading until we get a valid vertices count. while (!Assertions.checkEnoughVertices( verticesCount = consoleReader.readInt(null,…
Andrei K.
  • 534
  • 7
  • 20
4
votes
3 answers

why two ways to variable initialization

Why C++ gives two ways to initialize variable? First way is C-type initialization where we assign value to the variable at the place where we define it. int a = 0; Another way, constructor initialization which is done by enclosing the initial value…
HadeS
  • 2,020
  • 19
  • 36
4
votes
4 answers

Why C allows uninitialized local variables?

Looking into languages such as Java & C# use of uninitialized local variable is compile time error. Then why C & C++ allows uninitialized local variables? What is the reason that these languages allows this? I think many of the bad problems can't…
Destructor
  • 14,123
  • 11
  • 61
  • 126
4
votes
3 answers

Understanding Delphi Variable Declarations/Initializations

As it pertains to Delphi... When a variable is declare of a certain type, is it initialized to an OBJECT of that type? Or must the variable be assigned an expression which returns an object of that type? I'm coming from a strong Java background.…
3
votes
1 answer

Perl "my $bar if 0;" vs "my $bar = undef if 0;"

The following code will return an error, $ perl -E'sub foo { my $bar if 0; $bar++ }' This use of my() in false conditional is no longer allowed at -e line 1. But this code $ perl -E'sub foo { my $bar = undef if 0; $bar++ }' Does not return an…
Evan Carroll
  • 78,363
  • 46
  • 261
  • 468
3
votes
0 answers

Initializing large number of variables in tensorflow takes a long time

I have a large number of variables (2000) that need to be initialized. Tensorflow takes a long time to initialize these variables which is a blocker for me right now. I am running tf in distributed mode (between graph. with…
3
votes
3 answers

Why this program that performs invalid pointer initialization compiles fine in C?

I wrote a simple C program and I was expecting that it will fail in compilation but unfortunately it compiles and runs fine in C, but fails in compilation in C++. Consider below program: #include int main() { char *c=333; int…
Destructor
  • 14,123
  • 11
  • 61
  • 126
3
votes
1 answer

Why can't I access a member of this class?

I have the following three class definitions: class String { public: String() {} String(const char *) {} }; class ClassA { public: ClassA(const String &) {} }; class ClassB { public: ClassB(const ClassA…
Nathan Osman
  • 71,149
  • 71
  • 256
  • 361