What is compile time and runtime

Source: Internet
Author: User

I used to hear about the concepts of compile-time and run-time. But I'm not quite sure what that means. The back took a little time to study the next. At last, I know what I mean.

Compile-time

Compile-time as the name implies is the time to compile. What do you mean by compiling? The compiler helps you translate the source code into code that the machine can recognize. (This is, of course, only in the general sense that it may actually be a language that is translated into an intermediate state. For example, Java has only the JVM-recognized bytecode, and in C # There are only MSIL that the CLR recognizes. There are also linker. Assembler. For ease of understanding we can collectively refer to the compiler)

The compilation is simply to do some translation work , such as checking the man you have careless writing the wrong keyword. What is the lexical analysis, A process such as parsing. Just like a teacher. Check the student's composition for typos and wrong sentences. If you find anything wrong, the compiler will tell you. If you use Microsoft VS, click Build. Then start compiling, if there is errors or warning information below, That's what the compiler checks out. The so-called error is the compile-time error, the process of what type of check is called compile-time type check, or static type check (so-called static is not put the real code in memory to run, but just the code as text to scan). So sometimes some people say that it's definitely wrong to allocate memory at compile time.

Run-time

The so-called runtime is the code that runs. is loaded into memory. (Your code is a dead guy before it's loaded into memory on disk.) The Run-time type check is not the same as the previous compile-time type check (or static type check). Instead of simply scanning the code, do some things in memory and make some judgments.

Examples Show

Maybe you're still confused by the idea of a concept. Also, use C + + and C # to give an example of a simple point. Example of an array out-of-bounds check (developer tool with Microsoft VS)

In C + +

int arr[] = {n/a};

int result = Arr[4];

cout<<result<<endl;

The above code you see the code that you know is wrong, the array is out of bounds. But compile with the compiler, not a bit wrong. Visible compiler is actually quite stupid, not your brain seeds so smart ah. Then start running, start dubugging. So the error, so you think that although the compiler is stupid point, But it's not too bad to find a bug when it's running. But actually the run-time array check is not a supported feature in C + +, here you dubug is a tool for you to do the check. If you choose release instead of Dubug, you will find everything is running But the results are uncertain. (Because you don't know what data is in the memory that arr[4] refers to. Anyway so the east in memory is 0101 strings, you find a continuous 4 bytes of a string 0101来 and then as an int data processing.) I run to get a scary number, the number of the next seems to be more than 1 billion. If the bank calculates how much money I have in my account, I'm going to go out of the way, make a more than 1 billion, and I'll send it. Hey, obviously think more, or just knock the code.

In C #

Int[] arr = {1, 2, 3};

int result = Arr[4];

Console.WriteLine (result);

A compilation or a normal pass. But a run on the error. C # differs from C + + in that it has a run-time type check. Checks whether the array is out of bounds or not. If the cross-border does not give you a wrong result, but a direct error. If you don't have an exception-handling statement, the entire software hangs up.

Why isn't C + + not doing array cross-check at runtime?

This should be mainly due to performance issues. C + + Design at the beginning to achieve the same efficiency as C. Try not to do extra checks at run time. Because this will undoubtedly degrade performance. But some places have to do the run-time type check. For example, polymorphism, you do not have to do the type check when running. For example. If there is a parent class father, inherit from the subclass son of Father. These two classes have virtual function fun.

Father FA;

Son so;

FA = so;

Fa.   Fun (); At compile time, it is actually to treat fun as fun in the Father class.

But at runtime actually the fun here is called the function fun in son. So it's not OK to do a run-time type check.

What is compile time and runtime

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.