Initializing Variables (C++ programming)?

Pages: 12
is that it is prone to errors - because of a slip, number may be used without having been assigned a meaningful value. For instance, in the above example, if when prompted with "please enter a number: ", the user enters xyz, the attempted input fails, and nothing will get assigned to number.


Ahah.. I think we're onto something. Thanks for the explanation, that's pretty good JL Borges.

So can I confirm. An uninitialized variable (global or member variable) will not cause a problem during compilation at all. The error will only occur if you try to use is it in the program when you run it and haven't assigned it a value at all or a value corresponding to it's type. Is that correct?

If an error relating to an uninitialized variable does occur during compilation, then can it be assumed that it may have been called (within your code) to create some value/ object, whatever (not reliant on user input) etc. Which could indicate that the compiler actually runs the program in it's own way when it converts it to machine language.

Is this correct?
Using uninitialized variables isn't an error unless you personally tell your compiler to treat it as such. Using an uninitialized variable is a logical error though.

And no, the compiler doesn't "run" your program, but it can follow the control flow of your program to detect use of uninitialized variables and the like.
I'm certain now that nobody knows the answer to this.


Don't be so sure.

Anyway, declaring variables as late as possible is a best practice. No, they do not have to be initialized in all cases (such as the cin example) but deferring the declaration to where a value is assigned often allows it be be an initialization rather than assignment.


Why is the spell checker on here insisting that initialize is spelled with an s?
Why is the spell checker on here insisting that initialize is spelled with an s?

Perhaps the language is set to British English?
If an error relating to an uninitialized variable does occur during compilation...

Is this correct?


No, that makes no sense at all. The compiler does not care about uninitialised values. You can force it to view them as errors (or warnings) if you like (and your compiler gives you the option), but the compiler really couldn't care less (barring JLB's const and ref as seen below; the const definitely, the ref is a funny one as a ref doesn't actually have its own existence - it's another name for something else).
Last edited on
> An uninitialized variable (global or member variable) will not cause a problem during compilation at all.

Technically, there are types which can be default-initialized (int,std::string); if these are not initialized, it will not cause a problem during compilation at all.

There are other types for which there is no default initialization; these must be initialized or there would be a compile-time error.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
int i ; // ok, int can be default-initialized (trivial)
std::string s ; // ok, the string is default-constructed

std::cout << i << '\n' ; // a good compiler may generate a diagnostic here
// something like warning: 'i' is being used before it has been explicitly initialized
// it is an informational message to the programmer that all is not well with the code 
// good programmers pay heed to these diagnostic messages or 'compiler warnings'

int* pointer ; // ok, a pointer can be default-initialized
*pointer = 7 ; // leads to undefined behaviour; the default-initialization of a pointer is trivial 
// initialization (in C this would be referred to as an 'uninitialized' or 'wild' pointer)
// a good compiler would warn about this too.

const int c = 78 ; // must be initialized or there will be a compile-time error. 
// a const int does not have  default initialization

int& reference = i ; // must be initialized or there will be a compile-time error. 
// a reference  does not have  default initialization  



> compiler actually runs the program in it's own way when it converts it to machine language.

The C++ compiler does not run anything. A compiler or a linker is a just a program, much like any other typical program - it is given some input, it processes the input and generates an output. The input to the build cascade (preprocess-compile-link) is the source code that we write, the output is a file containing an 'executable' program. The components of the operating system, the Java virtual machine, the C++ compiler, the Minesweeper program, the Chrome web browser - all of these have been created this way. The job of the compiler is over once it has compiled the code; it has no role to play in the running of the resultant program. The vast majority of Windows installations out there do not have a compiler at all - but they do run Microsoft Excel (which had been compiled and linked on some machine in Microsoft).
Thanks very much for all the replies. I believe I have a better grasp of this concept now. I think the question has been pretty much answered.

I see what you mean hast99 with regards to this. As the compiler follows the flow of the program it may access uninitialized variables which then makes them an issue. I think that's the core of this. Even if you don't have to initialize, as a safety measure, it's not allot of effort to do so. I would assume that massive amounts of code would increase the need for initializing all variables as I could imagine it would be hard to keep track of.


And no, the compiler doesn't "run" your program, but it can follow the control flow of your program to detect use of uninitialized variables and the like.


The part that confuses me is the correct way to initialize a char variable. I can't seem to hit the right syntax. Not an array, just a simple 'f' or 'm'. Do I have to initialize my variable with one of those to make sure there is no garbage at the runtime address?
Last edited on
There's no "right syntax". Everything boils down to: never use the value of a variable before it was initialized. If you ever do that, the sky will come crashing down. So you need to ensure that this never happens, one way or another.

Accessing an uninitialized variable is always a logic/program flow error. When you look up someone's age and you get some random value like 854584 due to it never having been initialized, it just means the person responsible for entering the age in the database royally messed up.
But if you enforce that the correct age must be entered upon creation of the file in the database, this could never happen in the first place.
Topic archived. No new replies allowed.
Pages: 12