By constant I (and the designers of MISRA) mean a variable sits in read-only memory. Not RAM. Typically everything you declare const in C on a microcontroller ends up in flash which is read-only.
From my experience the biggest cause of memory corruption are buffer overruns. So allowing function pointers in read-only memory only, is a sensible compromise IMHO. At least you won't get the situation that code is executed from a random address.

Arrays of function pointers may have close to no benefit over switch statement.
But lists of function pointers are a whole another story
And they make static analysis even more hopeless.
Arrays of function pointers may have close to no benefit over switch statement.
To see, what I am referring to, try reverse-engineering (or even understanding source) code of somebody else, where indirect invocation is involved in a project of hundred source files.
To see, what I am referring to, try reverse-engineering (or even understanding source) code of somebody else, where indirect invocation is involved in a project of hundred source files.
This. I was given some quite complex code as an example of achieving some end, and it was really very painful trying to work out what it did, or when, or why.


A switch is merely syntactic sugar for multiple if statements; they are equivalent.s
A switch is merely syntactic sugar for multiple if statements; they are equivalent.sYes, but one brings clarity on the page, and the other is much harder to read. If the cases are complex you have no choice but to use a bunch of if-then-elses, although they can be laid out on the page in an unnested manner, to illuminate their switch like quality,
tggzzz: if these were nested conditionals, how would you express it in a more readable way in a lookup array? I am aware of perfect hashing approaches, but clarity is the last term I would use to describe them.
… or were you responding to my post… skipping the conditions?
Typically everything you declare const in C on a microcontroller ends up in flash which is read-only.That's true for small embedded controllers
Typically everything you declare const in C on a microcontroller ends up in flash which is read-only.That's true for small embedded controllers
Not even there - for example on AVR, every const, even including string literals, will be in RAM because ROM access is weird. For other systems, it's really fifty-sixty situation; maybe nctnico's assumption holds, maybe not. The developer should know, though. Better use exact language here, and if read-only memory is meant, then say so, not "const".

Typically everything you declare const in C on a microcontroller ends up in flash which is read-only.That's true for small embedded controllers
Not even there - for example on AVR, every const, even including string literals, will be in RAM because ROM access is weird. For other systems, it's really fifty-sixty situation; maybe nctnico's assumption holds, maybe not. The developer should know, though. Better use exact language here, and if read-only memory is meant, then say so, not "const".
Yep. Especially since where things end up in memory ultimately depends on the linker (thus on the linker script for those linkers that are scriptable, which covers all targets supported by at least GCC and LLVM, and many other compilers too).
It takes a 30 s linker script modification to make all const data, even if the compiler has decided to put it in the typical ".rodata" section, go directly to RAM. Otherwise, yes, some targets with odd architectures are not even flexible enough for that and data may end up in various memory areas for specific reasons.
I've written a small executive for a MCU just a few days ago that puts absolutely everything in RAM, as everything has to execute, read and write in it, so yes, all const data lies in RAM too.

C has mutated from its original concept so much that it has become part of the problem (i.e. not the solution). tggzzz: if these were nested conditionals, how would you express it in a more readable way in a lookup array? I am aware of perfect hashing approaches, but clarity is the last term I would use to describe them.
… or were you responding to my post… skipping the conditions?
Neither.
I was responding to the posts I quoted, or in a few cases to the immediately preceding post.
Do you have sufficient brainpower to remember all the points made by each contributor in all the 85 posts in this thread? I don't.
For a more specific response, please provide the context to your questions.
tggzzz: if a person is commenting on a direct response to my post, I find it reasonable to suspect they did read my post. Perhaps I am old-fashioned and not fitting the modern world.
In the early 90s the C++ committee spent years discussion whether it should be possible or impossible to "cast away const" (needed for debuggers and optimisations, respectively). I believe the decided it should be possible. Does C allow it now? Has that changed over the decades?
In the early 90s the C++ committee spent years discussion whether it should be possible or impossible to "cast away const" (needed for debuggers and optimisations, respectively). I believe the decided it should be possible. Does C allow it now? Has that changed over the decades?I think that is a natural consequence of evolution. If you ban const being cast away an ENORMOUS amount of great existing code becomes unusable by well written new code. If you allow const to be cast away new code is less robust than it might otherwise be. They probably should have made this a compiler option. Then you can ban it for entirely new clean code, and allow it when you need to interwork. That should have been an easy choice for a committee to agree upon, but too many zealots are usually involved.
In the early 90s the C++ committee spent years discussion whether it should be possible or impossible to "cast away const" (needed for debuggers and optimisations, respectively). I believe the decided it should be possible. Does C allow it now? Has that changed over the decades?I think that is a natural consequence of evolution. If you ban const being cast away an ENORMOUS amount of great existing code becomes unusable by well written new code. If you allow const to be cast away new code is less robust than it might otherwise be. They probably should have made this a compiler option. Then you can ban it for entirely new clean code, and allow it when you need to interwork. That should have been an easy choice for a committee to agree upon, but too many zealots are usually involved.
Not an easy choice, because there are competing important valid forces in each direction.
There was far less existing code in the early 90s, when C (and C++) had to decide whether to be a general purpose programming language or a low level systems language. Either choice would have been valid. In trying to satisfy both, they satisfied neither.
Engineers and the marketplace have chosen that C (and C++) are not the future of general purpose programming languages: too complex to understand and use, too many gotchas, too low level. That leaves close to the silicon and systems uses, and the compromises made for general purpose use have complicated using C there. Hence the rise of Rust, and to a lesser extent, Go.
C will, of course continue, in the same way that COBOL continues. The faster C++ withers, the better.
In the early 90s the C++ committee spent years discussion whether it should be possible or impossible to "cast away const" (needed for debuggers and optimisations, respectively). I believe the decided it should be possible. Does C allow it now? Has that changed over the decades?I think that is a natural consequence of evolution. If you ban const being cast away an ENORMOUS amount of great existing code becomes unusable by well written new code. If you allow const to be cast away new code is less robust than it might otherwise be. They probably should have made this a compiler option. Then you can ban it for entirely new clean code, and allow it when you need to interwork. That should have been an easy choice for a committee to agree upon, but too many zealots are usually involved.
Not an easy choice, because there are competing important valid forces in each direction.
There was far less existing code in the early 90s, when C (and C++) had to decide whether to be a general purpose programming language or a low level systems language. Either choice would have been valid. In trying to satisfy both, they satisfied neither.
Engineers and the marketplace have chosen that C (and C++) are not the future of general purpose programming languages: too complex to understand and use, too many gotchas, too low level. That leaves close to the silicon and systems uses, and the compromises made for general purpose use have complicated using C there. Hence the rise of Rust, and to a lesser extent, Go.
C will, of course continue, in the same way that COBOL continues. The faster C++ withers, the better.How is making it user selectable a difficult decision? It allows any view point to be catered for.
The marketplace can't decide what it wants for the future. The last new language to get and hold a strong position was C/C++. Everything else comes and goes, as people realise the latest new thing is no panacea. Python seems to be doing pretty well at the moment, but just a few years ago Java was going quite well, had masses of cash behind it, and has fallen into decline.
I see no evidence that Java is declining. Sure the fervour is dissipating, which is inevitable when something becomes the mainstream.
I see no evidence that Java is declining. Sure the fervour is dissipating, which is inevitable when something becomes the mainstream.Its hard to say. Java has been driven out of a lot of publicly facing places where its clear what is being used. However, if its still strong in business applications it may have locked itself in, the way COBOL did in my youth, and we wouldn't be all that aware.
Yep. Yet another example of someone thinking they know what the C keywords mean.
C has grown so baroquely complex that very few people understand it as well as they think they do