LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

Detecting uninitialized pointers

Hello,

 

I would like my function to detect whether a pointer has been initialized.  When a pointer is declared (say foo_t *foo) the CVI debugger registers 'foo' as 'Uninitialized', but (foo == NULL) is FALSE and (foo == 0) is FALSE.  In addition if(foo) is TRUE.  I've searched the forum, but have been unable to find anything about detecting if a pointer is initialized.  Is it possible to detect the initialization state of a pointer?

 

TIA,

 

NDo

0 Kudos
Message 1 of 8
(4,730 Views)

Your options are a bit limited, in the C language. Depending on the application and the reason for the pointer, you could make some elementary checks similar to your NULL comparisons. For example:

 

    static somestructure data [1000];

    somestructure *pointer;           // At this stage pointer is uninitialised - so it is better to have:

                                      // somestructure *pointer = NULL;

    ...

    pointer = &data [test];           // test should be 0 to 999, but you never know...

    function (pointer);               // Function processes the pointer

    ...

    function (somestructure *ptr) {

        if (ptr < data)

            ...                       // This is an error - we know it should point somewhere within the data array

        else if (ptr > &data [999])

            ...                       // Ditto

        else {

              ...                     // Pointer is confirmed correctly initialised - continue to use it

        }

    }

 

This is a bit contrived, as it is unusual to know for sure what your pointer is supposed to be pointing to in advance. Personally, I always NULL a pointer when I declare it, and NULL it again after freeing or closing it, and check for NULL before using it.

 

JR

0 Kudos
Message 2 of 8
(4,710 Views)

JR,

 

Thank you for responding.  Your suggested approach is definitely a valid way of range checking.  Unfortunately, I don't have a range to check against.

 

As a general question, how does the debugger know that my pointer is uninitialized and how do I access that capability?

 

--NDo

0 Kudos
Message 3 of 8
(4,695 Views)

The CVI debugger adds hidden variables and parameters to your code, with which it can track a wide variety of information, including the use of uninitialised variables at run time. I'm sure that this extra information is not accessible to a user program, otherwise we would have heard about it before now!

 

JR

0 Kudos
Message 4 of 8
(4,674 Views)

It looks like there's no way to do this.

 

Any NI folks like to chime in?

 

NDo

0 Kudos
Message 5 of 8
(4,626 Views)

JR is correct. The debugger does track pointer information, but this is not something that you can access in a release build of your program. Your best option is to do what JR suggested, which is to set a pointer's value to NULL before it's allocated and again, after it's freed. As for accessing data past the end of the buffer, there really is not much you can do about this in C, short of wrapping every indexing operation with your own custom function that always checks the index against your expected buffer size.

 

Luis

0 Kudos
Message 6 of 8
(4,577 Views)

ndo,

 

Thanks Luis.  Just to add, when you declare a pointer and don't assign it anything, at compile-time, the pointer is uninitialized and the debugger throws a warning saying the pointer has not been initialized.  The compiler is what determines what is uninitialized, so you can't tap into that functionality.  Your checks in your program for pointer == FALSE evaluates to false because that pointer is actually assigned to something, it just happenes to be a random memory address (I'm not sure how it's determined, but since you don't determine it in your program, it's up to the run-time engine or the OS).  That's why a warning is thrown, because you do need to initialize your pointers before they're used.

 

So in short, initialize your pointers to NULL before you use them.

Eric B.
National Instruments
0 Kudos
Message 7 of 8
(4,573 Views)

Thank you, all.  I think that initializing pointers to NULL is the way to go; at least that will put bounds on functions.

 

NDo

0 Kudos
Message 8 of 8
(4,570 Views)