tags:

views:

38

answers:

1

I experience quite annoying side-effect of class/structure padding with Purify. E.g.

struct something {
    int field1;
    char field2;
};

/* ... */

struct something smth, smth2;
smth.field1 = 1;
smth.field2 = 'A';

smth2 = smth;

The last line would highly likely trigger UMR warning saying that 3 bytes of initialized memory are accessed. This is obviously a false positive: there are no user data in the last three bytes of the struct, it's just a padding.

Often the warnings very very quickly fill up log files making it very hard to see the other, real problems.

Does anybody know any way to suppress the false positives?

A: 

I have no experience with purify, but perhaps explicitly initialising the first struct removes this warning:

struct something smth = {0};
struct something smth2;

I assume your structs have block scope (not file). If they have file scope the zero initialising is implicit.

schot
What dialect is this? It doesn't compile warning-free with gcc/g++ 4.1.2.
bstpierre
Ahh, the answer to my gcc warning question is in this question (which also provides hints to the OP's question): http://stackoverflow.com/questions/894300/when-zeroing-a-struct-such-as-sockaddr-in-sockaddr-in6-and-addrinfo-before-use
bstpierre
@schot: this is fine for C-like structs, but for C++ structs (ones with constructors) that wouldn't work. And yes, that is a either block scope (variables on stack) or heap.
Dummy00001
@bstpierre: Yes, that question might be useful. I usually compile with '-std=c99 -pedantic -Wall -Wextra -Wwrite-strings', but sometimes a warning is just that. BTW, gcc 4.1.2 is a pretty old version.@Dummy0001: OK, I missed the C++ part (dont' associate C++ structs).
schot
@schot: btw, memset() helps for C-style structs. but it is bogus to add piles of memset()s for a totally correct code.
Dummy00001
@schot, actually I figured out another possible solution: even for structs, in C++, implement default and copy c'tors. The copy c'tor apparently prevented compiler optimization of structure assignment, while the copy constructor does copy only the real fields, never touching the padding.
Dummy00001