![]() In the case of the enumeration the table expands to ‘a’ which is the first column of the state table the state code. In the case of the array, the table expands to ‘b’ which is the second column, the name of the function pointer. Here's how this technique enforces my code ordering dependency: One day while talking shop with a friend of mine, I explained my problem and he suggested using the C preprocessor to enforce the ordering. He explained the basic concept: Use preprocessor directives to define a table in the form of a macro and then redefine how the macro is expanded, as required. Much better. My only problem was that the C compiler I was working with was not compliant with the C99 standard. Back to square one. ![]() ![]() Now even if I change the ordering of the enumeration, the jumptable logic doesn’t break. * declare a table of function pointers */ * declare an enumeration of state codes */Įnum I began investigating solutions for this problem and discovered that in the C99 standard there was a new way to initialize arrays. An improved way to write the above code is as follows: The ordering of the array initializers has to match the ordering of the state code enumeration exactly. Historically I would comment this type of code liberally to warn future users about this dependency, but protection based on commenting is really no protection at all. What I needed was a tool that would automatically enforce the dependency. The issue with this type of code is maintainability. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |