核心转储尝试在结构中创建newVector(array 2D)

时间:2018-12-19 12:37:29

标签: c segmentation-fault

因此,我试图在结构内创建一个简单的2D数组,并读取文件中某些坐标的值。

此刻,我已经拥有所有内部函数,并且在尝试获取'get_coords'函数的坐标时遇到了分段错误。如果我在'int main'上编写所有完全相同的代码,而不是使用函数,那么它将起作用。

代码:

            #include <stdio.h>
            #include <stdlib.h>

            // structure
            typedef struct coordinates{
                double **a;
            } coord;

            // counts the nmbr of points 
            int count(){
                int i;
                int a1, a2, a3;
                FILE *fp;
                fp = fopen("abc.txt", "r+");
                while (fscanf(fp, "%d %d %d", &a1, &a2, &a3) != EOF){
                    i++;
                    if (feof(fp)){
                        break;          
                    }   
                }
                fclose(fp);
                return(i);
            }

            // creates new structure with the right size of memory allocated
            coord *newVector(size_t s){
                coord *v;
                int j;
                v = malloc(sizeof(coord));

                v->a = malloc(sizeof(double*)*3);

                for (j=0; j<3; j++){
                    v->a[j] = malloc(sizeof(double)*s);
                }

            }

            void get_coords(coord *points){
                int i=0;
                FILE *fp;
                fp = fopen("abc.txt", "r+");
                while (fscanf(fp, "%le %le %le", &points->a[i][0], &points->a[i][1], &points->a[i][2]) != EOF){
                    i++;
                }
                fclose(fp);
            }


            int main(){
                int i = 0, j=0;

                coord *points;

                i = count();

                points = newVector(i);

                get_coords(points);

                for (i=0; i<3; i++){
                        printf("%lf %lf %lf\n", points->a[i][0], points->a[i][1], points->a[i][2]);
                }
            }

abc.txt:

1 2 3
4 5 6
7 8 9

感谢您的帮助。

球, 迪伦。

PS:这只是我想要的原型。

2 个答案:

答案 0 :(得分:3)

count()中,您将递增一个未初始化的变量,从而调用未定义行为(UB)。

更改此:

int count() {
  int i;

对此:

int count() {
  int i = 0;

newVector() 返回动态分配的内存。

更改此:

coord *newVector(size_t s) {
  ...
}

对此:

coord *newVector(size_t s) {
  ...
  return v;
}

解决这些问题后,您应该看到以下输出:

1.000000 2.000000 3.000000
4.000000 5.000000 6.000000
7.000000 8.000000 9.000000

不是问题,但是我将使用%lf作为fscanf()中的格式说明符,而不是%le

此外,在count()中,此return(i);与此return i;相同。括号是多余的。

TODO:释放动态分配的内存(我假设您跳过了提供MCVE的那一部分)。


专业提示:在下次启用警告标志的情况下进行编译。在这种情况下,他们已经为您找到了问题。例如,在海湾合作委员会,我会得到这个:

gsamaras@myPc:~$ gcc -Wall  main.c
main.c: In function ‘main’:
main.c:51:28: warning: unused variable ‘j’ [-Wunused-variable]
                 int i = 0, j=0;
                            ^
main.c: In function ‘newVector’:
main.c:37:13: warning: control reaches end of non-void function [-Wreturn-type]
             }
             ^

最后一个警告是我回答的第二点。

答案 1 :(得分:0)

您忘了在v函数中返回newVector

        // creates new structure with the right size of memory allocated
        coord *newVector(size_t s){
            coord *v;
            int j;
            v = malloc(sizeof(coord));

            v->a = malloc(sizeof(double*)*3);

            for (j=0; j<3; j++){
                v->a[j] = malloc(sizeof(double)*s);
            }

            return v;

        }
相关问题