LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

How can I detect string invalid address without having error popup?

My codes are:
 
while  (str_1 =  va_arg(ap, char *))    // No more arguments? 
{
     if(str_1[0] == NULL)     // normally it detects no more arguments here (but error also can be happen)
           break;
}
 
When the next argument lis is empty sometime it return str_1 = invalid address , and an error popup with "General protection"
 
I did use  i_bPrevBreak = SetBreakOnLibraryErrors (FALSE); but it still generate error
 
Can we detect & avoid exception when str_1 = invalid address ?

0 Kudos
Message 1 of 2
(2,942 Views)
Hey Plit string,

Generally when people use the va_arg, one of the prior inputs helps to determine how many items are in the variable parameter list.  What is most likely happening here is that you are stepping outside the current memory space.  This would be the same thing as indexing past the end of an array. There isn't really a way to handle this. I have included some code below that shows how to use the variable parameter list with an input that determines how many paremeters have been passed into the function.

int sum_series(int num, ...)
{
    int sum = 0, t;
    va_list argptr;
   
    //Initialize argptr
    va_start(argptr, num);   
   
    //Sum the numbers
    for( ; num; num--)
    {
        t = va_arg(argptr, int);
        sum+= t;
    }
   
    //do orderly shutdown
    va_end(argptr);
    return sum;
   
}

void main()
{
    int d;
   
    d = sum_series(5,1,2,2,2,1);
   
    printf("Sum is %d\n", d);
}
Pat P.
Software Engineer
National Instruments
0 Kudos
Message 2 of 2
(2,911 Views)