我一直在努力,現在調試這一個小時,但我失敗了。我有一個可變參數函數,set_buffer,這需要在緩衝區設置輸入字符串轉換爲分段故障與可變參數函數
void set_buffer(char *buf, int num_str, ...) { // destructively sets buffer with the strings provided, in the order they are provided. Must provide number of arguments so function knows where to stop.
size_t length = sizeof(buf)/sizeof(*buf); // strlen() fails because it depends on the null terminator...
printf("length: %d\n", length);
va_list args;
va_start(args, num_str); // va_start takes on the NAME of the last known parameter in the function to determine where to start taking in optional arguments
for (int i = 0, offset = 0; i < num_str && offset < length; ++i) {
char *str = va_arg(args, char *);
printf("length of str: %d\n", strlen(str)); // SEG FAULT ERROR
offset += snprintf(buf+offset, strlen(str)+1, "%s", str); // I must be adding too much of an offset, resulting in a seg fault
if (i != (num_str - 1)) {
//offset -= 1;
}
}
va_end(args);
}
我懷疑它可能有一些做的,從所使用的va_list ARGS參數不正確的抓取。
究竟什麼是你的問題,你爲什麼不提供再現問題的完整最低工作的代碼示例? – 2014-03-01 00:34:43