Skip to main content
125 votes

How does garbage collection work in languages which are natively compiled?

Does the compiler store a copy of some garbage collection program and paste it into each executable it generates? It sounds unelegant and weird, but yes. The compiler has an entire utility library, ...
Kilian Foth's user avatar
108 votes
Accepted

Can we make general statements about the performance of interpreted code vs compiled code?

No. In general, the performance of a language implementation is primarily dependent on the amount of money, resources, manpower, research, engineering, and development spent on it. And specifically, ...
Jörg W Mittag's user avatar
95 votes

Detect manual changes to an autogenerated C header

I think you are approaching this problem from the wrong angle. Better let the generator place a clear and visible comment at the beginning of the C header file like // This file is autogenerated, ...
Doc Brown's user avatar
  • 214k
80 votes

Can we make general statements about the performance of interpreted code vs compiled code?

Generalizations and specific scenarios are literally opposites. You seem to be contradicting yourself. On the one hand, you want to make a general statement about interpreted vs compiled languages. ...
loneboat's user avatar
  • 971
79 votes
Accepted

Is it bad practice to use a C++ compiler just for function overloading?

I wouldn't go so far as to call it "bad practice" per se, but neither am I convinced it's really the right solution to your problem. If all you want is four separate functions to do your four data ...
Philip Kendall's user avatar
60 votes

How does garbage collection work in languages which are natively compiled?

Or does the compiler include some minimal garbage collector in the compiled program's code. That’s an odd way of saying “the compiler links the program with a library that performs garbage collection”...
Konrad Rudolph's user avatar
57 votes

Is it bad practice to use a C++ compiler just for function overloading?

Using only some features of C++ while otherwise treating it as C is not exactly common, but also not exactly unheard of either. In fact, some people even use no features at all of C++, except the ...
Jörg W Mittag's user avatar
57 votes
Accepted

How does garbage collection work in languages which are natively compiled?

Garbage collection in a compiled language works the same way as in an interpreted language. Languages like Go use tracing garbage collectors even though their code is usually compiled to machine code ...
avdgrinten's user avatar
54 votes

Do compilers optimise in concurrency?

Asuming expensive_calc_one and expensive_calc_two are pure functions Unfortunately, determining whether a function is pure is equivalent to solving the Halting Problem in the general case. So, you ...
Jörg W Mittag's user avatar
46 votes

What is the common procedure used when compilers statically type check "complex" expressions?

What is the usually method used when a compiler is type checking expressions with many operators and operands. Read wikipages on type system and type inference and on Hindley-Milner type system, ...
Basile Starynkevitch's user avatar
41 votes

Detect manual changes to an autogenerated C header

Don't commit the generated C header file at all. In fact, delete the current file (thanks @user1936), change the script to call the header file .g.h (thanks @davidbak), and add it to .gitignore, so ...
Jonathan's user avatar
  • 565
37 votes

Can we make general statements about the performance of interpreted code vs compiled code?

As a rule of thumb, an interpreted program is about 2x–10x slower than writing the program in the interpreter's host language, with interpreters for more dynamic languages being slower. This is ...
amon's user avatar
  • 135k
37 votes
Accepted

How do compilers work in a language that doesn't allow recursion?

Recursion can only be programmed either by having a call to function A within the definition of A itself (direct), or by having function A call function B, and function B call function A (indirect). ...
Kilian Foth's user avatar
37 votes

Why is it necessary to mark classes as not inherited from? Can't an optimizer automatically detect that virtual calls are unnecessary?

Don't we need to take a step back here? Under the hood, it generally all boils down to simply functions being called the with the this pointer as first arg. It's good to question things from first ...
Alexander's user avatar
  • 5,145
29 votes
Accepted

What exactly is a compile target?

Compilers are, in essence, translators that take input in one language and produce output in another. For example, Eiffel Software's compiler takes Eiffel-language input and produces C. GCC for ...
Blrfl's user avatar
  • 20.4k
27 votes

How do compilers work in a language that doesn't allow recursion?

To support recursion, a language needs to support function calls and a call stack. When a language doesn't allow recursion, it's typically because the language lacks one of these features. I'm not ...
JacquesB's user avatar
  • 61k
25 votes
Accepted

Is it possible to create a "bootstrapped" interpreter independent of the original interpreter?

The short answer is: you are right in your suspicion, you always need either another interpreter written in X or a compiler from Y to some other language for which you have an interpreter already. ...
Jörg W Mittag's user avatar
23 votes

How does garbage collection work in languages which are natively compiled?

How would this work with compiled languages though? Your wording is wrong. A programming language is a specification written in some technical report (for a good example, see R5RS). Actually you are ...
Basile Starynkevitch's user avatar
21 votes
Accepted

How are variables stored in a language compiler or interpreter?

Interpreter An intepreter will work about the way you guessed. In a simple model, it will maintain one dictionary with the variable names as dictionary keys and the variable values as dictionary ...
Ralf Kleberhoff's user avatar
21 votes

How can we avoid showing the literal path in the exception's stack trace?

Couple of pointers. You should never expose stacktrace to users. Thats a security risk. You should also never expose exception messages to users, only for custom exceptions that you know can not ...
Anders's user avatar
  • 671
19 votes
Accepted

Why do compilers typically only generate executables for the platform they are installed on?

what makes it difficult for say the visual C++ compiler on windows to generate a linux binary executable file? Other than an unwillingness to do that on Microsoft's part, absolutely nothing. The ...
Blrfl's user avatar
  • 20.4k
18 votes
Accepted

How are generics implemented in a modern compiler?

How are generics implemented in a modern compiler? I invite you to read the source code of a modern compiler if you wish to know how a modern compiler works. I'd start with the Roslyn project, which ...
Eric Lippert's user avatar
  • 46.4k
18 votes

Can we make general statements about the performance of interpreted code vs compiled code?

You absolutely can say something about the performance of compiled/interpreted technologies. But first, you must define "performance". If you're building a computationally simple embedded system, then ...
jhbh's user avatar
  • 289
17 votes
Accepted

What is the common procedure used when compilers statically type check "complex" expressions?

Recursion is the answer, but you descend into each subtree before handling the operation: int a = 1 + 2 - 3 * 4 - 5 to tree form: (assign (a) (sub (sub (add (1) (2)) (mul (3) (4))) (5)) Inferring ...
Simon Richter's user avatar
17 votes

Detect manual changes to an autogenerated C header

First a disclaimer: I don't think this is a good idea. But here is one way to do it anyway: void check_file_time() { if (strcmp(__TIMESTAMP__, "Sun Feb 16 19:38:35 2020") != 0) { asm(...
jpa's user avatar
  • 1,378
15 votes

How can a compiler be written for a language that allows rewriting code at runtime (such as Lisp macros)?

You are confusing two different concepts in your question. Macros are not about compiling code at runtime. They are the exact opposite: they are about running code at compile time. So, in this case, ...
Jörg W Mittag's user avatar
15 votes
Accepted

Why can't C# implicitly convert int to string?

In both cases, the language is offering syntactic sugar features, rather than doing implicit casting: var s = "1" + 1; is converted to the string Concat(object, object) method during compilation: ...
David Arno's user avatar
  • 39.5k
15 votes

Is it bad practice to use a C++ compiler just for function overloading?

An objective answer to your dilemma will depend on: Whether the size of the executable grows significantly if you use C++. Whether there is any noticeable negative impact on performance if you use C++...
R Sahu's user avatar
  • 1,986
15 votes
Accepted

Why does LLVM have an assembly-like IR rather than a tree-like IR? Or: why do projects target LLVM IR instead of clang's AST?

There's a number of inter-related questions here, I'll try to separate them as best I can. Why do other languages build on LLVM IR and not clang AST? This is simply because clang is a C/C++ front ...
Alex's user avatar
  • 3,922
15 votes
Accepted

Is there any use case for using 'L' letter after a number literal in C?

In the C language expressions are typed inside-out: literals have types. E.g. 123 is an int, and 123U is an unsigned int, 123L is a long int and so on. The type of an expression depends on the types ...
amon's user avatar
  • 135k

Only top scored, non community-wiki answers of a minimum length are eligible