由于某些原因,以下内容不起作用:
int i;
for(i = 1; i < argc; i++) // Create thread for each dataset.
{
filename = strcat(argv[i], ".sdx"); // Concatenate file-extension '.sdx' to basename.
pthread_attr_init(&attr); // Set the attribute of the thread (default).
pthread_create(&tid[i], &attr, start_routine, filename); // Create thread.
pthread_join(tid[i],NULL); // Join thread after it completed.
}如果我只传入一个文件,那么它就能工作,但不止一个文件会产生分段错误。我不明白,如果我没有连接文件扩展名,而是将完整的文件名(包括扩展名)作为命令行参数传递,那么一切都会正常工作。
发布于 2014-06-19 02:01:43
您不应该直接修改argv[i]。将其复制到本地缓冲区。
int i;
for(i = 1; i < argc; ++i)
{
char *filename = malloc(strlen(argv[i]) + 4 + 1);
sprintf(filename, "%s.sdx", argv[i]);
pthread_attr_init(&attr); // Set the attribute of the thread (default).
pthread_create(&tid[i], &attr, start_routine, filename); // Create thread.
pthread_join(tid[i],NULL); // Join thread after it completed.
free(filename);
}https://stackoverflow.com/questions/24297610
复制相似问题