我正在尝试为C中的代码使用动态内存。我正在使用struct,但我做错了什么,但我不知道是什么。第一部分是我的header(.h)文件的一部分,在其中定义了两个结构。

第二部分是我的C(.c)文件的一部分,我在其中初始化该结构并使用它。我遇到段错误,而ddd告诉我该段。故障发生在注释后的行中:这是段。发生故障。
非常感激任何的帮助

======================================

第一部分

======================================

    #include <stdio.h>
    #include <string.h>
    #include <ctype.h>
    #include <stdlib.h>
    #include <math.h>

    void parseFile(FILE * fp, FILE * sketcher);
    void processArgument(char argument[]);
    void printOutput();
    #define MAX_WORD 256
    #define initial_size 17
    extern const char argument[];

    FILE* popen(const char*, const char*);

    int pclose(FILE*);

    struct pointxy {

        double x;
        double y;

    };

    struct figure{

        char figureName[MAX_WORD];
        struct pointxy vertices[17];
        int countPoints;

    };

    struct figure figurehere[17];
    struct figure newFigure[17];


======================================

第二部分

======================================

#include "draw2.h"
#include "draw2a.h"
#include "memwatch.h"

struct figure *figureHere;
void printOutput(){
    printf("./draw2 started on:");

    fflush(stdout);
    system("date\n");
}
/*send what ever there is after the child to sketchpad(in that specific line)*/
void child (char line[], char word[], char nameFigure[], FILE * sketcher){
    sscanf(line, "%s%s", word, nameFigure);
    fprintf (sketcher, "%s\n", &line[6]);
}

/*I construct the struct by reading from the Figure line to the end figure line.*/
struct figure* figureFunction (FILE * fp, char line[], char word[], char figureName[], int countNumberoffigures){
    double startx, starty;
    int temp = 1;
    sscanf(line, "%s%s%lf%lf%s", word, figureName, &startx, &starty, word);
    figureHere->vertices[0].x = startx;
    figureHere->vertices[0].y = starty;
    strcpy(figureHere->figureName, figureName);
    fgets(line, MAX_WORD - 1, fp);
    int nuRead = sscanf(line, "%s", word);
    int i = 1;
    while (strncmp(word, "End", MAX_WORD)!=0){
        if (strncmp(word, "#", MAX_WORD) == 0){
            printf("%s",line);
        }
        if (strncmp(word, "draw", MAX_WORD) == 0){
            sscanf (line, "%s%lf%lf", word, &startx, &starty);
            figureHere->vertices[i].x = figureHere->vertices[i-1].x + startx;
            figureHere->vertices[i].y = figureHere->vertices[i-1].y + starty;
            i += 1;
        }
        fgets(line, MAX_WORD - 1, fp);
        nuRead = sscanf(line, "%s", word);
    }
    figureHere->countPoints = i;
    return figureHere;
}


======================================

第三方

======================================

#include "draw2.h"
#include "draw2a.h"
#include "draw2b.h"
#include "memwatch.h"

struct figure myFigures[17];

struct figure **pointsAndname;

const char Exec_c[]  = "java -jar Sketchpad.jar";

void parseFile(FILE * fp, FILE *sketcher){
    char line [MAX_WORD], word [MAX_WORD], figureName [MAX_WORD];
    int countNumberoffigures;                                   //accounts to which figure in the array we are on
    printOutput();
    int temp = 1;
    countNumberoffigures = 0;
    while ( fgets(line, MAX_WORD - 1, fp) != NULL ){
        int nuRead = sscanf(line, "%s", word);
        if ( nuRead > 0 ){
            if(strncmp(word, "Figure", MAX_WORD)==0){           //1)reads the figure, name and the two starting points
                countNumberoffigures += 1;                      //accounts to which figure in the array we are on
                pointsAndname = malloc(sizeof(struct figure*)*temp);
                pointsAndname[countNumberoffigures -1] = figureFunction(fp,line, word, figureName, countNumberoffigures);
            }
            if(strncmp(word, "printFigure", MAX_WORD)==0){      //4)read the command printFigure, name of the figure
                printFigure(fp, line, countNumberoffigures);
            }
            if(strncmp(word, "drawFigure", MAX_WORD)==0){       //5)read the command drawFigure and the name of the figure
                drawFigure(sketcher, line, countNumberoffigures);
            }
            if(strncmp(word, "translate", MAX_WORD)==0){        //6)read the command translate
                translate(line, sketcher, countNumberoffigures);
            }
            if(strncmp(word, "child", MAX_WORD)==0){            //7)reads command child and the name of the figure
                child(line, word, figureName, sketcher);
            }
            if(strncmp(word, "#", MAX_WORD)==0){                //8)reads the whole line until the \n
                printf(line);
                //printf("ani po\n");
            }
            if(strncmp(word, "end", MAX_WORD)==0){
                fprintf (sketcher, "end\n");
                //printf("ani po\n");
            }
            if(strncmp(word, "rotate", MAX_WORD)==0){
                rotate(line, sketcher, countNumberoffigures);
            }
        }
    }
}

void processArgument(char argument[]){
    FILE *sketcher;
    FILE *fp;
    fp = fopen (argument, "r");
    sketcher = popen(Exec_c, "w");
    if (sketcher == NULL){
        printf ("Could not open pipe to %s\n", argument);
    }else{
      parseFile(fp, sketcher);
        fclose(fp);
        if (pclose(sketcher) == -1){
          fprintf(stderr, "draw_line error: couldn't close pipe to %s.\n", Exec_c);
          exit(EXIT_FAILURE);
        }
    }
}

int main (int argc,  char *argv[]){
    int i;
    if ( argc < 2 ){
        printf ("%s\n", "0 comment(s)");
    }else{
        for (i = 1; i < argc; i++){
            processArgument(argv[i]);
        }
    }
    //int *a = malloc(sizeof(int));
    return 0;
}

最佳答案

在头文件中,您声明了一个静态的数字数组,其中包括:

struct figure figurehere[17];


但是,在实现文件(* .c)中,您正在使用以下命令创建指向“图”的单独指针

struct figure *figureHere;


现在,从技术上讲,它们是完全独立的,并且figureherefigureHere都可以在您的实现文件中访问,因为C区分大小写...但是,我想这可能对您而言不是故意的,最后一种做法可能被认为是不好的做法,因为在快速阅读代码时可能会造成混淆或误导。我建议,如果您希望它们是完全独立的变量,则应为它们命名明显不同。

但是,在前面的注释中所说的是正确的答案:您正在声明一个指针figureHere,并且无需初始化就可以使用它。

....我在假设您要使figureHere(大写H)指向figurehere(低H)的前提下写出了一个解释,但是后来我意识到这个问题专门提到了malloc。不幸的是,malloc甚至没有出现在您的代码中,因此我不确定用法是“错误的”,只是没有使用。代码丢失了吗?还是您在使用malloc正确创建figure指向figureHere指向时遇到麻烦?





编辑/附加:

重读我注意到你特别说的问题


  第一部分是我的一部分
  header(.h)文件,其中我定义了两个
  我的结构。


这确实使我相信您打算将figureherefigureHere设置为同一数组。为此,您有几种选择:

1)删除figureHere,直接使用figurehere。因为您在这里已将figurehere声明为全局变量(许多人会告诉您这很不好,但这是一个完全不同的主题),所以您可以直接访问它而无需任何指针。

2)初始化figureHere指向figurehere。您只需将声明更改为

struct figure *figureHere = figurehere;

关于c - 错误使用了malloc,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/4161901/

10-15 05:51