Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/61.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
从阵列读取SEGFULT(可能与malloc/realloc相关)_C_Arrays_Memory_Malloc_Realloc - Fatal编程技术网

从阵列读取SEGFULT(可能与malloc/realloc相关)

从阵列读取SEGFULT(可能与malloc/realloc相关),c,arrays,memory,malloc,realloc,C,Arrays,Memory,Malloc,Realloc,我只是试图从一个文件中读取一组双精度数组(其中每行有三个双精度数组,但我不知道事先会有多少行,所以我尝试动态分配数组jm->vertices.jm->vertices是一个(双精度**) 以下是代码(基本obj模型文件解析器): jm->texts=malloc(sizeof(double*); jm->顶点[0]=malloc(sizeof(double)*3); while(strcmp(theS,“v”)==0){ /*顶点在二维数组中存储顶点的x y z*/ 如果(i!=0){ /*试图

我只是试图从一个文件中读取一组双精度数组(其中每行有三个双精度数组,但我不知道事先会有多少行,所以我尝试动态分配数组jm->vertices.jm->vertices是一个(双精度**)

以下是代码(基本obj模型文件解析器):

jm->texts=malloc(sizeof(double*);
jm->顶点[0]=malloc(sizeof(double)*3);
while(strcmp(theS,“v”)==0){
/*顶点在二维数组中存储顶点的x y z*/
如果(i!=0){
/*试图重新定位*/
如果((jm->顶点=(双**)realloc(jm->顶点,sizeof(*jm->顶点)*i+1))==NULL){
fprintf(stderr,“分配内存时出错。正在退出\n”);
出口(1);
}
jm->顶点[i]=malloc(sizeof(double)*3);
}
printf(“%s\n”,theS);
如果(fscanf(fp,“%lf”,&jm->顶点[i][0])!=1{fprintf(stderr,“错误:没有足够的顶点”);退出(0);}
如果(fscanf(fp,“%lf”,&jm->texts[i][1])!=1{fprintf(stderr,“错误:没有足够的顶点”);退出(0);}
如果(fscanf(fp,“%lf”,&jm->顶点[i][2])!=1{fprintf(stderr,“错误:没有足够的顶点”);退出(0);}
/*因为某种原因,我可以在这里打印*/
printf(“#::%d//%.8lf%.8lf%.8lf\n”,i+1,jm->vertices[i][0],jm->vertices[i][1],jm->vertices[i][2]);
theS[0]='\0';
fscanf(fp,“%s”,theS);
如果(theS[0]='#'){
评论=1;
而(theS[0]='#'){
theS[0]='\0';
fgets(theS,70,fp);
theS[0]='\0';
fscanf(fp,“%s”,theS);
}
打破
}
if(strcmp(theS,“vn”)==0{break;}
48,0-1        11%
if(strcmp(theS,“g”)==0){
theS[0]='\0';
fscanf(fp,“%s”,theS);
theS[0]='\0';
fscanf(fp,“%s”,theS);
theS[0]='\0';
fscanf(fp,“%s”,theS);
theS[0]='\0';
fscanf(fp,“%s”,theS);
打破
}
如果(注释==1){break;}
i++;
jm->nvert++;
}
i=0;
/*这段代码有故障*/////////////////////////////////////////////////
对于(i=0;倒-1;i++){
printf(“%.8lf%.8lf%.8lf”,jm->顶点[i][0],jm->顶点[i][1],jm->顶点[i][2]);
}
//////////////////////????////////////////////////////////////

有人能告诉我为什么没有保留内存吗?

在调用realloc时:
sizeof(*jm->vertices)*i+1
应该是
sizeof(*jm->vertices)*(i+1)


您的代码重新分配了一个额外的字节。通过此更改,它为
double*

分配了足够的空间。您是否将jm->nvert初始化为0?是的,jm->nvert已初始化。
jm->vertices = malloc(sizeof(double *));
jm->vertices[0] = malloc(sizeof(double) * 3);
while(strcmp(theS,"v") == 0){

/*vertices stores the x y z of vertices in a 2d array*/
    if(i != 0){
        /*TRYING TO REALLOC*/
        if((jm->vertices = (double **)realloc(jm->vertices, sizeof(*jm->vertices) * i+1)) == NULL){
            fprintf(stderr,"Error allocating memory. Exitting\n");
            exit(1);
        }

        jm->vertices[i] = malloc(sizeof(double) *3);
    }

    printf("%s\n",theS);

    if(fscanf(fp, "%lf", &jm->vertices[i][0]) != 1){fprintf(stderr, "Error: Not enough vertices"); exit(0);}

    if(fscanf(fp, "%lf", &jm->vertices[i][1]) != 1){fprintf(stderr, "Error: Not enough vertices"); exit(0);}

    if(fscanf(fp, "%lf", &jm->vertices[i][2]) != 1){fprintf(stderr, "Error: Not enough vertices"); exit(0);}

    /*CAN PRINT HERE FOR SOME REASON*/
    printf("#:%d // %.8lf %.8lf %.8lf\n", i+1,jm->vertices[i][0], jm->vertices[i][1], jm->vertices[i][2]);

    theS[0] = '\0';

    fscanf(fp, "%s", theS);


    if(theS[0] == '#'){
        comment =1;
        while(theS[0] == '#'){
            theS[0] = '\0';
            fgets(theS, 70, fp);
            theS[0] = '\0';
            fscanf(fp, "%s", theS);
        }
        break;
    }

    if(strcmp(theS, "vn") == 0){break;}
                                                                                                                                  48,0-1        11%
    if(strcmp(theS, "g") == 0){
        theS[0] = '\0';
        fscanf(fp, "%s", theS);
        theS[0] = '\0';
        fscanf(fp, "%s", theS);
        theS[0] = '\0';
        fscanf(fp, "%s", theS);
        theS[0] = '\0';
        fscanf(fp, "%s", theS);
        break;
    }
    if(comment == 1){break;}
    i++;

    jm->nvert++;
}
i=0;

/*THIS CODE SEGFAULTS*/////////////////////////////////////////////////
for(i=0; i<jm->nvert-1; i++){
    printf("%.8lf %.8lf %.8lf", jm->vertices[i][0], jm->vertices[i][1], jm->vertices[i][2]);
}
//////////////////////????////////////////////////////////////