-1

In C++, how do I test if a pointer has been initialized? These don't run, as expected. How do I test? Here is the code:

TCHAR *sNullString;
INT bInitialized;
if (*sNullString) 
    bInitialized = TRUE;
if (sNullString)
    bInitialized = TRUE;
Jens Gustedt
  • 76,821
  • 6
  • 102
  • 177
JeffR
  • 765
  • 2
  • 8
  • 23
  • You have to initialize `TCHAR *sNullString;` first. – user0042 Oct 16 '17 at 15:29
  • That's the point of the question. How do I *test*. And you marked the question down when your answer doesn't address the question? – JeffR Oct 16 '17 at 15:30
  • 2
    There is no way to tell. – Rotem Oct 16 '17 at 15:30
  • 3
    Local non-static variables are *not* initialized, their value is *indeterminate* and will seem almost random. Dereferencing a pointer which have not be initialized leads to [*undefined behavior*](https://en.wikipedia.org/wiki/Undefined_behavior). In short, unless you explicitly initialize local variables they are not initialized, there's no way to safely test this. – Some programmer dude Oct 16 '17 at 15:30
  • 1
    Also note that there is a difference in behavior between C and C++ here: In C you can *read* the (indeterminate) value of an uninitialized local variable, it might not lead to undefined behavior. In C++ even the action of reading the indeterminate value is undefined. So in the future please don't use both the C and C++ tags for questions, unless you do some comparison between the languages. Use the language tag you actually program in, *only*. – Some programmer dude Oct 16 '17 at 15:40

1 Answers1

3

In C++, how do I test if a pointer has been initialized?

In C++, it is not possible to test if a variable (of pointer type, or otherwise) has been initialized. Same applies to dynamically allocated objects.

How do I test?

You don't, because you can't. Instead, you write your code so that you can prove without a doubt that the pointer is initialized. An example:

TCHAR *sNullString = nullptr;

Here, there is no doubt: sNullString is initialized, and it points to null.

eerorika
  • 232,697
  • 12
  • 197
  • 326
  • I've inherited source code and was seeing if there was a way to do a shortcut to test if a variable was initialized. Since that won't work, time to analyze some source code! Thank you. – JeffR Oct 16 '17 at 15:37
  • 2
    @JeffR turn on warnings. I believe, every compiler has a way to warn you about uninitialized variables. – Revolver_Ocelot Oct 16 '17 at 16:00
  • Would you (or somebody) know where it is in Visual Studio 2017? – JeffR Oct 16 '17 at 17:28
  • 1
    @Jeff - It is a level 1 warning, on by default. [C4700 uninitialized local variable 'name' used](https://learn.microsoft.com/en-us/cpp/error-messages/compiler-warnings/compiler-warning-level-1-and-level-4-c4700?f1url=https%3A%2F%2Fmsdn.microsoft.com%2Fquery%2Fdev15.query%3FappId%3DDev15IDEF1%26l%3DEN-US%26k%3Dk(C4700)%26rd%3Dtrue). – Bo Persson Oct 16 '17 at 17:42
  • 1
    @JeffR Here's a list of the possible values the debugger uses for initialization: https://stackoverflow.com/questions/370195/when-and-why-will-an-os-initialise-memory-to-0xcd-0xdd-etc-on-malloc-free-new – user0042 Oct 16 '17 at 19:13