我试图在OSX和Linux的ubuntu运行在终端的代码:C的误差
#include <pthread.h>
#include <stdio.h>
#include <unistd.h>
int fact=1; //this data is shared by thread(s)
int n;
int x;
int *arrayName;
int main(int argc, char *argv[])
{
if (argc != 3){ //check number of arguments
printf("Must use: a.out <integer value>\n");
return -1;
}
int x = atoi(argv[1]);
n = atoi(argv[2]);
if (n < 0){ //check second passed argument
printf("%d Must be >=0\n", atoi(argv[2]));
return -1;
}
arrayName = (int *) malloc(n * sizeof(int));
pthread_t tid[n];
for(int i=0;i<n;i++){
pthread_create(&tid[i], NULL, (void *) i, NULL);
}
int i=0;
while(i<n){
pthread_join(tid[i],NULL);
i++;
}
i=0;
while (i<n) {
printf("Thread is %d",arrayName[i]);
i++;
}
}
void *calculateMult(void *i) {
int j = (int) i;
arrayName[j] = x * j;
return NULL;
};
我跑在终端这些命令:
立方厘米-pthread main.c中
./a.out 1
但是它给了我段错误:11在osx 和段错误(核心转储)在linux, 为什么?
'if(argc!= 3)'ehhh?为_a.out <整数值> _ ?? –
[请参阅此讨论,为什么不在'C'中投射'malloc()'和家族的返回值。](http://stackoverflow.com/q/605845/2173917)。 –
检查参数的个数是否大于3。 –