HP Code Advisor Diagnostics

struct A {
unsigned int f1 : 1;
};
int main() {
A obj;
obj.f1 = 30;
}
Action:
Ensure that the assigned value is within the range of the values that the bit-field can store.
Reference:
4253 unsigned value cannot be less than zero
Cause:
An ordered comparison between an unsigned value and a constant that is less than or equal to
zero often indicates a programming error. A negative value is converted to an unsigned value
before the comparison, so any negative value compares larger than most unsigned values. If the
code is correct, the comparison could be more clearly coded by testing for equality with zero.
Example:
int main() {
unsigned data = 30;
if(data <= 0)
return 1;
}
Action:
Cast (or otherwise rewrite) one of the operands of the compare to match the signedness of the
other operand, or compare for equality with zero.
Reference:
4255 padding size of struct %sq1 with %s2 bytes to alignment boundary
Cause:
Padding bytes have been inserted for proper alignment of the struct.
Example:
typedef unsigned char uint8_t;
typedef unsigned int uint32_t;
int main() {
struct X {
uint32_t data_size;
uint8_t status;
};
}
Action:
Insertion of padding bytes themselves is not a problem but you need to ensure that you do not
use hard coded offsets for accessing fields of the struct through pointers, use offset of macro
instead. In some cases the number of padding bytes being inserted can be reduced by reordering
the fields.
Reference:
ANSI/ISO C++ 18.1(5)
62 Diagnostics Details