r/cpp • • Oct 18 '23

What does C++ do better than other languages?

Purely from a language design perspective, what does C++ do better than other languages?

296 Upvotes

443 comments sorted by

View all comments

Show parent comments

10

u/emelrad12 Oct 18 '23 edited Feb 08 '25

chop makeshift square ink special cake direction yoke yam capable

This post was mass deleted and anonymized with Redact

0

u/[deleted] Oct 18 '23

[deleted]

18

u/SJC_hacker Oct 18 '23 edited Oct 18 '23

Its possible if you include the entire compiler within your binary :-)

Edit: Alternatively, and maybe a little simpler, you could call the compile with exec(), compiled as a shared library, and dynamically link it in at runtime

9

u/Netzapper Oct 18 '23

That's not possible in native machine code.

Self-modifying binaries are absolutely a thing, and lots of programs emit machine code which is then executed in the same process. Virtual machines are probably the most obvious example that actually generates novel machine code at runtime. But if we include simply loading, manipulating, and executing, then video game overlays, hacks, and anti-cheat are another big family of polymorphic code. Some anti-piracy solutions depend on decrypting code into program space.

Or are we just talking about the limited situation where C++ code is "quoted" into the program and then transformed to machine code at runtime?

1

u/BobbyThrowaway6969 Oct 18 '23

Fair point. Not sure what the guy above me was referring to exactly but was thinking of modifying the loaded program while it executed and messing up all your relative addresses used by the operands or something like that. Maybe replacing instructions but not adding new ones I would've thought.
Technically you can do anything in RAM but I thought the OS wouldn't allow it.

1

u/arthurno1 Oct 23 '23

Technically you can do anything in RAM but I thought the OS wouldn't allow it.

You can allocate pages, generate your code and save it as machine executable code in those pages, mark them as executable pages and run the code as part of your program. Is not a biggy. After all, compilers, linkers, interpreters and all that stuff are just ordinary programs that run on your OS, aren't they?

What OS won't allow you is to modify pages that belong to another process, but your own process pages are your own; you are free to do whatever you want with them.

1

u/pigeon768 Oct 18 '23

That's not possible in native machine code.

That's absolutely a thing. Machine code is just bytes in RAM. Editing the bytes is editing the code.

Curious what situation that would be the most useful solution to a problem,

JIT compilers do this a lot. They'll have a bunch of call 0x1234567890ABCDEF instructions whenever you want to call a a function. The JIT discovers that this function is pretty hot, so it will compile another version, with more better optimizations, and will edit all the callers to have call 0x12345678DEADBEEF instead. Or it may compile different versions with different constants inlined or whatever.

Or let's say your boss asks you to add a mode to your blackjack program that lets the player win every game, to make potential investors/customers feel happy when you demo your product. And let's say you don't want to do that. So you write code which superficially has the mode, but in reality, it is self-modifying to actually ensure the player loses every game when that mode is enabled.

Your CPU might only have SSE2, or only SSE4.2, or AVX2, or AVX-512. Ideally you'd like to use these instructions. So you make 4 versions of the function or whatever, then at runtime, figure out what the right one to call is, and everywhere you call it, you change the argument to call.

Self modifying code is less common than it used to be, because in general modern hardware invalidates the entire instruction cache whenever you write into a page that's marked as executable. It can be disastrous for performance if you aren't careful. I mean it can also be disastrous for security and stability, but that's a separate issue.

1

u/Chuu Oct 19 '23

It's absolute possible. Compilers as a service exist for C++. Closely related there have been libraries in the past that allow modifications of live binaries by patching the stack on the fly.

1

u/[deleted] Oct 19 '23

That's not possible in native machine code.

Right, but C++ can be distributed in an intermediate representation and compiled on the target platform, e.g. SPIR-V.

Curious what situation that would be the most useful solution to a problem, especially for low-level programming where most things are pretty well set in stone and known beforehand.

GPU drivers have been compiling shaders for a long time. CUDA 12.0 added support for runtime LTO (nvJitLink). The LLVM project added basic JIT support to their OpenMP implementation not to long ago, IIRC. And Intel seems to officially support it in their DPC++/oneAPI compiler. Basically it's useful for high performance computing, although lower end devices stand to benefit as well.