05-01-2008 11:25 PM
05-05-2008 09:47 AM
05-05-2008 08:35 PM
05-06-2008 08:15 PM
06-21-2010 07:11 AM
Hi, I am trying to make a simple program to turn a set of check boxes into a binary number and I am getting the same error as above. My function reads:
int CVICALLBACK set (int panel, int control, int event,
void *callbackData, int eventData1, int eventData2)
{
switch (event)
{
case EVENT_COMMIT:
int i;
char hexa[9];
char str[8];
char num[2];
for(i=0;i<8;i++){
char *str="chan";
Fmt(num,"%s<%i",i);
strcat(str, num);
if(strcmp(str,"1")==0)
{
hexa[i]=1;
}
else
{
hexa[i]=0;
}
}
SetCtrlVal (panelHandle, PANEL_STRING, hexa);
break;
}
return 0;
}
The code compiles, but once the executable is ran the following message is given:
FATAL RUN-TIME ERROR: "active_channel.c", line 48, col 29, thread id 0x00001528: Out-of-bounds pointer argument (before start of memory block).
The issue seems to be with num in the strcat line. In the debugging mode, the value given for num is the corect one.
Thanks for any help,
Humberto
06-21-2010 07:49 AM
Hello Re_lambda,
what do you exactly intend to do with this code?
Here a few comments on it:
char hexa[9];
char str[8];
char num[2];
for (i = 0; i < 8; i++){
char *str="chan";
// Do you want to define a new variable or simply assign a value to 'str'? In the second case use strcpy instead
Fmt(num,"%s<%i",i); // 'num' will have values "0" through "7"
strcat(str, num); // 'str' will have values "chan0" through "chan7"
if(strcmp(str,"1")==0)
// This test will always fail as "chanx" is evidently different from "0" whichever is the value of x!
hexa[i]=1;
else
hexa[i]=0;
}
SetCtrlVal (panelHandle, PANEL_STRING, hexa);
// Regardless the result of the preceding loop, you will see nothing in the string control as neither '0' nor '1' are printable characters! Moreover, SetCtrlVal will stop at the first '0' found, as it is the standard string terminator in C
06-21-2010 08:30 AM
Hi Roberto,
Thanks for the quick reply. In my GUI panel I have eight check boxes, with Constant names chan0 through chan7. I want to create a string (hexa) which will contain an 8-bit binary number from the check boxes. If the check box is checked it'll become a one, or a zero vice versa. You are right that my strcmp is incorrect. I have changed it, but I am still not sure I can achieve what I want from the if statement.
for(i=0;i<8;i++){
char *str="chan";
Fmt(num,"%s<%i",i);
strcat(str, num);
if(*str==1)
{
hexa[i]=1;
}
else
{
hexa[i]=0;
}
For the if statement, I am looking to compare the value from the chanx constants (which should be one if the box is checked), and then translate this to a value in the binary number. I am still not able to get at least all zeroes in the binary because of the out-of-bounds error.
Thanks again for the help.
06-21-2010 09:17 AM - edited 06-21-2010 09:21 AM
Well, first of all, you are not reading actual checkbox values in any point in your loop... And you cannot address controls with their constant name only: you always need to use panel_control syntax. Nevertheless, panel_control is build up as a macro when the UIR is saved, and it's this macro you must use to address controls (you cannot use a string as the control ID parameter in a function call even if formatted in a proper way).
I normally operate via an array of control IDs, this way:
int ctl[8]; // Array of control IDs
int val = 0; // Resulting value
char msg[9]; // String to display the value in binary format
// Fill the array with control IDs
ctl[0] = PANEL_chan0;
ctl[1] = PANEL_chan1;
...
ctl[7] = PANEL_chan7;
for (i = 0; i < 8; i++) {
GetCtrlVal (panelHandle, ctl[i], &hexa[i]); // Read checkbox value
val += (hexa[i] << i); // Build the binary value
msg[7 - i] = a[i] ? '1' : '0'; // Build up the string
}
SetCtrlVal (panelHandle, PANEL_STRING, msg); // Display string