キャンセル
次の結果を表示 
次の代わりに検索 
もしかして: 

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 件の賞賛
メッセージ1/8
5,270件の閲覧回数

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 件の賞賛
メッセージ2/8
5,250件の閲覧回数

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 件の賞賛
メッセージ3/8
5,235件の閲覧回数

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 件の賞賛
メッセージ4/8
5,214件の閲覧回数

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

 

Any NI folks like to chime in?

 

NDo

0 件の賞賛
メッセージ5/8
5,166件の閲覧回数

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 件の賞賛
メッセージ6/8
5,117件の閲覧回数

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 件の賞賛
メッセージ7/8
5,113件の閲覧回数

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 件の賞賛
メッセージ8/8
5,110件の閲覧回数