|  | find uninitialized class members? |  | |
| | | Tom Horsley |  |
| Posted: Sat Sep 13, 2008 2:29 am Post subject: find uninitialized class members? |  |
Is there some compiler (or a g++ option I couldn't find) that can warn about constructors which fail to initialize some class members? Or even some code analysis tool which will do that?
I clearly have an uninitialized member somewhere in a large complicated app, and I was hoping I could use a tool to reduce the number of constructors I need to review.
(I know it must be something uninitialized because overriding the global new operators and calling calloc() in them makes the bug disappear .
-- [ See LINK for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ] |
| |
| | | analizer |  |
| Posted: Sat Sep 13, 2008 8:30 am Post subject: Re: find uninitialized class members? |  |
| |  | |
On 13 ÓÅÎÔ, 06:29, Tom Horsley <tomhors...@comcast.net> wrote:
| Quote: | Is there some compiler (or a g++ option I couldn't find) that can warn about constructors which fail to initialize some class members? Or even some code analysis tool which will do that?
I clearly have an uninitialized member somewhere in a large complicated app, and I was hoping I could use a tool to reduce the number of constructors I need to review.
(I know it must be something uninitialized because overriding the global new operators and calling calloc() in them makes the bug disappear .
|
{ clc++m banner removed -- please, please don't quote it. -mod }
Which platform do you have? On Linux I recommend to use Valrind (http://valgrind.org/) it provides a lot of information about program execution including using of uninitialized values, so you should just launch your program through valgring (e.g.: valgrind --leak-check=full --log-file=my.log ./ my_program program_parameters) and when program end execution you will receive all required information including file names and line numbers where uninitialized values have been used (some code investigation will be required anyway). Of course, binaries should be compiled in debug mode and source files should be available.
I don't know analogs for Valgrind of Windows, probably you should check Purify, I have no much experience of working with it.
-- [ See LINK for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ] |
| |
| | | mzdude |  |
| Posted: Sat Sep 13, 2008 8:29 pm Post subject: Re: find uninitialized class members? |  |
On Sep 12, 10:29 pm, Tom Horsley <tomhors...@comcast.net> wrote:
| Quote: | Is there some compiler (or a g++ option I couldn't find) that can warn about constructors which fail to initialize some class members? Or even some code analysis tool which will do that?
|
PC lint will warn that not all members are initialized during construction. And even if they are initialized in the wrong order.
-- [ See LINK for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ] |
| |
| | | Seungbeom Kim |  |
| Posted: Sun Sep 14, 2008 3:09 am Post subject: Re: find uninitialized class members? |  |
| |  | |
Tom Horsley wrote:
| Quote: | Is there some compiler (or a g++ option I couldn't find) that can warn about constructors which fail to initialize some class members? Or even some code analysis tool which will do that?
I clearly have an uninitialized member somewhere in a large complicated app, and I was hoping I could use a tool to reduce the number of constructors I need to review.
(I know it must be something uninitialized because overriding the global new operators and calling calloc() in them makes the bug disappear .
|
I'm not sure how a constructor can fail to initialize a class member: the member's constructor should be called, whether it is explicitly mentioned in the initializer list. (And if it's not mentioned, the default constructor should be available; otherwise, the program is ill-formed.)
Are you talking about compiler bugs?
-- Seungbeom Kim
[ See LINK for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ] |
| |
| | | Erik Wikström |  |
| Posted: Sun Sep 14, 2008 5:24 pm Post subject: Re: find uninitialized class members? |  |
| |  | |
On 2008-09-14 05:09, Seungbeom Kim wrote:
| Quote: | Tom Horsley wrote: Is there some compiler (or a g++ option I couldn't find) that can warn about constructors which fail to initialize some class members? Or even some code analysis tool which will do that?
I clearly have an uninitialized member somewhere in a large complicated app, and I was hoping I could use a tool to reduce the number of constructors I need to review.
(I know it must be something uninitialized because overriding the global new operators and calling calloc() in them makes the bug disappear .
I'm not sure how a constructor can fail to initialize a class member: the member's constructor should be called, whether it is explicitly mentioned in the initializer list. (And if it's not mentioned, the default constructor should be available; otherwise, the program is ill-formed.)
|
Is that true for types such as int and double? Of course that depends on how you define initialised, if just using the value that happens to be in the memory they are occupying is initialisation to you then there is no problem.
-- Erik Wikström
[ See LINK for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ] |
| |
| | | Tom Horsley |  |
| Posted: Sun Sep 14, 2008 5:28 pm Post subject: Re: find uninitialized class members? |  |
On Sat, 13 Sep 2008 21:09:43 -0600, Seungbeom Kim wrote:
| Quote: | I'm not sure how a constructor can fail to initialize a class member: the member's constructor should be called
|
I'm talking about plain old scalar members (ints, pointers, etc) where the constructor fails to initialize the memory, and the member type has no default constructor to do the initialization.
-- [ See LINK for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ] |
| |
| | | terminator |  |
| Posted: Sun Sep 14, 2008 10:49 pm Post subject: Re: find uninitialized class members? |  |
| |  | |
On Sep 14, 9:28 pm, Tom Horsley <tomhors...@comcast.net> wrote:
| Quote: | On Sat, 13 Sep 2008 21:09:43 -0600, Seungbeom Kim wrote: I'm not sure how a constructor can fail to initialize a class member: the member's constructor should be called
I'm talking about plain old scalar members (ints, pointers, etc) where the constructor fails to initialize the memory, and the member type has no default constructor to do the initialization.
-- [ LINK info about ] [ comp.lang.c++.moderated. First time posters: Do this! ]
|
pointers are ddificult to trace but for other scalar types you can prepare your own debug library:
simulate those types with UDTs lacking default ctor:
class my_int{ public: my_int(int); .... private: explicit my_int(){throw "my|_int";}; };
use preprocessor to replace built-in with corresponding UDT:
/*myDEBUG.h*/ #ifndef my_guard_ //TODO:place UDT definitions here #define my_guard_ #define int my_int ....... #endif
/*user.h*/ #ifdef _DEBUG #include "myDEBUG.h" #endif //TODO:the rest of user.h
regards, FM.
-- [ See LINK for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ] |
| |
| | | Jiang |  |
| Posted: Mon Sep 15, 2008 11:14 pm Post subject: Re: find uninitialized class members? |  |
| |  | |
On Sep 15, 2:28 am, Tom Horsley <tomhors...@comcast.net> wrote:
| Quote: | On Sat, 13 Sep 2008 21:09:43 -0600, Seungbeom Kim wrote: I'm not sure how a constructor can fail to initialize a class member: the member's constructor should be called
I'm talking about plain old scalar members (ints, pointers, etc) where the constructor fails to initialize the memory, and the member type has no default constructor to do the initialization.
|
Several concepts for this issue:
1. As Seungbeom Kim said, you can not bypass the member *initialization* during object construction. Maybe this is not that accurate, but you can think member initialization is an entry-action for object construction. Check 12.6.2/p5 if you need the details for member initialization.
2. "plain old scalar member" has constructors, which are called trivial constructor (12.1/p5), trivial copy constructor (12.8/p6).
3. For initialization, according to the code we have different kinds of initialization rules. For your code, default initialization and value initialization are important in my mind. More specifically, compiler generates initialization code according to whether you provide the initializer (and what is it) or not.
---------------------------
* case 1: no initializer
struct foo { int i; };
foo obj1;
Here, obj1.i has indeterminate initial value (garbage) according to 8.5/p9 (default initialization).
---------------------------
* case 2: has initializer, which is an empty set of parentheses
struct foo { foo() : i() {} // initializer is an empty parentheses int i; };
foo obj2;
Here obj2.i is value initialized, from 8.5/p5, compiler will zero initialize i therefore assert( obj2.i == 0) yields true.
---------------------------
* case 2: has initializer with value
struct foo { foo() : i(100) {} // initializer is a value int i; };
foo obj3;
Here obj3.i is value initialized and assert( obj3.i == 100) yields true.
--------------------------
Therefore you see the compiler will never "fail to initialize" the member, but indeed the initialized value perhaps is not the value you expected, especially for case 1. Providing necessary initializer is definitely your job, if you really want the member is initialized as desired value.
Regards,
Jiang
-- [ See LINK for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ] |
| |
| | | Tom Horsley |  |
| Posted: Tue Sep 16, 2008 10:30 pm Post subject: Re: find uninitialized class members? |  |
On Mon, 15 Sep 2008 17:14:57 -0600, Jiang wrote:
| Quote: | Here, obj1.i has indeterminate initial value (garbage) according to 8.5/p9 (default initialization).
|
Precisely. And I'm looking for a tool which will point out instances of this case in the code. I'm tempted to try and add a new warning to g++, but have a feeling it would take a lot longer to do that than to review all the constructors in my source (but it could be close .
-- [ See LINK for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ] |
| |
| | | Greg Herlihy |  |
| Posted: Wed Sep 17, 2008 10:59 am Post subject: Re: find uninitialized class members? |  |
| |  | |
On Sep 16, 3:30 pm, Tom Horsley <tomhors...@comcast.net> wrote:
| Quote: | On Mon, 15 Sep 2008 17:14:57 -0600, Jiang wrote: Here, obj1.i has indeterminate initial value (garbage) according to 8.5/p9 (default initialization).
Precisely. And I'm looking for a tool which will point out instances of this case in the code. I'm tempted to try and add a new warning to g++, but have a feeling it would take a lot longer to do that than to review all the constructors in my source (but it could be close .
|
Compile the C++ source code with both g++'s "-Wunitialized" flag and some level of optimization enabled ("-O1" or greater) - in order to have g++ warn about uninitialized class members.
For example:
class A { public: A() {}
int get_i() const { return i; }
private: int i; };
int main() { A a;
int i2 = a.get_i();
return i2; }
Compiler (g++ 4.0.1) Output:
test.cc: In function ‘int main()’: test.cc:21: warning: ‘a$i’ is used uninitialized in this function
Greg
-- [ See LINK for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ] |
| |
| Page 1 of 2 .:. Goto page 1, 2 Next | |
|
|