Thread: How tell if pointer has been initialized?

  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,366
    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,195
    <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.

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    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,366
    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
    Location
    Inside my computer
    Posts
    24,654
    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, 10:10 AM
  2. Quick Pointer Question
    By gwarf420 in forum C Programming
    Replies: 15
    Last Post: 06-01-2008, 03:47 PM
  3. Parameter passing with pointer to pointer
    By notsure in forum C++ Programming
    Replies: 15
    Last Post: 08-12-2006, 07:12 AM
  4. Direct3D problem
    By cboard_member in forum Game Programming
    Replies: 10
    Last Post: 04-09-2006, 03:36 AM
  5. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 12:04 PM