foke
foke

Reputation: 1359

force g++ to zero initialize memory

I want to write some unit tests of a library I'm writing

I have a class which looks like this one :

class A
{
public:
    A(B* pB)
    // construction details, involves my library
    // but ultimately there is a new being done ,)

    B* m_pB;
};

I'd like to check that the pointer m_pB is actually initialized, so I did something along these lines :

A* p = // creation of this object involves my library
BOOST_REQUIRE( p->m_pB != NULL );

but it happens g++ does not zero-initialize the memory, so the value of p->m_pB is just plain random. Is there a way to force g++ to zero initialize this memory for me when I new the object?

I believe Visual Studio does something similar with specific codes dependeing on where the memory is allocated.

edit: I can think of 2 backup solutions right now: using a smart pointer, or writing a new operator ...

Upvotes: 1

Views: 1161

Answers (4)

foke
foke

Reputation: 1359

Thanks for your answers, but I'd like a less invasive technique, as the classes are part of the test, and their constructor is right in the midle of the subject which is dependency injection.

I was just hopping for something similar to visual studio which sets the memory to some specific value in debug mode (I think?), through an option of g++ or something alike.

some answers/comments basically fall back to saying "don't test it, just don't make the mistake"

Finally, I've overloaded the new operator and added a call to memset(ptr, 0, size); and I get my critical check p->m_pB != __null failed, it works fine.

you all get a +1 anyway, especially DeadMg for the interesting solution

Upvotes: -2

Puppy
Puppy

Reputation: 146968

Use an always-initializing class.

template<typename T> class always_initialized {
    T t;
public:
    operator T&() { return t; }
    operator const T&() const { return t; }
    always_initialized() : t() {}
    always_initialized(const T& ref) : t(ref) {}
};

Edit: I see that most people didn't understand what this actually does. If you just set the pointer to NULL, then you have to do that separately in every constructor, and then you have to do that again for every variable, and it's also not generic for other POD things like POD structs. always_initialized is more maintainable, as you're not repeating yourself, more concise, and more generic, as it's good for any POD type or even non-POD type.

Upvotes: 5

Mark B
Mark B

Reputation: 96281

Did you consider making m_pB private and always initializing it in your constructors? By using encapsulation to enforce your class invariants you don't even need to do the kinds of checks you're implementing here.

Upvotes: 3

J T
J T

Reputation: 5156

Simply implement the default constructor. This guarantees that without passing an argument, the pointer is initialized to NULL.

class A
{
public:
    A(B* pB)
    // construction details, involves my library
    // but ultimately there is a new being done ,)

    A() : m_pB(NULL) {}

    B* m_pB;
};

Or as Fritschy points out:

A() : m_pB() {}

Upvotes: 9

Related Questions