| View previous topic :: View next topic |
| Author |
Message |
Albrecht Fritzsche Guest
|
Posted: Mon Dec 15, 2003 9:49 pm Post subject: Checking for uninitialized members - is this possible? |
|
|
Does anyone know of a tool which checks code like the following and
detects such uninitialized members? Unfortunately I am not aware of
such a tool.
struct Foo {
Foo() { init(); }
bool mBool;
void init() { } // init() does NOT initialize mBool
};
int main()
{
Foo f;
if (f.mBool) // use of uninitialized member
return 1;
else
return 0;
}
Thanx for any hint - or has anyone an idea how to write such a checker?
Ali
--
albrecht fritzsche
ableton, schönhauser allee 6/7
10119 berlin germany
http://www.ableton.com
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Yves Deweerdt Guest
|
Posted: Tue Dec 16, 2003 3:26 pm Post subject: Re: Checking for uninitialized members - is this possible? |
|
|
Albrecht Fritzsche wrote:
| Quote: | Does anyone know of a tool which checks code like the following and
detects such uninitialized members? Unfortunately I am not aware of
such a tool.
struct Foo {
Foo() { init(); }
bool mBool;
void init() { } // init() does NOT initialize mBool
};
int main()
{
Foo f;
if (f.mBool) // use of uninitialized member
return 1;
else
return 0;
}
Thanx for any hint - or has anyone an idea how to write such a checker?
Ali
|
Hi Ali,
Try valgrind: http://www.tldp.org/HOWTO/Valgrind-HOWTO/
Kind regards,
Yves
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Dhruv Guest
|
Posted: Tue Dec 16, 2003 6:34 pm Post subject: Re: Checking for uninitialized members - is this possible? |
|
|
On Mon, 15 Dec 2003 16:49:34 -0500, Albrecht Fritzsche wrote:
| Quote: | Does anyone know of a tool which checks code like the following and
detects such uninitialized members? Unfortunately I am not aware of
such a tool.
struct Foo {
Foo() { init(); }
bool mBool;
void init() { } // init() does NOT initialize mBool
};
int main()
{
Foo f;
if (f.mBool) // use of uninitialized member
return 1;
else
return 0;
}
Thanx for any hint - or has anyone an idea how to write such a checker?
Ali
|
Try this:
#include <iostream>
using namespace std;
#define TREAT_ASSIGN_AS_INIT
//Use only for PODs.
template <typename T>
struct Same_Type {
T data;
bool initialized;
Same_Type () : initialized(false) { }
Same_Type (T const& _data) : data (_data), initialized(true) { }
#if defined TREAT_ASSIGN_AS_INIT
T& operator= (T const& _data) { initialized = true; data = _data; return data; }
#endif
operator T& () { assert (initialized); return data; }
};
#define CHECK(TYPE) Same_Type<TYPE>
struct test {
CHECK(int) x;
CHECK(bool) flag;
test () { }
void check_all ()
{
// x = 23;
int d = x;
}
};
int main ()
{
test t;
t.check_all ();
}
Regards,
-Dhruv.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Wojciech Basalaj Guest
|
Posted: Wed Dec 17, 2003 7:21 pm Post subject: Re: Checking for uninitialized members - is this possible? |
|
|
Albrecht Fritzsche wrote:
| Quote: | Does anyone know of a tool which checks code like the following and
detects such uninitialized members? Unfortunately I am not aware of
such a tool.
|
QA C++ (http://www.programmingresearch.com/qacpp.html) will perform this and
other static analysis (compile time) checks. Here is the output for your
code:
++ WARNING ++: <=0=(0999) QAC++ Release 1.5 Jul 1 2003 12:13:27
1: struct Foo {
^
++ WARNING ++: <=1=(2400) The type name 'Foo' is in the global scope.
++ WARNING ++: <=1=(2175) This struct is not a C-type struct.
++ WARNING ++: <=1=(2173) The struct '::Foo' has constructors and/or
destructor.
++ WARNING ++: <=5=(2186) This class has non-static data members and no
non-default constructors.
2: Foo() { init(); }
^
++ WARNING ++: <=1=(2106) This function is defined inside a class
definition.
++ WARNING ++: <=5=(4054) This constructor does not have an initialisation
list.
++ WARNING ++: <=5=(4207) Member object 'mBool' is potentially not
initialized by constructor.
3:
4: bool mBool;
^
++ WARNING ++: <=1=(2100) 'mBool' is public.
5: void init() { } // init() does NOT initialize mBool
^
++ WARNING ++: <=1=(2106) This function is defined inside a class
definition.
++ WARNING ++: <=3=(4212) This non static member function does not access
any member data.
6: };
7:
8: int main()
^
++ WARNING ++: <=1=(4020) Multiple exit points found.
9: {
10: Foo f;
11: if (f.mBool) // use of uninitialized member
^
++ WARNING ++: <=1=(4060) This 'if' statement is not followed by a block.
++ WARNING ++: <=1=(4061) The 'else' statement for this if statement is not
followed by a block.
12: return 1;
13: else
14: return 0;
15: }
Regards,
Wojciech
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Albrecht Fritzsche Guest
|
Posted: Thu Dec 18, 2003 11:50 pm Post subject: Re: Checking for uninitialized members - is this possible? |
|
|
Dhruv wrote:
| Quote: | Try this:
#include
using namespace std;
#define TREAT_ASSIGN_AS_INIT
//Use only for PODs.
template
struct Same_Type {
T data;
bool initialized;
Same_Type () : initialized(false) { }
Same_Type (T const& _data) : data (_data), initialized(true) { }
#if defined TREAT_ASSIGN_AS_INIT
T& operator= (T const& _data) { initialized = true; data = _data; return data; }
#endif
operator T& () { assert (initialized); return data; }
};
#define CHECK(TYPE) Same_Type
|
Hi Druv,
what's actually the reason you've chosen a conversion operator returning
a reference about a return by value?
I mean, for POD there shouldn't be a difference anyway?
Ali
--
albrecht fritzsche
ableton, schönhauser allee 6/7
10119 berlin germany
http://www.ableton.com
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Dhruv Guest
|
Posted: Fri Dec 19, 2003 7:52 am Post subject: Re: Checking for uninitialized members - is this possible? |
|
|
On Thu, 18 Dec 2003 18:50:57 -0500, Albrecht Fritzsche wrote:
[...]
| Quote: |
Hi Druv,
Dhruv  |
| Quote: |
what's actually the reason you've chosen a conversion operator returning
a reference about a return by value?
I mean, for POD there shouldn't be a difference anyway?
|
When you write statements such as this:
CHECK(int) i;
and:
i = 23;
Then, the value of member data stored within the class should be changed,
not the temporary returned by operator T(). Hence, operator T&().
Regards,
-Dhruv.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Sarir Khamsi Guest
|
Posted: Thu Jan 08, 2004 9:10 am Post subject: Re: Checking for uninitialized members - is this possible? |
|
|
Albrecht Fritzsche <albrecht (AT) ableton (DOT) com> writes:
| Quote: | Does anyone know of a tool which checks code like the following and
detects such uninitialized members? Unfortunately I am not aware of
such a tool.
struct Foo {
Foo() { init(); }
bool mBool;
void init() { } // init() does NOT initialize mBool
};
int main()
{
Foo f;
if (f.mBool) // use of uninitialized member
return 1;
else
return 0;
}
|
A tool like PC-lint (http://www.gimpel.com/) would check to see if
you initialized data in your member initialization list. While that is
not exactly what you are asking for, it's probably a good place to
init your data members anyway.
Sarir
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Luís Pedro Coelho Guest
|
Posted: Fri Jan 09, 2004 11:51 am Post subject: Re: Checking for uninitialized members - is this possible? |
|
|
| Quote: | Albrecht Fritzsche <albrecht (AT) ableton (DOT) com> writes:
Does anyone know of a tool which checks code like the following and
detects such uninitialized members? Unfortunately I am not aware of
such a tool.
struct Foo {
Foo() { init(); }
bool mBool;
void init() { } // init() does NOT initialize mBool
};
int main()
{
Foo f;
if (f.mBool) // use of uninitialized member
return 1;
else
return 0;
}
|
valgrind (Limited to x86/Linux, I believe).
HTH,
--
Luis Pedro Coelho
check out my game of hearts for the KDE at
http://hearts.luispedro.org
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
|