1

So I went through this video - https://youtu.be/e4ax90XmUBc

Now, my doubt is that if C++ is compiled language, that is, it goes through the entire code and translates it, then if I do something like

void main() {
   int a;
   cout<<"This is a number = "<<a;   //This will give an error (Why?)
   a = 10;
}

Now, answer for this would be that I have not defined the value for a, which I learned in school. But if a compiler goes through the entire code and then translates it then I think it shouldn't give any error. But by giving an error like this, it looks to me as if C++ is a interpreted language. Can anyone put some light on this and help me solve my dilemma here?

  • 1
    Are you getting a compiler or runtime error? If you are getting a compiler error, the compiler can easily see that, statically, `a` is never assigned a value so any use of it will be an error. If you get a runtime error, you may be running a debug or unoptimized build. The compiler is allowed to help you find your mistakes at runtime, if you ask it to. – François Andrieux May 20 '21 at 13:31
  • 1
    Please be more specific than "giving an error like this". – molbdnilo May 20 '21 at 13:33
  • I'm surprised that this doesn't give an error: `void main()` - It should be `int main()`. How old is your compiler? – Ted Lyngmo May 20 '21 at 13:36
  • 1
    Also `#include ` and `using std::cout;` are missing. – MikeCAT May 20 '21 at 13:37
  • If you read an uninitialized variable like you are trying to do, your program has undefined behavior. It doesn't make C++ into an interpreted language. I'm not sure where you got that from. – Ted Lyngmo May 20 '21 at 13:39
  • guys, it's no error, it's just a question I asked out of curiosity and I used to code in C++ 2 years ago and I remember void main worked just fine as int main. – Rajat Sharma May 20 '21 at 13:42
  • Yes compiler will go to the undefined variable but as I learned from the internet that compiler goes through the complete code and then translate it then shouldn't it know about the variable that is assigned the value after its read..... – Rajat Sharma May 20 '21 at 13:44
  • 2
    @RajatSharma If there is no error, then what _is_ your question? No, `void main` didn't work just fine 2 years ago either. You must have used a really ancient compiler then too. "_then shouldn't it know about the variable that is assigned the value after its read_" - It will know that you assign to it after you read from it but that's too late. Your program already has undefned behavior. – Ted Lyngmo May 20 '21 at 13:45
  • please read the complete problem!! – Rajat Sharma May 20 '21 at 13:46
  • 2
    I do not understand. `But by giving an error like this, it looks to me as if C++ is a interpreted language` Giving an error without running the code is a sign of a compiled language, not scripting. `solve my dilemma here?` What are you trying to solve? `my doubt is that if C++ is compiled language` Why are you doubting that? Overall, a compiler is _not_ required to error when you write code that uses an uninitialized variable value. It could - for example gcc has `-Wuninitialized` switch. Aaand your code should not compile because `cout` is not defined. – KamilCuk May 20 '21 at 13:46
  • Your code live - https://godbolt.org/z/bMsrrx76c - with compilation warnings – Richard Critten May 20 '21 at 13:51
  • @RajatSharma I've read your complete problem. I still don't understand what mean by "_This will give an error (Why?)_". What does "_give_" and "_error_" mean in this context? – Ted Lyngmo May 20 '21 at 14:00
  • Okay, so now what I've understood from all the comments is that the compiler starts executing the code but whenever there is some undefined behaviour the compilation of the code is stopped. What I was thinking before was that even if there is some undefined behaviour, compiler will still continue the compilation and might know about the variable that i used before assigning some value. – Rajat Sharma May 20 '21 at 14:03
  • "_that the compiler starts executing the code_" - No it doesn't. It compiles the code and yes, it will continue to do so even if it is capable of seeing that you read uninitialized variables unless you tell it to abort at that stage (with `-Wuninitialized -Werror` for example) - but it's not fool proof. It's not required to be able to spot all the possible reads of uninitialized variables you have in your code. – Ted Lyngmo May 20 '21 at 14:08

1 Answers1

1

Technically, the C++ standard doesn't mandate that the compiler has to compile C++ into machine code. As an example LLVM Clang first compiles it to IR (Intermediate Representation) and only then to machine code.

Similarly, a compiler could embed a copy of itself in a program that it compiles and then, when the program is executed compile the program, immediately invoke it and delete the executable afterwards which in practice would be very similar to the program being interpreted. In practice, all widely used C++ compilers parse and assemble programs beforehand.

Regarding your example, the statement "This will give an error" is a bit ambiguous. I'm not sure if you're saying that you're getting a compile-time error or a runtime error. As such, I will discuss both possibilities.

If you're getting a compile time error, then your compiler has noticed that your program has undefined behaviour. This is something that you always want to avoid (in some cases, such as when your application operates outside the scope of the C++ Standard, such as when interfacing with certain hardware, UB occurs by definition, as certain behaviour is not defined by the Standard). This is a simple form of static analysis. The Standard doesn't mandate the your compiler informs you of this error and it would usually be a runtime error, but your compiler informed you anyway because it noticed that you probably made a mistake. For example on g++ such behaviour could be achieved by using the -Wall -Werror flags.

In the case of the error being a runtime error then you're most likely seeing a message like "Memory Access Violation" (on Windows) or "Signal 11" (on Linux). This is due to the fact that your program accessed uninitialized memory which is Undefined Behaviour.

In practice, you wouldn't most likely get any error at all at runtime. Unless the compiler has embedded dynamic checks in your program, it would just silently print a (seemingly) random value and continue. The value comes from uninitialized memory.

Side note: main returns int rather than void. Also using namespace std; considered harmful.

janekb04
  • 4,304
  • 2
  • 20
  • 51