2010-11-14 112 views
0

我是C新手,我無法使用動態內存。 我malloc爲結構圖,然後根據需要realloc。當我試圖調試我的代碼時,我注意到在ddd的幫助下,我覆蓋了前一個數字的一​​個數字(在我提供的第二塊代碼中 - 變量的名稱是figureHere)。如果有人能看到我愚蠢的錯誤的共鳴,請讓我知道。動態內存:malloc和realloc

第一塊代碼在第二塊中調用void。

#include <assert.h> 
#include "draw2.h" 
#include "draw2a.h" 
#include "draw2b.h" 

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

void parseFile(FILE * fp, FILE *sketcher){ 
    char line [MAX_WORD] = {"NULL"}; 
    char word [MAX_WORD] = {"NULL"}; 
    char figureName [MAX_WORD] = {"NULL"}; 
    struct figure *pointsAndname;      
    int countNumberoffigures = 0;         //accounts to which figure in the array we are on 
    printOutput(); 
    int temp = 0; 
    pointsAndname = malloc(temp); 
    assert(pointsAndname != NULL); 
    while (fgets(line, MAX_WORD - 1, fp) != NULL){ 
     int nuRead = sscanf(line, "%s", word); 
     assert(pointsAndname != NULL); 
     if (nuRead > 0){ 
      if(strncmp(word, "Figure", MAX_WORD)==0){   //1)reads the figure, name and the two starting points 
       countNumberoffigures += 1;      
       assert(pointsAndname != NULL); 
       figureFunction(fp,line, word, figureName, countNumberoffigures, pointsAndname + countNumberoffigures - 1); 
       if (temp <= countNumberoffigures){ 
        temp += sizeof(struct figure); 
        pointsAndname = realloc(pointsAndname, temp); 
       } 
      }             
      if(strncmp(word, "printFigure", MAX_WORD)==0){  //4)read the command printFigure, name of the figure 
       printFigure(fp, line, countNumberoffigures, pointsAndname + countNumberoffigures - 1); 
      } 
      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); 
      } 
      if(strncmp(word, "end", MAX_WORD)==0){ 
       fprintf (sketcher, "end\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 (fp == NULL){ 
     printf ("Could not open pipe to %s\n", argument); 
     exit (EXIT_FAILURE); 
    } 
    if (sketcher == NULL){ 
     printf ("Could not open pipe to %s\n", argument); 
     exit (EXIT_FAILURE); 
    }else{ 
     parseFile(fp, sketcher); 
     if(fclose(fp)==EOF){ 
      printf("couldn't close pipe to %s.\n", argument); 
      exit(EXIT_FAILURE); 
     } 
     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)"); 
     exit(EXIT_FAILURE); 
    }else{ 
     for (i = 1; i < argc; i++){ 
      processArgument(argv[i]); 
     } 
    } 
    return 0; 
} 

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

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.*/ 
void figureFunction (FILE * fp, char line[], char word[], char figureName[], int countNumberoffigures, struct figure *figureHere){ 
    double startx, starty; 
    int temp = 0; 
    printf("The line: %s", line); 

    figureHere->vertices = malloc(temp); 
    sscanf(line, "%s%s%lf%lf%*s", word, figureHere->figureName, &startx, &starty); 
    (*(figureHere->vertices)).x = startx;    
    (*(figureHere->vertices)).y = starty; 
    printf("I'm in side figureFunction and this is my figureHere->figureName = %s\n\n", figureHere->figureName); 
    fgets(line, MAX_WORD - 1, fp);     
    int nuRead = sscanf(line, "%s", word);    //until the line with End Figure I construct my points of figure. 
    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; 
    if (temp >= figureHere->countPoints){ 
     temp += sizeof(struct pointxy); 
     figureHere->vertices = realloc(figureHere->vertices, temp); 
    }       
} 

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

/* prototypes for non-c99 library functions */ 
FILE* popen(const char*, const char*); 
int pclose(FILE*); 
struct pointxy { 
    double x; 
    double y; 
}; 

struct figure{ 
    //struct figure *nextfigure; 

    char figureName[MAX_WORD]; 
    struct pointxy *vertices; 
    int countPoints; 
}; 

struct figure *figurehere; 
+3

將來,您應該儘量簡化您的示例代碼,儘可能減少仍然會產生錯誤的最小數量。這甚至可以幫助你自己解決問題。 – 2010-11-14 00:42:00

回答

2

保羅,

您最初的malloc()是零級;你至少需要一個元素才能開始。最重要的是,你正在做這個倒退。您正在嘗試在分配空間來保存它們之前將值賦給figureHere。要麼你需要找出結構中需要多少新值,然後調用malloc()或將realloc()代碼放入if(strncmp(word,「draw」,MAX_WORD)== 0)部分:

if (strncmp(word, "draw", MAX_WORD) == 0){ 
    temp += sizeof(struct pointxy); 
    figureHere->vertices = realloc(figureHere->vertices, temp); 
    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; 
} 
+0

而不是temp = o我可以做temp = sizeof(struct pointxy)嗎? – 2010-11-14 00:40:07

+0

是的,您需要爲至少一個元素分配空間。 – vonmoltke 2010-11-14 00:50:46

+0

謝謝。現在我得到段錯誤......這場戰鬥非常艱難:( – 2010-11-14 01:06:57

1
void figureFunction (FILE * fp, char line[], char word[], char figureName[], int countNumberoffigures, struct figure *figureHere){ 
    /* ... */ 
    int temp = 0; 
    /* ... */ 
    figureHere->vertices = malloc(temp); 

溫度是0

/* ... */ 
    int nuRead = sscanf(line, "%s", word);    //until the line with End Figure I construct my points of figure. 
    int i = 1;              
    while (strncmp(word, "End", MAX_WORD)!=0){ 
     /* ... */ 
     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[1]不存在

  figureHere->vertices[i].y = figureHere->vertices[i-1].y + starty; 
      i += 1; 
     } 
     fgets(line, MAX_WORD - 1, fp); 
     nuRead = sscanf(line, "%s", word); 
    } 
    /* ... */ 
} 
+0

可以寫intead: – 2010-11-14 00:34:07

+0

temp = sizeof(struct figure); – 2010-11-14 00:35:12

+0

@Paul:簡潔地說,@ pmg建議(有些間接),你需要在使用它之前分配空間。分配零字節是實現定義的行爲;你可能會得到一個NULL指針,或者你可能會得到一個非空指針,但由於零字節的可用空間,除了釋放它之外,你無法對它做任何事情。作爲一般規則,當在SO上提出問題時,消除代碼中不必要的所有內容 - 並且在代碼中顯示了很多不必要的內容。另外,使它可編譯 - 你提供的不是。 – 2010-11-14 00:38:40