开发者

Wrong usage of malloc

I am trying to use dynamic memory for my code in C. I am using struct and I am doing something wrong, but I can't figure out what. The first part is part of my header(.h) file where I define both of my structs.

The second part is part of my C(.c) file where I initialize the struct and use it. I get a seg fault and ddd tells me that seg. fault occurs in the line after the comment: this is where the seg. fault occurs. Any help would be most appreciated

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

first part

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

    #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];

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

second part

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

#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;                        
}  

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

third part

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

#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(开发者_StackOverflow中文版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;
}


In your header file, you're declaring a static array of figures with:

struct figure figurehere[17];

But in your implementation file (*.c), you are creating a separate pointer to a 'figure' with

struct figure *figureHere;

Now, technically these are completely independent and both figurehere and figureHere will be accessible in your implementation file, because C is case-sensitive...but, I imagine this might not be intentional on your part, and at the very last might be considered poor practice because of it's potential to be confusing or misleading when reading the code quickly. I would recommend, if you intend them to be completely separate variables, that you name them more obviously different.

However, what was said in comments earlier is the correct answer: You are declaring a pointer figureHere, and using it without ever having initialized it.

....I had written out an explanation under the assumption that you intended for figureHere (capital H) to point to figurehere (lower H), but then I realized that the question specifically mentions malloc. Unfortunately, malloc does not even appear in your code, so I am unsure how the use is "wrong" except that there is no use. Is code missing, or are you having trouble correctly using malloc to create a figure for figureHere to point to?



Edit/Additional:

Re-reading the question I notice you specifically say

The first part is part of my header(.h) file where I define both of my structs.

This really makes me believe that you intended for figurehere and figureHere to be the same array. To make this happen, you have a few options:

1) Remove figureHere and just use figurehere directly. Because you have declared figurehere as a global here (which many will tell you is bad, but that's a completely different topic), you can access it directly without requiring any pointers.

2) Initialize figureHere to point to figurehere. You can do this by simply changing your declaration to

struct figure *figureHere = figurehere;

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜