Skip to main content
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
  • 221k
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
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
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,205
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
  • 62.4k
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
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,406
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
  • 136k
15 votes
Accepted

What are the 'practical' advantages of LR parser over LL parser 'in today'?

True LL parsers are fast and simple – but also completely unable to parse many interesting languages. Many real-world programming languages are in the LALR class, which is closely related to LR but ...
amon's user avatar
  • 136k
15 votes

How does code work without getting compiled or interpreted?

Compilers aren't magic - they're just programs, which can be run from the command line. Visual Studio Code has a "Terminal" tab in the bottom pane of the default UI, where you can invoke the ...
autophage's user avatar
  • 880
14 votes

Why C allows multiple global declarations of the same variable but NOT multiple local declarations?

@msc gives a good introduction into the rules behind this behavior. I noticed that if I declare a global variable multiple times the compiler does not even output a warning. C has three kinds of ...
Erik Eidt's user avatar
  • 34.8k
13 votes
Accepted

Why does Forth's flexibility make a grammar inappropriate for it?

A "normal" word is pretty much just a subroutine. ...but you can write a user-defined defining word, which change how the compiler works. For example, a definition normally starts with a colon (":") ...
Jerry Coffin's user avatar
  • 44.9k
13 votes

Difference between direct and indirect function() calls

The main difference between the direct and the indirect call, is that: the direct call uses an instruction call with a fixed address as argument. After the linker has done its job, this address will ...
Christophe's user avatar
  • 82.3k
12 votes
Accepted

In c++, is there a way inside a function block, to execute cleanup code after the value has been returned?

Addressing the question about cleanup code: yes, there is a way, it's called RAII (for Resource Acquisition Is Initialization, an acronym whose limpid clarity is rivalled only by CRTP), and it is both ...
Useless's user avatar
  • 12.9k
12 votes

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

Imagine this extremely simple example A value = Random.Shared.Next(0, 2) == 0 ? new B() : new C(); value.M(); abstract class A { public abstract void M(); } class B : A { public override ...
Euphoric's user avatar
  • 38.2k
11 votes

Why C allows multiple global declarations of the same variable but NOT multiple local declarations?

According to coding-guidelines : In the set of translation units and libraries that constitutes an entire program, each declaration of a particular identifier with external linkage denotes the ...
msc's user avatar
  • 307
11 votes
Accepted

How to make compiler portable?

You cannot make a portable compiler (not in the sense you dream of). You could make a compiler for a particular target machine -or language- (and that machine might even be something like LLVM or ...
Basile Starynkevitch's user avatar
11 votes
Accepted

Only one number type in language design

We have different number representation in general because they have different strengths and weaknesses, be it speed, precision, or range. Also this has to be the case because we cannot represent all ...
jk.'s user avatar
  • 10.3k
11 votes
Accepted

Should my lexer allow what is obviously a syntax error?

Your lexer is never going to be able to diagnose all syntax errors unless you make it as powerful as the parser itself. This would be a large and totally unnecessary amount of work, and the only ...
Kilian Foth's user avatar
10 votes
Accepted

Does the Perfect Compiler trump source differences

No: First, let's note that there are many, many equivalent yet differing code sequences when it comes to machine code (let alone other languages).  A simple case is swapping two registers where ...
Erik Eidt's user avatar
  • 34.8k
10 votes

How to optimize a mixed stack/register bytecode with control flow and side effects?

There's a huge body of research on this topic, so I really recommend reading a compiler book. Muchnik's book can work well as a reference. I really like Modern Compiler Implementation but I think it's ...
Iulian Dragos's user avatar
10 votes

How can one interpret an Abstract Syntax Tree without recursion?

You can systematically calculate such an approach. For simplicity, I'll consider evaluating arithmetic expressions, but the transforms are completely mechanical. I'll use Haskell as the language. data ...
Derek Elkins left SE's user avatar
10 votes

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

From this blog: <PropertyGroup> <PathMap>$([System.IO.Path]::GetFullPath('$(MSBuildThisFileDirectory)'))=./</PathMap> </PropertyGroup> So instead of this: Unhandled ...
lonix's user avatar
  • 241
10 votes
Accepted

code generation - would C be a good compiler backend?

Define "good" and "bad" backend According to what criteria do you evaluate whether it is a good or bad solution? Without knowing, we are more in subjective beliefs rather than objective advise: ...
Christophe's user avatar
  • 82.3k
10 votes
Accepted

In C++, If a member function can be made static with no change to functionality, are there any performance or memory benefits from doing so?

Practically in 99.9% of cases: no. Theoretically: maybe. You won't be passing the implicit this parameter to every function, and not passing that could save you bytes and the time passing those bytes. ...
Philip Kendall's user avatar
10 votes

Is it possible to make a compiler for any dynamic/script/interpreter language

Can we (Is it possible to) make a compiler for any dynamic/script/interpreter language, Yes, it is. Note that there is no such thing as an "interpreter" language. An interpreter is a ...
Jörg W Mittag's user avatar
9 votes
Accepted

Why am I advised to not inline functions that are called only once?

In C and C++, the inline keyword has effects that make inline possible, but also many other effects – such as suppressing the one-definition rule (ODR). This can obscure bugs. Therefore, the inline ...
amon's user avatar
  • 136k
9 votes
Accepted

How does a compiler work when it's not directly compiling to machine code

I know the compilation process goes with this flow: source -> parse -> AST -> intermediate code -> assembly -> machine code ,and in the case of Java you will have bytecode which ...
Jörg W Mittag's user avatar
9 votes
Accepted

Relationship between the C standard libraries and Java standard libraries

Which is the relationship between the standard libraries of C language and the standard libraries of other software platforms, e.g. Java, .NET, Python? There is no relationship. Some library ...
Jörg W Mittag's user avatar

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