[英]Core dumped/ Segmentation fault when running the code
When I run this I get a warning regarding returning the local address, which is reasonable since all these functions will return a local address.当我运行它时,我收到有关返回本地地址的警告,这是合理的,因为所有这些函数都将返回本地地址。 However, I need the functions to return a matrix since I need modulation in my code.
但是,我需要这些函数来返回一个矩阵,因为我需要在我的代码中进行调制。 The major issue is that I get a core dumped/ segmentation fault error when running this on my PC.
主要问题是在我的 PC 上运行它时出现核心转储/分段错误错误。
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#define N 10
float *A[N], *B[N], *C[N];
int i, j, k;
float** matrix_create(int n){
float* M[n];
for (i = 0; i < n; i++)
M[i] = (float*)malloc(n * sizeof(float));
return M;
}
float** add(float* M1[], float* M2[], int n){
float** M3 = matrix_create(n);
for (i = 0; i < n; i++)
for (j = 0; j < n; j++)
M3[i][j] = M1[i][j] + M2[i][j];
return M3;
}
float** sub(float* M1[], float* M2[], int n){
float** M3 = matrix_create(n);
for (i = 0; i < n; i++)
for (j = 0; j < n; j++)
M3[i][j] = M1[i][j] - M2[i][j];
return M3;
}
void print(float* M[], int n){
for (i = 0; i < n; i++){
for (j = 0; j < n; j++)
printf("%f\t", M[i][j] );
printf("\n");
}
}
int main(){
int count = 0, i,j, k;
for (i = 0; i < N; i++)
A[i] = (float*)malloc(N * sizeof(float));
for (i = 0; i < N; i++)
B[i] = (float*)malloc(N * sizeof(float));
for (i = 0; i < N; i++)
C[i] = (float*)malloc(N * sizeof(float));
for (i = 0; i < N; i++)
for (j = 0; j < N; j++){
A[i][j] = ++count;
B[i][j] = count;
}
float** D = add(A, B, N);
print(D, N);
}
Heyo,嘿嘿,
The segmentation fault happens as a result of what the compiler is warning you about!分段错误是编译器警告您的结果! In the
matrix_create
function, you create a local array of float*
.在
matrix_create
function 中,您创建一个float*
的本地数组。 In c, when you return an array, it will only return the address of the first element in the array.在 c 中,当你返回一个数组时,它只会返回数组中第一个元素的地址。 When the function returns, the stack shrinks, and this memory is no longer part of your program.
当 function 返回时,堆栈收缩,并且此 memory 不再是您程序的一部分。 When you use the
[]
operator, you're dereferencing the pointer to the address that matrix_create
returns.当您使用
[]
运算符时,您将取消引用指向matrix_create
返回的地址的指针。 Since this memory doesn't belong to your program anymore, you get a segmentation fault.由于此 memory 不再属于您的程序,因此您会遇到分段错误。 In order to solve it, you should also allocate the required memory for the
float*
on the heap with malloc
, such that you can keep using the memory after the function returns: In order to solve it, you should also allocate the required memory for the
float*
on the heap with malloc
, such that you can keep using the memory after the function returns:
float** matrix_create(int n){
float** M = malloc(n * sizeof(float*));
for (i = 0; i < n; i++)
M[i] = (float*)malloc(n * sizeof(float));
return M;
}
Keep in mind that you shouldn't just allocate memory on the heap, you should also free it after you no longer need it!请记住,您不应该只在堆上分配 memory,还应该在不再需要它之后释放它!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.