Admin Production ni-theme
Current Publication

Disabling Protection for an Individual Pointer

LabWindows/CVI

Disabling Protection for Individual Pointers

You can disable pointer checking for a particular pointer by casting it first to an arithmetic type and then back to its original type, as shown in the following macro:

#define DISABLE_RUNTIME_CHECKING(ptr) ((ptr) = (void *) ((uintptr_t)(ptr)))
{

char *charPointer;
/* run-time checking is performed for charPointer before this line */
DISABLE_RUNTIME_CHECKING(charPointer);
/* no run-time checking is performed for charPointer after this line */

}

This macro can be useful if LabWindows/CVI reports erroneous run-time errors because you set a pointer to dynamic memory in a source module and then you resize this pointer in an object module. The following steps describe how this error occurs.

  1. You declare a pointer in a source module that you compile with debugging enabled. You then assign to the pointer an address that malloc or calloc returns.

    AnyType *ptr;
    ptr = malloc(N);

  2. You reallocate the pointer in an object module so that it points to the same location in memory as before. This might occur if you call the realloc function or free the pointer and then reassign it to memory that you allocate with malloc.

    ptr = realloc(ptr, M); /* M > N */

    or

    free(ptr);
    ptr = malloc(M);

  3. You use the same pointer in a source module you compile with debugging enabled. At this point, LabWindows/CVI still expects the pointer to point to a block of memory of the original size (N).

    *(ptr+(M-1))/* This generates a fatal run-time error, */
    /* even though it is a legal expression. */

To prevent this error, use the DISABLE_RUNTIME_CHECKING macro to disable checking for the pointer after you allocate memory for it in the source module.

ptr = malloc(N);
DISABLE_RUNTIME_CHECKING(ptr);