我有以下C代碼,其給出了一個錯誤:C代碼停止運行
Program stopped at 0x4019b3.
It stopped with signal SIGSEGV, Segmentation fault.
調試時。
下面是代碼:
#include <stdio.h>
#include <complex.h>
#include <stdlib.h>
#include <time.h>
int main()
{
clock_t begin, end;
double time_spent;
begin = clock();
int n = 100;int i; int j;
int N = 64;int r;
double complex (s)[4] = {-1-1*I, -1+1*I, 1-1*I, 1+1*I};
double complex symbol[n][N];
for (i=0; i<n; i++){
for (j=0; j<N; j++){
r = rand() % 4;
symbol[i][j]=s[r];
}
// Now add pilots:
symbol[i][11] = 1;
symbol[i][22] = 1;
symbol[i][33] = 1;
symbol[i][44] = 1;
}
end = clock();
time_spent = (double)(end - begin)/CLOCKS_PER_SEC;
return 0;
}
任何想法有什麼不對?
編輯:
現在,我可以把這些有價值的討論後,把它放在一起。下面是時間和內存分配和每一件事情的工作代碼:
#include <stdio.h>
#include <complex.h>
#include <stdlib.h>
#include <time.h>
int main()
{
clock_t begin, end;
double time_spent;
begin = clock();
int n = 100000; int i; int j;
int N = 64;int r;
double complex (s)[4] = {-1-1*I, -1+1*I, 1-1*I, 1+1*I};
double complex (*symbol)[N] = malloc(n * sizeof *symbol);
for (i=0; i<n; i++){
for (j=0; j<N; j++){
r = rand() % 4;
symbol[i][j]=s[r];
}
// Now add pilots:
symbol[i][11] = 1;
symbol[i][22] = 1;
symbol[i][33] = 1;
symbol[i][44] = 1;
}
end = clock();
time_spent = (double)(end - begin)/CLOCKS_PER_SEC;
printf("%3.7f\n",time_spent);
return 0;
}
你做了什麼 - 如果有的話 - 試圖找出這個問題? – 2014-09-29 02:28:50
你需要在某處分配內存 – almanegra 2014-09-29 02:31:45
這行是錯誤的:'複數(*符號)[n] [N] = malloc(n * sizeof *符號);'。更仔細地從R Sahu的回答中複製出來。你也到處都冒出了虛假的'*'。它應該是'symbol [i] [j]',而不是'* symbol [i] [j]',對於所有的飛行員來說都是一樣的。 – 2014-09-29 02:58:51