01-22-2015 01:06 AM
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(
Solved! Go to Solution.
01-22-2015 05:41 AM - edited 01-22-2015 05:42 AM
Debug initializes your variables with 0 (zero) while release doesn't.
So your uninitialized strings contain the garbage you see.
01-22-2015 05:44 AM
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.
01-22-2015 07:00 AM
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!)))