C 两个矩阵大小超过800*800时的分段错误
我正在尝试编写一个超级简单的C程序,用于整数数据类型的向量乘加“axpy”算法。程序输出执行时间以测量机器的性能。矩阵由随机数填充C 两个矩阵大小超过800*800时的分段错误,c,arrays,segmentation-fault,C,Arrays,Segmentation Fault,我正在尝试编写一个超级简单的C程序,用于整数数据类型的向量乘加“axpy”算法。程序输出执行时间以测量机器的性能。矩阵由随机数填充 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 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);
}
int基准测试(无效){
int N;/*矩阵大小,由用户输入控制*/
int r,c;/*行和列编号*/
int random;/*填充矩阵的随机数*/
int a=rand()%20;/*将数值乘以x矩阵*/
printf(“输入矩阵的大小(N*N)(最大1000000)\N”);
scanf(“%d”和“&N”);
如果(N>1000000){
fprintf(stderr,“矩阵大小太大!\n”);
返回0;
}
/*初始化并填充矩阵x和y*/
int-xMatrix[N][N],yMatrix[N][N],resultMatrix[N][N];
/*计算时间*/
时钟;
t=时钟();
对于(r=0;r总时间:%f秒\n”,所用时间);
printf(“\n->向量长度:%d”,n*n);
}
用户控制矩阵的大小。
当
N
的值小于800
时,程序运行良好,我建议使用mallocfromstdlib.h如下图所示
此外,请查看以下SO帖子:,以及
#包括
#包括
#包括
int基准(void){
int N;/*矩阵大小,由用户输入控制*/
int r,c;/*行和列编号*/
int random;/*填充矩阵的随机数*/
int a=rand()%20;/*将数值乘以x矩阵*/
printf(“输入矩阵的大小(N*N)(最大1000000)\N”);
scanf(“%d”和“&N”);
如果(N>1000000){
fprintf(stderr,“矩阵大小太大!\n”);
返回0;
}
/*初始化并填充matix和y*/
int**xMatrix=NULL;
int**yMatrix=NULL;
int**resultMatrix=NULL;
/*使用堆内存分配而不是堆栈*/
xMatrix=(int**)malloc(N*sizeof(int*);
y矩阵=(int**)malloc(N*sizeof(int*);
结果矩阵=(int**)malloc(N*sizeof(int*);
对于(r=0;r总时间:%f秒\n”,所用时间);
printf(“\n->向量长度:%d”,n*n);
/*始终记得释放分配的内存*/
对于(r=0;r
如果您试图动态分配内存,我建议使用stdlib.h中的malloc,如下所示
此外,请查看以下SO帖子:,以及
#包括
#包括
#包括
int基准(void){
int N;/*矩阵大小,由用户输入控制*/
int r,c;/*行和列编号*/
int random;/*填充矩阵的随机数*/
int a=rand()%20;/*将数值乘以x矩阵*/
printf(“输入矩阵的大小(N*N)(最大1000000)\N”);
scanf(“%d”和“&N”);
如果(N>1000000){
fprintf(stderr,“矩阵大小太大!\n”);
返回0;
}
/*初始化并填充matix和y*/
int**xMatrix=NULL;
int**yMatrix=NULL;
int**resultMatrix=NULL;
/*使用堆内存分配而不是堆栈*/
xMatrix=(int**)malloc(N*sizeof(int*);
y矩阵=(int**)malloc(N*sizeof(int*);
结果矩阵=(int**)malloc(N*sizeof(int*);
对于(r=0;r总时间:%f秒\n”,所用时间);
printf
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
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 matrixs(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 matix x and y */
int** xMatrix = NULL;
int** yMatrix = NULL;
int** resultMatrix = NULL;
/* Using the heap memory allocation instead of the stack */
xMatrix = (int **) malloc(N * sizeof(int *));
yMatrix = (int **) malloc(N * sizeof(int *));
resultMatrix = (int **) malloc(N * sizeof(int *));
for (r = 0; r < N; r++) {
xMatrix[r] = (int *) malloc(N * sizeof(int));
yMatrix[r] = (int *) malloc(N * sizeof(int));
resultMatrix[r] = (int *) malloc(N * sizeof(int));
}
/* 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 matix 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);
/* Always remember to free your allocated memory */
for (r = 0; r < N; r++) {
free(xMatrix[r]);
free(yMatrix[r]);
free(resultMatrix[r]);
}
free(xMatrix);
free(yMatrix);
free(resultMatrix);
}
int main() {
benchmark();
return 0;
}
/* 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));
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;
}