I am concatenating several strings. When running the program it gives me a Debug Assertion Failed! message which says Expression: (L”String is not null terminated” && 0).
All strings seem to be NULL terminated, but I am not sure what else could it be. Any ideas?
I am including the code excerpt where it triggers the problem.
void foo(){
....
char adj_command_string[COMMAND_SIZE];
reconstructCommand(&command_parameters, adj_command_string);
....
}
void reconstructCommand(command* command_data, char* adjusted_string){
char partial_string[COMMAND_SIZE];
char string_rep[COMMAND_SIZE];
if (command_data->g_code.new_value){
sprintf_s(string_rep, COMMAND_SIZE, "G%d ", command_data->g_code.value);
strcat_s(partial_string, COMMAND_SIZE, string_rep);
}
// If position changed, include change in command.
if (command_data->pos.X.value){
sprintf_s(string_rep, COMMAND_SIZE, "X%.4f ", command_data->pos.X.value);
strcat_s(partial_string, COMMAND_SIZE, string_rep);
}
if (command_data->pos.Y.value){
sprintf_s(string_rep, COMMAND_SIZE, "Y%.4f ", command_data->pos.Y.value);
strcat_s(partial_string, COMMAND_SIZE, string_rep);
}
if (command_data->pos.Z.value){
sprintf_s(string_rep, COMMAND_SIZE, "Z%.4f ", command_data->pos.Z.value);
strcat_s(partial_string, COMMAND_SIZE, string_rep);
}
if (command_data->pos.A.value){
sprintf_s(string_rep, COMMAND_SIZE, "A%.4f ", command_data->pos.A.value);
strcat_s(partial_string, COMMAND_SIZE, string_rep);
}
// If Feedrate originally included, include it in modified command
if (command_data->feedrate.new_value){
sprintf_s(string_rep, COMMAND_SIZE, "F%.4f ", command_data->feedrate.value);
strcat_s(partial_string, COMMAND_SIZE, string_rep);
}
// If spindle speed originally included, include it in modified command
if (command_data->speed.new_value){
sprintf_s(string_rep, COMMAND_SIZE, "S%.4f ", command_data->speed.value);
strcat_s(partial_string, COMMAND_SIZE, string_rep);
}
adjusted_string = partial_string;
}
I am not sure if this is the source of the problem but:
Will not copy the contents of
partial_stringintoadjusted_string. Usestrcpy()or some other similar mechanism and ensure thatadjusted_stringis null terminated.