|
From: Stephen T. <st...@to...> - 2007-04-04 06:22:28
|
When I have a class like:
class A {
public:
A (){}
int a;
int y;
float c;
};
that I am using in A like:
class A_Parser {
public:
boost::shared_ptr<A>
parse ( std::string file )
{
m_a_ptr.reset ( new A() );
VALGRIND_CHECK_MEM_IS_DEFINED ( &m_a_ptr, sizeof(&m_a_ptr) );
... parse A contents ...
return m_a_ptr;
}
Is this the correct way to be assured that the A pointer inside the
boost::shared_ptr is defined?
Stephen
|
|
From: Jeroen N. W. <jn...@xs...> - 2007-04-04 18:17:37
|
Stephen,
Comments inline and below:
> When I have a class like:
>
> class A {
// Assumption:
boost::shared_ptr<A> m_a_ptr;
> public:
> A (){}
>
> int a;
> int y;
> float c;
> };
>
> that I am using in A like:
>
> class A_Parser {
> public:
>
> boost::shared_ptr<A>
> parse ( std::string file )
> {
> m_a_ptr.reset ( new A() );
> VALGRIND_CHECK_MEM_IS_DEFINED ( &m_a_ptr, sizeof(&m_a_ptr) );
> ... parse A contents ...
>
> return m_a_ptr;
> }
>
> Is this the correct way to be assured that the A pointer inside the
> boost::shared_ptr is defined?
This entirely depends on the implementation of boost::shared_ptr<A>. All
the above does is to check that at least part of the internals of
boost::shared_ptr<A> are defined. It may be that the pointer to your A()
is not there, but stored somewhere else and pointed to by the storage
checked above.
Now this is really a question of C++, but it seems to me that you are
trying to test that the A() constructor did not fail quietly somehow and
returned a bogus pointer. If this is so, then you are testing part of the
C++ implementation you are using, not your program. The allocation of
storage for the object should either succeed, or throw an exception.
HTH
Jeroen.
|
|
From: Stephen T. <st...@to...> - 2007-04-04 23:43:38
|
On Wed, 2007-04-04 at 20:17 +0200, Jeroen N. Witmond wrote:
> This entirely depends on the implementation of boost::shared_ptr<A>. All
> the above does is to check that at least part of the internals of
> boost::shared_ptr<A> are defined. It may be that the pointer to your A()
> is not there, but stored somewhere else and pointed to by the storage
> checked above.
>
> Now this is really a question of C++, but it seems to me that you are
> trying to test that the A() constructor did not fail quietly somehow and
> returned a bogus pointer. If this is so, then you are testing part of the
> C++ implementation you are using, not your program. The allocation of
> storage for the object should either succeed, or throw an exception.
Thanks for the feedback. How can I implement what you are suggesting?
Should I make a constructor like the following:
class A {
A ()
: a ( 0 ),
y ( 0 ),
c ( 0.0 )
{
// Do error checking here and throw an exception is a problem is found?
}
};
Or were you considering a new function to do this?
Stephen
|
|
From: Jeroen N. W. <jn...@xs...> - 2007-04-05 17:42:35
|
> Should I make a constructor like the following:
>
> class A {
>
> A ()
> : a ( 0 ),
> y ( 0 ),
> c ( 0.0 )
> {
> // Do error checking here and throw an exception is a problem is found?
> }
>
>
> };
>
> Or were you considering a new function to do this?
This is a different question: Now you want to test what the constructors
did with the storage allocated by the C++ implementation; successfully
allocated, or execution would never reach your tests.
There are (at least :-) two answers to this question: If you want to
assert the validity of your code in the C++ way, then by all means test,
throw *and* *catch*. If you want to debug an obscure memcheck complaint
about undefined storage, you can add tests for definedness in appropriate
places in your code.
I would not consider adding blanket definedness tests anywhere in my code,
but the epilogue of the constructors is as good a place as any.
HTH,
Jeroen.
|