LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

strtol NON-FATAL RUN-TIME ERROR (errno == 34 [0x22]). (ERANGE) Range error

Using LabWindowsCVI 2012, I have the following piece of code :

 

int sn;
int ret;
char str[16];
char *pEnd;


switch (event)
{
  case EVENT_COMMIT:

    SetWaitCursor (1);
    GetCtrlVal(panel, FOST_FOSTSN, str);

    sn = (int)strtol(str, &pEnd, 10);

    ...

}

 

str retrieved is "222" and I got the ERANGE error. Replace strtol() with strtoul() and the error gone.

 

Sadly, when I tried the following sample code from cplusplus.com and it works:

 

/* strtol example */
#include <stdio.h>      /* printf */
#include <stdlib.h>     /* strtol */

int main ()
{
  char szNumbers[] = "2001 60c0c0 -1101110100110100100000 0x6fffff";
  char * pEnd;
  long int li1, li2, li3, li4;
  li1 = strtol (szNumbers,&pEnd,10);
  li2 = strtol (pEnd,&pEnd,16);
  li3 = strtol (pEnd,&pEnd,2);
  li4 = strtol (pEnd,NULL,0);
  printf ("The decimal equivalents are: %ld, %ld, %ld and %ld.\n", li1, li2, li3, li4);
  return 0;
}

 It seems the behavior of strtol() is quite unpredictable.

0 Kudos
Message 1 of 14
(6,193 Views)

Hello,

 

When you say “str received” is 222, do you mean that the input on the control is just ‘222’?

 

The ERANGE error tends to come up when the data type cannot handle the number of the input’s size size. The strtol function attempts to read a string into a long integer, but it returns an ERANGE error if the number is larger than 2,147,483,647. The strtoul function reads into an unsigned long integer which has a range of 4,294,967,295, which is twize the range of strtol function. If you are reading a number between these two ranges, that would explain why strtoul works but strtol does not.

 

This is a non-fatal error, so what value is saved to sn when the code runs?

0 Kudos
Message 2 of 14
(6,169 Views)

Yes, the value entered in the control is "222". When it break I see the string "222" assigned to str variable but this still triggered the ERANGE error. After I replaced strtol() with strtoul(), it works fine and I got 222 exactly. This value is certainly not out of range

0 Kudos
Message 3 of 14
(6,159 Views)

I just tried after the break in strtol(), I continue and check the value of sn, it is assigned 222. Not sure why this would trigger the ERANGE error.

0 Kudos
Message 4 of 14
(6,157 Views)
Does the error still occur when the data type of the result is a long int instead of an int?
0 Kudos
Message 5 of 14
(6,152 Views)

I change the code to:

int sn;
long int longVal;
int ret;
char str[16];
char *pEnd;


switch (event)
{
case EVENT_COMMIT:

SetWaitCursor (1);
GetCtrlVal(panel, FOST_FOSTSN, str);

longVal = strtol(str, &pEnd, 10);
sn = (int)longVal;

 

I am still seeing the ERANGE error in strtol() call. See attachment.

0 Kudos
Message 6 of 14
(6,134 Views)

Hello,

 

I feel that it might have something to do with your UI: what is the data type of your numeric control?

0 Kudos
Message 7 of 14
(6,129 Views)

The control is a text box, not a numeric control. The data type is fixed as char *. Besides, the str variable is retrieved correctly and clearly has value "222". It can't be out of range. Why is it that strtoul() works fine if the UI is bad?

0 Kudos
Message 8 of 14
(6,125 Views)

OK, my guess was wrong, didn't read carefully enough Smiley Embarassed

0 Kudos
Message 9 of 14
(6,123 Views)

Hello,

 

I am currently reproducing the code, and not running into any ERANGE errors. Below is what I have.

 

	int sn;
	int ret;
	char str[16];
	char *pEnd;
	
	switch (event)
	{
		case EVENT_COMMIT:
			
			SetWaitCursor(1);
			GetCtrlVal(panelHandle, PANEL_STRING, str);
			sn = (int)strtol(str, &pEnd, 10);
			SetCtrlVal(panelHandle, PANEL_RESULT, sn);
			SetWaitCursor (0);
			break;
	}
	return 0;

I imagine that you are working with a much larger project, however. Does your ERANGE error still occur when you have a small project with only the strtol function with the necessary inputs/outputs?

0 Kudos
Message 10 of 14
(6,114 Views)