【发布时间】:2013-04-08 01:17:40
【问题描述】:
我在 pthread 内的以下代码块上收到“Segmentation Fault 11”:
void *func(void *len){
char *temp = len;
size = (int) strtol(temp, (char **)NULL, 10); // this throws the segfault
}
pthread create的写法如下:
int main(int argc, const char *argv[]){
pthread_t t0;
const char * length = argv[1];
pthread_create(&t0, NULL, &func, (void *)length);
// rest of code
...
}
让我感到困惑的是,SegFault 在编译并在终端中运行时被抛出,而不是在 Xcode 中。知道为什么会抛出它吗?
【问题讨论】:
-
你是否也在从其他线程调用函数 func ?
-
在将
length传递给pthread_create()之后,您如何使用它?size是什么? -
在将
temp传递给strtol()之前,您不测试temp与NULL的不同之处。您也可以使用argc来测试argv[1]是否包含任何有效的引用。
标签: c xcode terminal segmentation-fault pthreads