LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

Program puts garbage to file only in release configuration

Solved!
Go to solution

Got completely tired of trying to figure out what is happening with these char strings(

 

I have a very simple function which writes a string to file. String consists of five "substrings" .I make this string by "strcat", here is my code:

 

int formRegString() {
	char time[15], *temp, aiTemp[8], tempAI[20], ain[257], dout[65], din[65];
	double now, aiVal;
	int i, color, len;
	
	
	
	GetCurrentDateTime(&now);
	FormatDateTimeString(now, "%H:%M:%S.%3f ", time, sizeof(time));
	
	for (i=0; i<16; i++) {
	
		GetCtrlAttribute(panelEngineer, array_BUT_Out_1[i], ATTR_CMD_BUTTON_COLOR, &color);	
		if (color == VAL_GREEN) temp = "1 ";
		else temp = "0 ";
		strcat(dout, temp);
	}
	
	for (i=0; i<16; i++) {
	
		GetCtrlAttribute(panelEngineer, array_BUT_Out_2[i], ATTR_CMD_BUTTON_COLOR, &color);
		if (color == VAL_GREEN) temp = "1 ";
		else temp = "0 ";
		strcat(dout, temp);															 
		
		
	}

	
	
	for (i=0; i<16; i++) {
	
		GetCtrlVal(panelEngineer, array_LED_In_1[i], &color);
		if (color == 1) temp = "1 ";
		else temp = "0 ";
		strcat(din, temp);
		
	}
	
	for (i=0; i<16; i++) {

		GetCtrlVal(panelEngineer, array_LED_In_2[i], &color);
		if (color == 1) temp = "1 ";
		else temp = "0 ";
		strcat(din, temp);												
		
	}
	

	for (i=0; i<32; i++) {
		
		Fmt(tempAI, "%f[p3] ", fVoltage[i] * kGrade[i] * 0.1);
		strcat(ain, tempAI);										    
		
	}
	

	char result[strlen(time) + strlen(dout) + strlen(din) + strlen(ain) + 1];						 
	
	strcat(result, time);   
	strcat(result, ain);
	strcat(result, dout);
	strcat(result, din);																			   


	
	WriteLine(regFileHandle, result, sizeof(result));										 
	
return 0;

}

 When i run a program in debug configutration - everything is OK.

Here is the output:

 

22.01.2015
09:45:13.203 0.000 0.000 -25.049 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.0000 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 

 

(I have 2 chars of garbage in the end, but it's ok)

 

When i run release configuration something magical happenes (or it's just me who is stupid and angry with this damn CHAR things instead of normal STRINGS as in other languages). 

Here is my output for release config:

 

22.01.2015
09:46:29.000 д?е0.000 0.000 -25.049 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 └═Кh╕═Кh0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

 

 

Why does this garbage appear in my strings? And why only in release config?

I know about need to put '\0' to the end of char strings - i've tried to put it everywhere i just could!

I did

 

dout[strlen(dout)] = '\0';
din[strlen(din)] = '\0';
ain[strlen(ain)] = '\0'
result[strlen(result)] = '\0'

 But nothing helpes. I just can't understand why it is so difficult to JUST MAKE A STRING WITH TEXT in C(((( 

 

Please, help me, guys, because i'm completely tired and pissed off(

 

 

 

0 Kudos
Message 1 of 4
(4,378 Views)

Debug initializes your variables with 0 (zero) while release doesn't.

So your uninitialized strings contain the garbage you see.

-----------------------
/* Nothing past this point should fail if the code is working as intended */
0 Kudos
Message 2 of 4
(4,359 Views)
Solution
Accepted by topic author rovnyart

Variables local to a function are not initialized in compiled execution, while may be initialized to 0 or empty string in debug builds.

I suppose the solution to your problem is simpy to add strcpy (variable, ""); at the beginning of the function, just to ensure no garbage is in the strings before strcat-ing any values to them.



Proud to use LW/CVI from 3.1 on.

My contributions to the Developer Community
________________________________________
If I have helped you, why not giving me a kudos?
0 Kudos
Message 3 of 4
(4,356 Views)

I suppose the solution to your problem is simpy to add strcpy (variable, ""); at the beginning of the function, just to ensure no garbage is in the strings before strcat-ing any values to them.

 

 

You saved my life! Thank you very much, it helped!)))

 

0 Kudos
Message 4 of 4
(4,347 Views)