This question already has answers here:
Closed 2 years ago.
Segmentation fault on large array sizes
(5个答案)
我试图为整数数据类型编写一个向量乘加“axpy”算法的超级简单C程序。程序输出执行时间以测量机器的性能。矩阵由随机数填充。
用户控制矩阵的大小。
当
并验证
下面是修改后的代码:
但是请注意,您的计算非常简单,大部分时间可能都花在
(5个答案)
我试图为整数数据类型编写一个向量乘加“axpy”算法的超级简单C程序。程序输出执行时间以测量机器的性能。矩阵由随机数填充。
int benchmark(void) {
int N; /* The matrix size, controlled by user input */
int r, c; /* Row and Column number */
int random; /* Random number to fill the matix */
int a = rand() % 20; /* Scale number to multiply x matrix */
printf("Enter the size(N*N) of the matrices(Maximum 1,000,000)\n");
scanf("%d", &N);
if (N > 1000000) {
fprintf(stderr, "Size of matrix is too large!\n");
return 0;
}
/* Initialize and fill the matrix x and y */
int xMatrix[N][N], yMatrix[N][N], resultMatrix[N][N];
/* Compute time */
clock_t t;
t = clock();
for (r = 0; r < N; r++) {
for (c = 0; c < N; c++) {
random = rand() % 100;
xMatrix[r][c] = a * random; /* Multiply matrix x with random value a */
}
}
for (r = 0; r < N; r++) {
for (c = 0; c < N; c++) {
int random = rand() % 100;
yMatrix[r][c] = random;
}
}
/* Add two matrix together */
for (r = 0; r < N; r++) {
for (c = 0; c < N; c++) {
resultMatrix[r][c] = xMatrix[r][c] + yMatrix[r][c];
}
}
t = clock() - t;
double timeTaken = ((double)t) / CLOCKS_PER_SEC;
printf("\n -> Total time : %f seconds\n", timeTaken);
printf("\n -> Vector length : %d", N * N);
}
用户控制矩阵的大小。
当
N
的值小于800
时,程序运行良好。 最佳答案
使用自动存储(在堆栈上)分配的对象太大,会出现未定义的行为,更确切地说是堆栈溢出。
相反,您应该从堆中分配对象:
/* Initialize and fill the matix x and y */
int (*xMatrix)[N] = malloc(N * sizeof(*xMatrix));
int (*yMatrix)[N] = malloc(N * sizeof(*yMatrix));
int (*resultMatrix)[N] = malloc(N * sizeof(*resultMatrix));
并验证
malloc()
返回的指针都不是NULL
。下面是修改后的代码:
int benchmark(void) {
int N; /* The matrix size, controlled by user input */
int r, c; /* Row and Column number */
int random; /* Random number to fill the matix */
int a = rand() % 20; /* Scale number to multiply x matrix */
printf("Enter the size(N*N) of the matrices (Maximum 1,000,000)\n");
if (scanf("%d", &N) != 1) {
fprintf(stderr, "Input error!\n");
return 0;
}
if (N > 1000000) {
fprintf(stderr, "Matrix size is too large!\n");
return 0;
}
/* Initialize and fill the matrix x and y */
int (*xMatrix)[N] = malloc(N * sizeof(*xMatrix));
int (*yMatrix)[N] = malloc(N * sizeof(*yMatrix));
int (*resultMatrix)[N] = malloc(N * sizeof(*resultMatrix));
if (xMatrix == NULL || yMatrix == NULL || resultMatrix == NULL) {
fprintf(stderr, "Memory allocation failed!\n");
free(xMatrix);
free(yMatrix);
free(resultMatrix);
return 0;
}
/* Compute time */
clock_t t = clock();
for (r = 0; r < N; r++) {
for (c = 0; c < N; c++) {
random = rand() % 100;
xMatrix[r][c] = a * random; /* Multiply matrix x with random value a */
}
}
for (r = 0; r < N; r++) {
for (c = 0; c < N; c++) {
random = rand() % 100;
yMatrix[r][c] = random;
}
}
/* Add two matrix together */
for (r = 0; r < N; r++) {
for (c = 0; c < N; c++) {
resultMatrix[r][c] = xMatrix[r][c] + yMatrix[r][c];
}
}
t = clock() - t;
double timeTaken = ((double)t) / CLOCKS_PER_SEC;
printf("\n -> Total time : %f seconds\n", timeTaken);
printf("\n -> Vector length : %lld", (long long)N * N);
free(xMatrix);
free(yMatrix);
free(resultMatrix);
return 0;
}
但是请注意,您的计算非常简单,大部分时间可能都花在
rand()
函数中。关于c - 两个矩阵大小超过800 * 800时出现段错误,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/46616044/
10-10 08:03