我正在连接几个字符串。运行程序时,它给我一个调试断言失败!表示表达式的消息:(L“字符串不是空终止的”&& 0)。
所有字符串似乎都以 NULL 结尾,但我不确定它还能是什么。有什么想法吗?
我在触发问题的地方包括了代码摘录。
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;
}
最佳答案
您试图在字符串中放置比 COMMAND_SIZE
允许的更多的字符,没有为终止零留出空间。 COMMAND_SIZE
必须至少比字符串可以达到的最大长度大 1,否则将没有空间来终止字符串。
还有:
adjusted_string = partial_string;
这并不像您认为的那样。它将 adjusted_string
的值更改为指向 partial_string
,但随后通过返回丢弃该值。您可能需要调用 strcpy_s
来复制结果。
关于c - 调试警告 : String is not null terminated. 知道它是什么吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8333000/