how the following PC-Lint warning be cleaned in C? - c

I have a question about the following code:
#define NUM_DAYS 60
#define NUM_PEOPLE 30
int days[NUM_DAYS];
int people[NUM_PEOPLE];
int size;
size = sizeof(day) > sizeof(people) ? sizeof(day) : sizeof(people);
while the macros NUM_PEOPLE can be changed to bigger than NUM_DAYS or less than NUM_DAYS, then I got the warning:
Warning 506: Constant value Boolean, how to fix it? or are there other ways to walk around it except change constant to variable?

It's a harmless warning by PC-Lint.
A sizeof expression is an integer constant (well except when its operand is a variable length array, which is not your case) and PC-Lint just notifies you that:
sizeof(day) > sizeof(people)
is a boolean constant (value 1 here) in your program.
You can get rid of the warning by adding the following comment right after the relational expression:
size = sizeof(day) > sizeof(people) /*lint -save -e506 */ ? sizeof(day)
: sizeof(people);
Be sure to include the comment right after the > expression otherwise the warning will not disappear.

Your checker is informing you that sizeof(day) and sizeof(people) are known at compile time, and so the conditional will always take one branch, and never the other.
As an alternative to suppressing the warning on your tool, you can modify your code to use a conditional preprocessor directive to make the taken branch explicit.
#if (NUM_DAYS > NUM_PEOPLE)
#define SIZE sizeof(day)
#else
#define SIZE sizeof(people)
#endif
size = SIZE;

Related

Sending parameter to a #define

I wonder to know is it possible to send a parameter to a #define macro for selecting different output
For example:
#define Row(1) LPC_GPIO0
#define Row(2) LPC_GPIO3
#define Row(3) LPC_GPIO2
Then in my code I create a loop for sending the parameter
Row(x)
This macro syntax doesn't exist.
Moreover, it can't possibly exist, because macros are expanded before the compiler compiles the code. If your x isn't a compile time constant, there would never be a way to determine what to replace in the source code for the macro invocation.
If you need to index some values, just use an array, e.g. (assuming these constants are integers):
static int rows[] = { 0, LPC_GPIO0, LPC_GPIO3, LPC_GPIO2 };
Writing
rows[x]
would have the effect you seem to have expected from your invalid macro syntax.
if you want to use macros
#define GPIOx(x) GPIO##x
and GPIOx(1) will expand to GPIO1
If you want these calculated at runtime there is a way to do what you want
#define Row(x) (x == 1 ? LPC_GPIO0 : (x == 2 ? LPC_GPIO3 : (x == 3 ? LPC_GPIO2 : ERROR_VALUE)))
Though this gets messy as the number of options increases
Also, even if you do want this evaluated at compile time, most optimizing compilers would do that for you as long as x is a constant

Extracting the number of digits in a macro constant at compile-time

I need to do some preprocessor magic. Assume that I have a global constant
#define MAX_VALUE 99999
What I need to do is to extract the length of this constant in its decimal representation at compile-time. In other words, I don't want to have another constant
#define MAX_VALUE_STRLEN 5
polluting the global namespace and I don't want to add a place in the code that needs to be changed in the case that MAX_VALUE is modified. If I have a number literal, then I can do something like
#define INTLEN(x) (sizeof(#x)/sizeof((#x)[0]) - 1)
and then INTLEN(99999) would expand to 5 at compile-time. Unfortunately, I can't do something like
INTLEN(MAX_VALUE),
because the preprocessor expands INTLEN first, so that I get
(sizeof("MAX_VALUE")/sizeof(("MAX_VALUE")[0]) - 1)
Is there a preprocessor trick that achieves what I want? Another trickier issue that I should be able to safely ignore is that can this be made generic enough that if someone decides to add a type annotation, say, 99999L to the constant that I can still get the right value?
Stringify using # and two levels of macro expansion, then chop off the terminating NUL:
#define MAX_VALUE 99999
#define STRINGIFY(x) #x
#define LENGTH(x) (sizeof(STRINGIFY(x)) - 1)
#include <stdio.h>
int main()
{
size_t n = LENGTH(MAX_VALUE);
printf("length = %zu\n", n);
return 0;
}

syntax error : missing ']' before ';' Array declaration error in C

i am writing in c, using Visual c++.
The compiler gives me the errors with the code below:
#define SIZE 3;
int myMatrix[SIZE][SIZE];
void funcMatrix(int M[SIZE][SIZE]);
The errors i get:
error C2143: syntax error : missing ']' before ';'
error C2059: syntax error : ']'
Ive tried declaring the constant differently, inside main and outside. It still doesn't work.
Would really appreciate it if someone can help me out...
You should use #define SIZE 3.
Preprocessor works as a raw string substitution, so with #define SIZE 3; your SIZE is replaced with 3; and you get:
int myMatrix[3;][3;];
void funcMatrix(int M[3;][3;]);
as a final result, hence the errors. This is a common error, then you get used to place ; at the end of C expression. However the preprocessor #define-s are not C code actually, but a simple (or not so simple) string processing operation which occurs before compilation.
In your code SIZE will be replaced by the define in this case 3; so you'll have
int myMatrix[3;][3;];
void funcMatrix(int M[3;][3;]);
Which causes a syntax error, so use #define SIZE 3 (without the ;) instead of #define SIZE 3;.
A #define NAME literally inserts whatever value comes after NAME.
Your macro expands to
int myMatrix[3;][3;];
Remove the ; in the macro definition.
you change :
#define SIZE 3;
to
#define SIZE 3
You need to remove the ; after the #define SIZE 3.
#defines don't require a semicolon
#define SIZE 3 not #define SIZE 3;
You can see what the compiler sees.
It can be a mess if you don't comment out included header files,
and I don't know how to do it in Visual C++,
but with GCC you run
gcc -E xx.c > xx.e
and you see what your code expands to after the #define's are processed.
There must be some way to do that with Visual C++ too.
Then you can still compile the .e file as c code.

Compile time variable sized string literal in C

What should be done for DD ?
if
#define HEADING_TITLE_PROJECT_NAME "<= Version Maintenance Based On Compiled DateTime =>"
#define SIZE_OF_HEADER_FOR_DECORATION_PURPOSE sizeof(HEADING_TITLE_PROJECT_NAME)
#define DD ????
#define HEADING "\r\n"DD"\r\n"HEADING_TITLE_PROJECT_NAME"\r\n"DD"\r\n"
I want to get HEADING string literal as follows:
<==================================================>
<= Version Maintenance Based On Compiled DateTime =>
<==================================================>
The = sign or anything I put once will repeat within <== ... ==> to fill the HEADING_TITLE_PROJECT_NAME space.
Can it be done this way or other.
I only want to change the HEADING_TITLE_PROJECT_NAME in coding time and nothing else.
JUST THOUGHT IF IT CAN BE DONE
:)
<==Edit start==>
Something like
#define DD\
char * get()\
{\
char arr[100] = '\0';\
for (int i=0; i < SIZE_OF_HEADER_FOR_DECORATION_PURPOSE - 1; i++)\
{\
arr[i] = "=";\
}\
return arr;\
}
<==Edit ends==>
Unfortunately, there's no automatic way to generate DD in the standard C preprocessor, as long as you want to use it the way you use in the definition of HEADING macro.
As long as you insist of having that HEADING defined they way it is currently defined, I can only suggest using a semi-automatic approach :). Define DD manually, explicitly.
#define HEADING_TITLE_PROJECT_NAME "<= Version Maintenance Based On Compiled DateTime =>"
#define DD "<==================================================>"
And then just add
STATIC_ASSERT(sizeof HEADING_TITLE_PROJECT_NAME == sizeof DD);
right under it (with your favorite implementation of STATIC_ASSERT). That way any discrepancy in DD's length will immediately trigger an error and force the developer to update the DD.
This is impossible because of sizeof is evaluated after the preprocessor, rather than before.
If you knew the length of the string in advance, it would be.
Because the proprocessor doesn't have any looping constructs, you wind up creating them. Boost does it something like this
#define REPEAT_TIMES(macro, n) REPEAT##n(macro)
#define REPEAT1(macro) MACRO
#define REPEAT2(macro) REPEAT1(macro)MACRO
#define REPEAT3(macro) REPEAT2(macro)MACRO
....
You would then simply:
#define FILLER "="
#define DD "<"REPEAT_TIMES(FILLER, 34)">"
Your implementation of DD isn't a bad idea, though it suffers from some poor syntax and undefined behavior.
const char *DDD(void)
{
static char arr[] = HEADING_TITLE_PROJECT_NAME;
if(arr[2] == ' ')
for(size_t i = 2; i + 3 < sizeof arr; i++)
arr[i] = '=';
return arr;
}
#define DD DDD()
You can't return a pointer to stack data, so you have to use a static array. We can make sure it's the right size by having it automatically set to the #defined string, then checking if it's been filled with '=' yet and, if not, fill it. Then we return a const pointer to it so that no one tries to modify it.
Your macro defines a function, get, with unspecified arguments, and returning a modifiable char * to stack data. Unfortunately, this function will be defined everywhere you use the macro, which will result in many multiple definition errors.
You can't use this with raw string concatenation, but it will work for everything else you want.

How can I use "sizeof" in a preprocessor macro?

Is there any way to use a sizeof in a preprocessor macro?
For example, there have been a ton of situations over the years in which I wanted to do something like:
#if sizeof(someThing) != PAGE_SIZE
#error Data structure doesn't match page size
#endif
The exact thing I'm checking here is completely made up - the point is, I often like to put in these types of (size or alignment) compile-time checks to guard against someone modifying a data-structure which could misalign or re-size things which would break them.
Needless to say - I don't appear to be able to use a sizeof in the manner described above.
There are several ways of doing this.
Following snippets will produce no code if sizeof(someThing) equals PAGE_SIZE; otherwise they will produce a compile-time error.
1. C11 way
Starting with C11 you can use static_assert (requires #include <assert.h>).
Usage:
static_assert(sizeof(someThing) == PAGE_SIZE, "Data structure doesn't match page size");
2. Custom macro
If you just want to get a compile-time error when sizeof(something) is not what you expect, you can use following macro:
#define BUILD_BUG_ON(condition) ((void)sizeof(char[1 - 2*!!(condition)]))
Usage:
BUILD_BUG_ON( sizeof(someThing) != PAGE_SIZE );
This article explains in details why it works.
3. MS-specific
On Microsoft C++ compiler you can use C_ASSERT macro (requires #include <windows.h>), which uses a trick similar to the one described in section 2.
Usage:
C_ASSERT(sizeof(someThing) == PAGE_SIZE);
Is there anyway to use a "sizeof" in a pre-processor macro?
No. The conditional directives take a restricted set of conditional expressions; sizeof is one of the things not allowed.
Preprocessing directives are evaluated before the source is parsed (at least conceptually), so there aren't any types or variables yet to get their size.
However, there are techniques to getting compile-time assertions in C (for example, see this page).
I know it's a late answer, but to add on to Mike's version, here's a version we use that doesn't allocate any memory. I didn't come up with the original size check, I found it on the internet years ago and unfortunately can't reference the author. The other two are just extensions of the same idea.
Because they are typedef's, nothing is allocated. With the __LINE__ in the name, it's always a different name so it can be copied and pasted where needed. This works in MS Visual Studio C compilers, and GCC Arm compilers. It does not work in CodeWarrior, CW complains about redefinition, not making use of the __LINE__ preprocessor construct.
//Check overall structure size
typedef char p__LINE__[ (sizeof(PARS) == 4184) ? 1 : -1];
//check 8 byte alignment for flash write or similar
typedef char p__LINE__[ ((sizeof(PARS) % 8) == 0) ? 1 : 1];
//check offset in structure to ensure a piece didn't move
typedef char p__LINE__[ (offsetof(PARS, SUB_PARS) == 912) ? 1 : -1];
I know this thread is really old but...
My solution:
extern char __CHECK__[1/!(<<EXPRESSION THAT SHOULD COME TO ZERO>>)];
As long as that expression equates to zero, it compiles fine. Anything else and it blows up right there. Because the variable is extern'd it will take up no space, and as long as no-one references it (which they won't) it won't cause a link error.
Not as flexible as the assert macro, but I couldn't get that to compile in my version of GCC and this will... and I think it will compile just about anywhere.
The existing answers just show how to achieve the effect of "compile-time assertions" based on the size of a type. That may meet the OP's needs in this particular case, but there are other cases where you really need a preprocessor conditional based on the size of a type. Here's how to do it:
Write yourself a little C program like:
/* you could call this sizeof_int.c if you like... */
#include <stdio.h>
/* 'int' is just an example, it could be any other type */
int main(void) { printf("%zd", sizeof(int); }
Compile that. Write a script in your favorite scripting language, which runs the above C program and captures its output. Use that output to generate a C header file. For example, if you were using Ruby, it might look like:
sizeof_int = `./sizeof_int`
File.open('include/sizes.h','w') { |f| f.write(<<HEADER) }
/* COMPUTER-GENERATED, DO NOT EDIT BY HAND! */
#define SIZEOF_INT #{sizeof_int}
/* others can go here... */
HEADER
Then add a rule to your Makefile or other build script, which will make it run the above script to build sizes.h.
Include sizes.h wherever you need to use preprocessor conditionals based on sizes.
Done!
(Have you ever typed ./configure && make to build a program? What configure scripts do is basically just like the above...)
What about next macro:
/*
* Simple compile time assertion.
* Example: CT_ASSERT(sizeof foo <= 16, foo_can_not_exceed_16_bytes);
*/
#define CT_ASSERT(exp, message_identifier) \
struct compile_time_assertion { \
char message_identifier : 8 + !(exp); \
}
For example in comment MSVC tells something like:
test.c(42) : error C2034: 'foo_can_not_exceed_16_bytes' : type of bit field too small for number of bits
Just as a reference for this discussion, I report that some compilers get sizeof() ar pre-processor time.
JamesMcNellis answer is correct, but some compilers go through this limitation (this probably violates strict ansi c).
As a case of this, I refer to IAR C-compiler (probably the leading one for professional microcontroller/embedded programming).
#define SIZEOF(x) ((char*)(&(x) + 1) - (char*)&(x)) might work
In C11 _Static_assert keyword is added. It can be used like:
_Static_assert(sizeof(someThing) == PAGE_SIZE, "Data structure doesn't match page size")
In my portable c++ code ( http://www.starmessagesoftware.com/cpcclibrary/ ) wanted to put a safe guard on the sizes of some of my structs or classes.
Instead of finding a way for the preprocessor to throw an error ( which cannot work with sizeof() as it is stated here ), I found a solution here that causes the compiler to throw an error.
http://www.barrgroup.com/Embedded-Systems/How-To/C-Fixed-Width-Integers-C99
I had to adapt that code to make it throw an error in my compiler (xcode):
static union
{
char int8_t_incorrect[sizeof( int8_t) == 1 ? 1: -1];
char uint8_t_incorrect[sizeof( uint8_t) == 1 ? 1: -1];
char int16_t_incorrect[sizeof( int16_t) == 2 ? 1: -1];
char uint16_t_incorrect[sizeof(uint16_t) == 2 ? 1: -1];
char int32_t_incorrect[sizeof( int32_t) == 4 ? 1: -1];
char uint32_t_incorrect[sizeof(uint32_t) == 4 ? 1: -1];
};
After trying out the mentioned macro's, this fragment seems to produce the desired result (t.h):
#include <sys/cdefs.h>
#define STATIC_ASSERT(condition) typedef char __CONCAT(_static_assert_, __LINE__)[ (condition) ? 1 : -1]
STATIC_ASSERT(sizeof(int) == 4);
STATIC_ASSERT(sizeof(int) == 42);
Running cc -E t.h:
# 1 "t.h"
...
# 2 "t.h" 2
typedef char _static_assert_3[ (sizeof(int) == 4) ? 1 : -1];
typedef char _static_assert_4[ (sizeof(int) == 42) ? 1 : -1];
Running cc -o t.o t.h:
% cc -o t.o t.h
t.h:4:1: error: '_static_assert_4' declared as an array with a negative size
STATIC_ASSERT(sizeof(int) == 42);
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
t.h:2:84: note: expanded from macro 'STATIC_ASSERT'
...typedef char __CONCAT(_static_assert_, __LINE__)[ (condition) ? 1 : -1]
^~~~~~~~~~~~~~~~~~~~
1 error generated.
42 isn't the answer to everything after all...
To check at compile time the size of data structures against their constraints I've used this trick.
#if defined(__GNUC__)
{ char c1[sizeof(x)-MAX_SIZEOF_X-1]; } // brakets limit c1's scope
#else
{ char c1[sizeof(x)-MAX_SIZEOF_X]; }
#endif
If x's size is greater or equal than it's limit MAX_SIZEOF_X, then the gcc wil complain with a 'size of array is too large' error. VC++ will issue either error C2148 ('total size of array must not exceed 0x7fffffff bytes') or C4266 'cannot allocate an array of constant size 0'.
The two definitions are needed because gcc will allow a zero-sized array to be defined this way (sizeof x - n).
The sizeof operator is not available for the preprocessor, but you can transfer sizeof to the compiler and check the condition in runtime:
#define elem_t double
#define compiler_size(x) sizeof(x)
elem_t n;
if (compiler_size(elem_t) == sizeof(int)) {
printf("%d",(int)n);
} else {
printf("%lf",(double)n);
}

Resources