What is "Static Code Analysis"?

Go To Last Post
65 posts / 0 new

Pages

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Dan Saks often writes wisely : http://www.embedded.com/electronics-blogs/programming-pointers/4023879/Enumeration-Constants-vs-Constant-Objects
EDIT: Note that the article is from 2001. While the principles still holds, the performance of compilers might have changed considerably.

"He used to carry his guitar in a gunny sack, or sit beneath the tree by the railroad track. Oh the engineers would see him sitting in the shade, Strumming with the rhythm that the drivers made. People passing by, they would stop and say, "Oh, my, what that little country boy could play!" [Chuck Berry]

 

"Some questions have no answers."[C Baird] "There comes a point where the spoon-feeding has to stop and the independent thinking has to start." [C Lawson] "There are always ways to disagree, without being disagreeable."[E Weddington] "Words represent concepts. Use the wrong words, communicate the wrong concept." [J Morin] "Persistence only goes so far if you set yourself up for failure." [Kartman]

Last Edited: Thu. Jun 8, 2017 - 08:26 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Note that "const objects" is one of the places where 'C' does differ from C++ !

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I must have missed the memo. Why is an enum member a "better" choice than the more obvious "static const int foo = 10;" ?

 

(But I agree that either is better than a #define)

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Isn't scoped enums even better (C++11 onwards)? Should at least remove the implicit cast between enum members and int.

 

EDIT; for someones enjoyment: cpp.sh

:: Morten

 

(yes, I work for Atmel, yes, I do this in my spare time, now stop sending PMs)

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

meolsen wrote:
EDIT; for someones enjoyment: cpp.sh
Not that enjoyable!

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Sure... can't convert enum class to int implicitly

:: Morten

 

(yes, I work for Atmel, yes, I do this in my spare time, now stop sending PMs)

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

When I asked about enums occupying memory, here is what I had in mind.

 

If I #define NextVal 10, then the only "memory" used is the same flash space that any numeric value would occupy. [nb: this statement is clearly true only for Harvard architectures where code occupies some nominally nonvolatile memory]

 

But, if I do

 

    enum   {
        NextVal = 10,
        };

What is the memory "footprint"? Does is live in SRAM? Is the memory footprint any different than

 

static const uint8_t NextVal = 10;

 

 If used in a place where speed is critical, aren't there usually more operations to get it out of SRAM than from FLASH (especially if it is a single byte would otherwise be embedded in the (AVR) instruction)?

 

And, again, folks, I am not in any way critical of the suggestions and comments above; they are really appreciated. Just trying to compare and contrast (and learn).

 

Jim

Jim Wagner Oregon Research Electronics, Consulting Div. Tangent, OR, USA http://www.orelectronics.net

Last Edited: Thu. Jun 8, 2017 - 03:32 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0
#define NextVal 10

enum {
    NextVal = 10,
};

None of these occupy ANY memory.

:: Morten

 

(yes, I work for Atmel, yes, I do this in my spare time, now stop sending PMs)

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

All of those are evaluated at compile-time?

 

OK, lets extend this a little. We know that

 

uint8_t MaxVal = 10;

Does not, all by itself, occupy memory. Memory occupancy is determined as it is used. But, the simple variable declaration, just shown, does occupy memory once you write

 

x = MaxVal;

And, in a Harvard architecture, that memory occupancy is SRAM rather than FLASH because MaxVal, here as a plain variable, can be altered at other points in the program. But, if I write

 

static const uint8_t NextVal = 10;

And use it thus:

 

x = MaxVal;

Does MaxVal live in SRAM or does it live in FLASH (again, Harvard architecture)? Is the "assignment" made at compile time (as a #define would be) or is it assigned at run time?  The same question, then, of an enum. If MaxVal had been defined as an enum, and used in an assignment statement, does that value live in SRAM or does it live in FLASH? If (and, of course, this is a big "if") speed and memory footprint are important at a given point in the program, it seems to me that this would be useful information.

 

HOWEVER, this thread started out about MISRA, static code analysis, and, by inference, "safety". We know that type checking is an important aspect of safety. On the other hand, safety can include constants being in FLASH where we have a very high confidence level that nothing will alter them (no buffer over-runs, no stack overflows, no nothing!). And, by "FLASH", I don't mean p-strings, but the way the AVR op-codes embeds constants into the opcodes, the results then being in FLASH.

 

So, please, I am not trying to be argumentative, here. Quite the contrary. Trying to learn!

 

Thanks for every one's input!

 

Jim

Jim Wagner Oregon Research Electronics, Consulting Div. Tangent, OR, USA http://www.orelectronics.net

Last Edited: Thu. Jun 8, 2017 - 04:25 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Perhaps some of the C experts hammer me down, but I remember it this way.

 

Because it's legal to make a pointer to a const, it has to live in RAM on an AVR.

 

add

You can't make a pointer to enum therefore it is different. 

 

 

Last Edited: Thu. Jun 8, 2017 - 05:29 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

The case for using a name instead of a raw number is simple:
Documentation and the ease of making reliable changes.

 

Deciding between #define , static const int
and enum constants can be interesting.

 

@ka7ehk:
Unless MaxVal is global, the as-if rule allows the
compiler to do with MaxVal pretty much whatever it wants.
Even without help, the compiler will probably be
able to figure out whether MaxVal is ever changed.
If never changed, (uint8_t)10 will be quietly substituted for MaxVal.
MaxVal might be as-if-ed completely away.
Do not make a pointer to it.
x=MaxVal will probably become LDI Rx, 10 .
In C, no object may be used as the dimension of a global array.
In C++, your static const uint_t NextVal may be so used.
NextVal will almost certainly be as-if-ed away.
The necessary reasoning is required to allow its use as an array size.

 

Deciding between #define , static const int
and enum constants is not always interesting.

 

If you just want one number in the range -0x7FFF..0x7FFF
and do not need it in a constant expression,
use whatever makes you feel good.
If you need it for a constant expression, e.g. an array dimension,
in C, scratch static const int.
If you need a built-in type, scratch enum.
If you need it in assembly, use #define.

International Theophysical Year seems to have been forgotten..
Anyone remember the song Jukebox Band?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

skeeve wrote:

 

In C, IIRC the types of the enum constants are int (!!)
and sizeof(enum menage) == sizeof(int) .

Not sure about C++.

 

GNU has always had, since I've used it, the -fshort-enums compiler option.  This causes enums to have the smallest int that can contain the enumerated values.  In my code that is almost invariably an 8 bit int.

 

In the latest GNU and Microsoft C++ compilers, you can specify the size of the enums.  I don't know about C compilers.

 

   enum   Events : unsigned char  {
      None = 0,
      };
 

 

Last Edited: Thu. Jun 8, 2017 - 07:44 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

C++ now allows one to specify the underlying type of an enumeration type.

For C, IIRC, the underlying type is always int.

Optimization usually handles size issues with enumerators.

It's not much of an issue, except for arrays of enumeration variables.

Making such arrays arrays of bytes (not enums) will sidestep the size issue.

C's enums do not help much with type safety..

ints and C's enums can be implicitly converted to each other.

International Theophysical Year seems to have been forgotten..
Anyone remember the song Jukebox Band?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

PC-lint

PC-lint Plus is multi-platform instead of Windows only (now: Linux 64b, macOS, Windows) along with numerous improvements.

Gimpel Software

Gimpel Software

http://www.gimpel.com/html/index.htm

PC-lint Plus is Now Available

...

 

What is PC-lint Plus?

PC-lint Plus is a rewrite of PC-lint from the ground up. ...

...

 

"Dare to be naïve." - Buckminster Fuller

Pages