3

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?

Dummy00001
  • 16,630
  • 5
  • 41
  • 63
  • For C++, actually I figured out another possible solution: even for structs implement the default and the copy c'tors. The copy c'tor apparently prevented compiler optimization of structure assignment, while the copy constructor implementation obviously does copy only the real fields, never touching the padding. – Dummy00001 Aug 03 '10 at 12:00

1 Answers1

0

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
  • 10,958
  • 2
  • 46
  • 71
  • What dialect is this? It doesn't compile warning-free with gcc/g++ 4.1.2. – bstpierre Jul 21 '10 at 12:15
  • 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 Jul 21 '10 at 12:26
  • @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 Jul 21 '10 at 14:37
  • @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 Jul 21 '10 at 14:38
  • @schot: btw, memset() helps for C-style structs. but it is bogus to add piles of memset()s for a totally correct code. – Dummy00001 Jul 21 '10 at 14:39
  • @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 Aug 03 '10 at 12:00