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, ...
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, ...
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, ...
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. ...
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 ...
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”...
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 ...
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 ...
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 ...
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, ...
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 ...
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 ...
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). ...
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 ...
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 ...
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 ...
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. ...
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 ...
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 ...
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 ...
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 ...
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 ...
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 ...
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 ...
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(...
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, ...
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:
...
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++...
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 ...
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 ...
Only top scored, non community-wiki answers of a minimum length are eligible
Related Tags
compiler × 499programming-languages × 64
c++ × 61
c × 60
interpreters × 42
parsing × 41
optimization × 36
java × 27
language-design × 27
assembly × 26
c# × 23
python × 18
javascript × 17
performance × 16
computer-science × 14
grammar × 14
jit × 14
language-agnostic × 12
compilation × 12
lexer × 12
machine-code × 12
design × 11
data-structures × 11
llvm × 11
architecture × 10