1

I understand that overhead is involved in automatically initializing uninitialized pointers to null. But I am ready for this overhead; so how can I make Visual Studio 2010 C++ compiler to set all uninitialized pointers to null.

Please note I don't need smart pointer solution as I am debugging existing application.

MSalters
  • 173,980
  • 10
  • 155
  • 350
Ishekk ҆
  • 87
  • 1
  • 6
  • 5
    What is "c/c++"? Pick _one language_ to ask about. – Lightness Races in Orbit Oct 05 '15 at 10:29
  • 2
    You can make VS2010 (and a fair few other compilers) give a warning if an attempt is made to use the value of an uninitialised variable. You will be better off in the long run enabling such warnings, and coding to eliminate such warnings, than trying to turn on non-standard behaviour that will cause problems down track. – Peter Oct 05 '15 at 10:35
  • @LightnessRacesinOrbit: To be fair, "C/C++" has been a fair description of MSVC for a long time. It too was neither C nor C++, missing all of C99 and quite a few chunks of C++98. – MSalters Oct 05 '15 at 12:09

2 Answers2

10

I'm not aware of any such feature, and I've never heard of anybody looking for it before.

This seems like a very dangerous path to take.

What happens when you write your code to just assume that uninitialised pointers are null pointers, then you migrate your code to a new installation of your IDE, or give it to someone else? You will be propagating hard-to-find UB bugs. Not good!

Instead:

  • turn on warnings for uninitialised variables
  • initialise those variables

Or, for debugging existing broken code, use the fact that Visual Studio gives uninitialised data with automatic storage duration the underlying byte pattern 0xCC. Look for this value, rather than 0x00.

Community
  • 1
  • 1
Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
1

You can create a class with this behavior, something like:

template <typename T>
class Ptr
{
public:
    Ptr() = default;
    Ptr(T*ptr) : ptr(ptr) {}
    // ...
    operator T*&() {return ptr;}
private:
    T* ptr = nullptr;
};
Jarod42
  • 203,559
  • 14
  • 181
  • 302