4 ms·
> [...] fully control the memory layout of your data. Is that actually guaranteed by the standard? > * Try doing anything non-trivial on a microcontroller wit
by eru 6y ago
> [...] fully control the memory layout of your data.
Is that actually guaranteed by the standard?
> * Try doing anything non-trivial on a microcontroller with 32KB of RAM. You could theoretically use a higher-level language, but you will end up using >10x amount of RAM.
It's a shame Forth never took off. Sometimes I wonder what it would have been like to live in an alternate universe where Forth had been the lingua franca instead of C.
(Ie imagine that universe's equivalent of Unix being implemented in Forth as its killer application and going from there.)
- andi999 6y agoWhat is so good about forth?
- eru 6y agoIt's rather simple, can easily run on bare metal (ie no OS). You can go very low level, but it has better higher level abstraction capabilities than C. It's also really weird, which fascinates me. Eg most Forth code doesn't use variable names. I've toyed around with Forth a bit. But never used it in anger. Wikipedia says that Forth is used today in some bootloaders and outer space. I suspect the latter is a historic accident, because the inventor of Forth worked on a big telescope. See https://en.wikipedia.org/wiki/Forth_(programming_language) https://en.wikipedia.org/wiki/Forth_(programming_language)
- im3w1l 6y ago> Is that actually guaranteed by the standard? Kinda sorta. Technically, the program runs on an abstract machine, and the compiler is free to do whatever it wants that has the same observable behavior as the code you wrote. But this is not really a problem in practice: You will get what ask for, or occasionally the optimizer will give you something even better. If you really need to ensure that you have exactly the layout you ask for, you could use "volatile" which tells the compiler that some region of memory is "special". Storing stuff there could have side effects. The memory could change at any time. This will force the compiler to follow your instruction verbatim. But it's not really advised since it will read the data from memory every time even if it's available for quick and easy access in a register - the memory could have changed, better read it again! One important thing that is not possible to express in C++, is that some value, e.g. a password, should be purged from memory. A copy of the password in some random location of memory, that is not observable, so the compiler is free to create such a copy if it wants to.