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
0
votes
1 answer

Not initialized variable in nested for js react

I am trying to create 40 cells with react , each should have a different title. For example a0 ...a9, b0 ..b9 etc. This is my code const mapItems = () => { let playerId = ["a", "b", "c", "d"]; let cellId; let l, j; for (j = 0; j < 4;…
0
votes
1 answer

How do I get C++ signaling_nan() to throw an exception, without needing if tests?

I am trying to use signaling_nan() to automatically throw an exception if a variable whose value will be set post-initialization, is used before being set. I have tried using the floating point exception functionalities via fenv.h, however that…
0
votes
1 answer

When do instance variables get auto-initialized in Java?

Stages of instance initialization are captured for example in this question. Nevertheless, it doesn't capture the moment when the instance variable get's declared and auto-initialized. From my test code below I can see that instance variable int "i"…
0
votes
1 answer

JS - Auto-initialisation behaviour of `let` vs `var`

In 'Chapter 5: The (Not So) Secret Lifecycle of Variables' of 'You Don't Know JS Yet', the section titled "Uninitialized Variables (aka, TDZ)" says that the only way to initialise an uninitialised let variable is with an assignment attached to a…
0
votes
1 answer

Difference between int i(x); and int i = x;

I have been practicing C++ in HackerRank. There I was seeing different submissions and something new came in my sight. someone used int i(0) in for loop like for (int i(0), mark; i
0
votes
2 answers

Why does this C code take so long to compile and execute if the variable is not initialized?

The following C code takes so long to compile and execute. #include int main() { int max; printf("Enter a value for max: "); scanf("%d", &max); for (int i=0; i
0
votes
2 answers

C++ -> Which is faster?? int a(5); or int a = 5;

I know this is easy one but I'm confused whether int a (5); is faster than int a=5; in C++, as I read somewhere, if a constructor with only one parameter is defined in the class, the initialisation can be done with an equal sign.(A statement can be…
0
votes
2 answers

Is it variable initialization failure?

Being following the book "Programming Principles and Practice Using C++" by Bjarne Stroustrup for learning C++ right from the very basics. It all proved good until this happened... int main() { cout << "Please enter your first name and…
Vector
  • 17
  • 3
0
votes
1 answer

Declaring static initializer block before declaring variable explanation

How does this code work fine and prints 9? public class Dog{ static { age=9; } static int age=7; } And this code doesn't compile(illegal forward reference)? Notice I changed age in static block. public class Dog{ static…
0
votes
1 answer

Iterating through variable names with for loop

I am trying to use a for loop to iterate through a series of variable names each ending in a number from 1 to 10. I have seen a few other answers to this question but have been unable to make any work for my specific situation. My code is as…
Josh
  • 11
0
votes
1 answer

Memory problems shown at run time because of the initialisation of an int data type

After running the program I can only insert data for the first student, and after that it only displays what I should insert, but does not give me the right to do so. No compile errors. Although I think that the problem is when I assign the…
0
votes
2 answers

Initialize a multiple levels hash in rails

So I have some codes look like the following: @foo ||= {} @foo[:bar] ||= {} @foo[:bar][:baz] ||= {} I am not concerning the performance, but the cleanness. Is there a more beautiful way or better way to do so?
PeterWong
  • 15,951
  • 9
  • 59
  • 68
0
votes
4 answers

Eager Initialization over static block

As I'm trying to understand things better, I'm realizing how less I know. I'm sorry, if this sounds like a simple or silly question. Do we really need static block, if it is ONLY for initialization of STATIC variables without any other logic coded…
0
votes
1 answer

What Form Does the Implicitly-Declared Default Constructor Take?

So let's say I'm working with this toy example: struct Foo { int member; }; I know that the default constructor won't default initialize member. So if I do this, member remains uninitialized: const Foo implicit_construction. As an aside this…
0
votes
5 answers

How can I pass a group of objects to a function for creation?

So I'm working in Delphi 2007 and I am cleaning up my code. I have come to notice that in a great many procedures I declare a number of different variables of the same type. for example the one procedure I am looking at now I declare 4 different…
Tim
  • 1,549
  • 1
  • 20
  • 37