How tell if pointer has been initialized?

This is a discussion on How tell if pointer has been initialized? within the C++ Programming forums, part of the General Programming Boards category; First off, thank you to everyone for all your help, I really appreciate it! If I have a pointer that ...

  1. #1
    Registered User
    Join Date
    Nov 2006
    Posts
    184

    How tell if pointer has been initialized?

    First off, thank you to everyone for all your help, I really appreciate it!

    If I have a pointer that is not null but was simply declared (and not initialized), how can i know that? How do i know it points to nothing?

    If I have:

    someType* n;

    if ( n == 0 || n == NULL )...

    It won't work because it wasn't initialized. So how do I tell that it's "empty"?

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,344
    You can't know that. An uninitialized variable could have any value (including maybe 0).

    You have to just always initialize your variables and you won't have this problem. Note that it only really occurs with POD types (basically built-in types like int, char, pointers, etc.). Class types will have their default constructor called if you don't initialize them explicitly.

  3. #3
    Malum in se abachler's Avatar
    Join Date
    Apr 2007
    Posts
    3,189
    <groan> I hate to recommend this, but here goes...
    first check for NULL, most uninitialized pointers have a value of NULL, under windows they always do. That doesnt eman you dont have a pointer that was initialized and is now invalid, thats another case.

    Other than that, I think smart pointers have a method to tell if they are initialized.
    Until you can build a working general purpose reprogrammable computer out of basic components from radio shack, you are not fit to call yourself a programmer in my presence. This is cwhizard, signing off.

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    22,915
    Quote Originally Posted by abachler View Post
    first check for NULL, most uninitialized pointers have a value of NULL, under windows they always do. That doesnt eman you dont have a pointer that was initialized and is now invalid, thats another case.
    What? Pointers do not have a default value of NULL. They have if they're explicitly initialized to it, which is recommended, but otherwise they are "undefined"...
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Registered User
    Join Date
    Nov 2006
    Posts
    184
    Quote Originally Posted by Elysia View Post
    What? Pointers do not have a default value of NULL. They have if they're explicitly initialized to it, which is recommended, but otherwise they are "undefined"...
    Yep. If I don't explicitly set it to NULL, then it's not NULL when uninitialized on windows.

    How do I know if it's a "smart pointer"?

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    7,344
    I smart pointer is a special kind of class that encapsulates the resource management of a resource. In the simple/common case that resource is a pointer that points to memory allocated with new. By choosing the right kind of smart pointer, you don't have to worry about remembering to call delete, because the smart pointer will delete the memory when it is no longer being referenced (remember that unlike Java, C++ doesn't use garbage collection and you must delete memory you allocate with new).

    In fact, in good C++ you won't really need raw pointers all that often because you'll be using smart pointers or references which are generally safer.

    An example of a smart pointer in standard C++ is auto_ptr, although there are others that can be more useful that aren't quite standard yet.

  7. #7
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    22,915
    Or TR1's shared_ptr, weak_ptr. And maybe more?
    TR1 is available for Visual Studio and... I think GCC also supports TR1?
    Otherwise, boost has a smart pointer (or several) which were borrowed and included in TR1.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Following CTools
    By EstateMatt in forum C Programming
    Replies: 5
    Last Post: 06-26-2008, 11:10 AM
  2. Quick Pointer Question
    By gwarf420 in forum C Programming
    Replies: 15
    Last Post: 06-01-2008, 04:47 PM
  3. Parameter passing with pointer to pointer
    By notsure in forum C++ Programming
    Replies: 15
    Last Post: 08-12-2006, 08:12 AM
  4. Direct3D problem
    By cboard_member in forum Game Programming
    Replies: 10
    Last Post: 04-09-2006, 04:36 AM
  5. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 01:04 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21