- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在编写一个 gauss seidel 程序,目前在使用 malloc 时遇到问题。
请帮我解决这个问题。我还没有开始进行高斯赛德尔迭代,因为我被困在这里。
错误显示“段错误(核心已转储)”。我不知道这意味着什么。我尝试扫描代码多次,但就是找不到错误。
我使用 FSCANF 的方式可能有问题吗?
#include <stdio.h>
#include <stdlib.h>
//structure declarations
struct table2 {
int k;
float value;
int row;
int col;
int nextK_row;
int nextK_col;
};
struct table3 {
int index;
int fir;
int fic;
};
//function prototypes
void allocate_memory(int num_unknowns, int num_entries, struct table2 *pTable2, struct table3 *pTable3, float *b, FILE* fInput, FILE* fOutput);
void free_close (FILE* fInput, FILE* fOutput, struct table2 *pTable2, struct table3 *pTable3, float *b);
int main (int argc, char* argv[]) {
//variable declarations
FILE *fInput=NULL;
FILE *fOutput=NULL;
struct table2 *pTable2=NULL;
struct table3 *pTable3=NULL;
int num_unknowns, num_entries;
float *b=NULL;
int i, j, m;
int count1, count2, count3, count4, l;
//check if arguments from user is 3, else error
if (argc!=3) {
printf("Error, the number of arguments should be exactly as needed(three).\n");
return 1;
}
//open input file
fInput = fopen(argv[1], "r");
//check if input file opened successfully
if (fInput == NULL) {
printf("Error. Input file wasn't opened successfully.\n");
return 1;
}
//open output file
fOutput = fopen(argv[2], "w");
//check if output file opened successfully
if (fOutput == NULL) {
printf("Error. Output file wasn't opened successfully.\n");
return 1;
}
//scan no. of unknowns from file and check if successful
count1 = fscanf(fInput, " %d", &num_unknowns);
if (count1 != 1) {
printf("Error, fscanf() did not read number of unknowns successfully.");
//call function to free memory and close files
free_close (fInput, fOutput, pTable2, pTable3, b);
return 1;
}
//scan no. of entries from file and check if successful
count2 = fscanf(fInput, " %d", &num_entries);
if (count2 != 1) {
printf("Error, fscanf() did not read number of entries successfully.");
//call function to free memory and close files
free_close (fInput, fOutput, pTable2, pTable3, b);
return 1;
}
//call function to allocate memory
allocate_memory(num_unknowns, num_entries, pTable2, pTable3, b, fInput, fOutput);
//loop to read values from Table2 and check if it read successfully
for(i=1; i<=num_entries; i++) {
count3 = fscanf(fInput, " %d %f %d %d %d %d", &(pTable2[i].k), &(pTable2[i].value), &(pTable2[i].row), &(pTable2[i]).col, &(pTable2[i].nextK_row), &(pTable2[i].nextK_col));
if (count3 != 6) {
printf("Error, fscanf() did not read table 2 values successfully.");
//call function to free memory and close files
free_close (fInput, fOutput, pTable2, pTable3, b);
return 1;
}
}
//loop to get values from Table3
for(j=1; j<=num_unknowns; j++) {
count4 = fscanf(fInput, " %d %d %d", &(pTable3[j].index), &(pTable3[j].fir), &(pTable3[j].fic));
if (count4 != 3) {
printf("Error, fscanf() did not read table3 values successfully.");
//call function to free memory and close files
free_close (fInput, fOutput, pTable2, pTable3, b);
return 1;
}
}
//loop to get constants from file
for(m=1; m<=num_unknowns ; m++) {
l = fscanf(fInput, " %f", &(b[m]));
if (l != 1) {
printf("Error, fscanf() did not read constant values successfully.");
//call function to free memory and close files
free_close (fInput, fOutput, pTable2, pTable3, b);
return 1;
}
}
//test print
for(i=1; i<=num_entries; i++) {
printf("%d\t", pTable2[i].k);
printf("%f\t", pTable2[i].value);
printf("%d\t", pTable2[i].row);
printf("%d\t", pTable2[i].col);
printf("%d\t", pTable2[i].nextK_row);
printf("%d\t", pTable2[i].nextK_col);
}
printf("\n\n\n");
for(j=1; j<=num_unknowns; j++) {
printf("%d\t", pTable3[j].index);
printf("%d\t", pTable3[j].fir);
printf("%d\t", pTable3[j].fic);
}
printf("\n\n\n");
for(m=1; m<=num_unknowns ; m++) {
printf("%f", b[m]);
}
printf("\n\n\n");
//call function to free memory and close files
free_close (fInput, fOutput, pTable2, pTable3, b);
return 0;
}
//function to allocate memory
void allocate_memory(int num_unknowns, int num_entries, struct table2* pTable2, struct table3* pTable3, float* b, FILE* fInput, FILE* fOutput) {
//allocate memory for table 2
pTable2 = (struct table2* ) malloc(24*num_entries);
if (pTable2 == NULL) {
printf("Error, memory allocation for table2 failed.");
//call function to free memory and close files
free_close (fInput, fOutput, pTable2, pTable3, b);
exit(-1);
}
//allocate memory for table 3
pTable3 = (struct table3* ) malloc(12*num_unknowns);
if (pTable3 == NULL) {
printf("Error, memory allocation for table3 failed.");
//call function to free memory and close files
free_close (fInput, fOutput, pTable2, pTable3, b);
exit(-1);
}
//allocate memory for constants
b = (float*) malloc(sizeof(float)*num_unknowns);
if (b == NULL) {
printf("Error, memory allocation for matrix B entries failed.");
//call function to free memory and close files
free_close (fInput, fOutput, pTable2, pTable3, b);
exit(-1);
}
return;
}
//function to free allocated memory and close files
void free_close (FILE* fInput, FILE* fOutput, struct table2 *pTable2, struct table3 *pTable3, float *b) {
if (fInput != NULL)
fclose(fInput);
if (fOutput != NULL)
fclose(fOutput);
if (pTable2 != NULL)
free(pTable2);
if (pTable3 != NULL)
free(pTable3);
if (b != NULL)
free(b);
return;
}
最佳答案
在此函数调用中:
allocate_memory(num_unknowns, num_entries, pTable2, pTable3, b, fInput, fOutput);
您按值传递所有变量。然后里面allocate_memory
您更改这些变量的本地副本。这些更改不会影响 main()
中的变量.
您的段错误可能来自于 ptable2[i]
等main()
因为pTable2
仍然是NULL
.
要解决此问题,请通过引用传递变量。然而,你的整个free_close
设置非常难看。我建议将所有相关的控制变量放入 struct
中;并有main()
调用一个包含其余代码的函数,并且 main()
可以在该函数结束后进行释放。
此外,您还应该学习如何找出发生段错误的位置,这是一项有用的技能。如果您没有设置调试器并且现在不想学习它,则可以通过在代码中插入输出语句(包括刷新)并运行程序并查看是否出现该输出来“调试”。这样,您就可以逐渐缩小问题范围,确定哪一行有问题。
关于c - 实现 malloc 时出现段错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22892205/
我是 C 的新手,在 Linux 中使用带有开关 gcc -g -std=c89 -Wall ... 的 gcc4.4.6 进行编程,我在许多函数深处遇到了这个错误我的程序名为 compute: **
今天阅读Rust subreddit时,我发现以下评论: jemalloc针对(多线程)速度而不是内存使用进行了优化 经过更多研究后,我发现还有更多选择(例如calloc)。 我想了解不同内存分配器的
相关代码: write(-1, "test", sizeof("test")); void * p = malloc(1024); void * p2 = malloc(510); w
我正在比较不同的 malloc 实现,我想比较它们的运行时间和内存使用情况。 特别是,我对运行时和最大常驻内存感兴趣。重要的是最大常驻内存将是真实的(没有代码段等)。 我不能使用像 valgrind
我承认这三个都有不同的含义。但是,我不明白这些具体情况适用于哪些特定情况。任何人都可以分享每个例子吗?谢谢。 malloc(sizeof(int)) malloc(size
GLib 文档推荐使用 GLib Slice Allocator 而不是 malloc: "For newly written code it is recommended to use the ne
我正在分配一个字符串 int main(){ int buf = 1024; char *input = malloc(sizeof(char*) * buf); //CODE
Here有一个关于 malloc 包的环境变量列表: MallocStackLogging MallocStackLoggingNoCompact MallocPreScribble MallocSc
总体问题:当您将通过malloc分配的返回值分配给一个指针时,您是否需要malloc该指针以及,还是您可以简单地声明并分配它? 例如,假设我有一个函数 foo,它在执行过程中使用 malloc 创建了
这个问题在这里已经有了答案: String assignment in C (4 个答案) 关闭 7 年前。 这是有问题的片段。 int main() { char** RESERV = (
任务是将一个二进制文件解析到内存中。但是,我事先不知道需要分配的内存量。 哪种方法更可取:在解析例程中进行多个小 malloc,或者首先遍历文件以确定所需的内存量,然后再次解析? 感谢任何提示。 最佳
我最近一直在尝试理解严格别名的一个特定方面,我认为我已经制作了尽可能最小的有趣代码。 (对我来说很有趣,就是这样!) 更新:根据到目前为止的答案,很明显我需要澄清这个问题。从某个角度来看,这里的第一个
我一直在为我创建的一个简单程序创建测试。我总是使用类似这样的方法检查使用 malloc 分配内存是否失败 int* ptr = malloc(sizeof(int) * x); if(!ptr){
我是 malloc 和对齐 malloc 的新手。我知道如何使用它们。但是,我不知道在什么情况下我们应该使用对齐的 malloc 而不是标准的 malloc。你能给我解释一下吗? 最佳答案 glibc
这样分配内存是不好的做法吗?: FOO *foo; while (!(foo = malloc(sizeof(FOO)))) ; 最佳答案 我不知道有什么不好的做法,但这种情况并不常见。 malloc
有人可以向我解释使用和不使用 malloc 创建结构之间的区别吗?什么时候应该使用 malloc,什么时候应该使用常规初始化? 例如: struct person { char* name;
假设我有一个类型 node_t typedef struct node{ char* value; struct node *next; }node_t; 当我想创建一个名为 n1 的
关闭。这个问题需要更多 focused .它目前不接受答案。 想改进这个问题?更新问题,使其仅关注一个问题 editing this post . 3年前关闭。 Improve this questi
我对指针感到困惑。这是交换两个名称的代码。请看代码。考虑输入:hellohai(对于 d)和 asd(对于 e)。我得到的输出:asd 1ellohai 1ellohai #include #incl
我已经编写了这个函数(如下)。它应该逐行读取文件。编辑该行并将某些单词/字符放入各种功能中。然后将这些函数放入“entrant”结构的数组(malloc)中。 问题是,当我退出循环并尝试打印数组时,放
我是一名优秀的程序员,十分优秀!