【发布时间】:2014-10-28 18:22:40
【问题描述】:
我正在使用以下函数维护一些丑陋的遗留代码,我得到了
warning: value computed is not used
对于下面由 cmets 标记的行:
void ReadKeyValuePipe(char* buffer, char* key, char* value) {
char* pos;
char key_str[1024];
char* val = value;
sprintf(key_str,"%s:",key);
if((pos = strstr(buffer,key))) {
pos += strlen(key_str);
while (*pos && *pos != '|') {
*val = *pos;
*val++; // this is actually used
*pos++; // so is this
}
*val = 0;
}
}
当我删除这些行时,代码会中断。这是有道理的,因为它们似乎是递增的标记。
我如何让编译器识别出这些计算实际上被使用了?
【问题讨论】:
-
它们没有被使用 - 查看链接的副本。
-
我会更关心调用
sprintf导致的潜在缓冲区溢出。至少,请输入assert(strlen(key) + 2 <= sizeof(key_str)); -
@Casey 这整个函数对我来说似乎是错误的做法,但我必须选择我的战斗。
-
WTF?
key_str仅用于计算1 + strlen(key)的效率低下?那应该是strstr(buffer, key_str)吗?这个功能实在是太可怕了。 -
说真的。如果我要重写这个函数,我只需将任何
char*转换为string并使用string splitting 来完成。
标签: c++ c c++11 gcc-warning gcc4.7