C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Checking for uninitialized members - is this possible?

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated)
View previous topic :: View next topic  
Author Message
Albrecht Fritzsche
Guest





PostPosted: Mon Dec 15, 2003 9:49 pm    Post subject: Checking for uninitialized members - is this possible? Reply with 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
--
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





PostPosted: Tue Dec 16, 2003 3:26 pm    Post subject: Re: Checking for uninitialized members - is this possible? Reply with quote



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





PostPosted: Tue Dec 16, 2003 6:34 pm    Post subject: Re: Checking for uninitialized members - is this possible? Reply with quote



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





PostPosted: Wed Dec 17, 2003 7:21 pm    Post subject: Re: Checking for uninitialized members - is this possible? Reply with quote

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





PostPosted: Thu Dec 18, 2003 11:50 pm    Post subject: Re: Checking for uninitialized members - is this possible? Reply with quote

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





PostPosted: Fri Dec 19, 2003 7:52 am    Post subject: Re: Checking for uninitialized members - is this possible? Reply with quote

On Thu, 18 Dec 2003 18:50:57 -0500, Albrecht Fritzsche wrote:
[...]

Quote:

Hi Druv,
Dhruv Wink


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





PostPosted: Thu Jan 08, 2004 9:10 am    Post subject: Re: Checking for uninitialized members - is this possible? Reply with quote

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





PostPosted: Fri Jan 09, 2004 11:51 am    Post subject: Re: Checking for uninitialized members - is this possible? Reply with quote

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
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.