双指针不会正确存储指针值而不能正确释放

时间:2017-01-28 22:30:15

标签: c printf malloc double-pointer

我使用了一系列指向其他双指针的双指针。 目前我有一个结构,指向其他具有双指针的结构。

我使用“sprintf”命令来存储值,并且它工作得很好,直到遇到seg故障。

sprintf(a->cs[a->structCount].fs[sFunCount].parameterType[insideParam], 
        "%s", a->array);

我摆脱了sprintf,只是让它等于a->数组并且它起作用了。但它没有正确存储它。

a->cs[a->structCount].fs[sFunCount].parameterType[insideParam] = a->type;

它只是继续打印“int”而不是其他任何东西。当我做的时候

a->cs[a->structCount].fs[sFunCount].parameterType[insideParam] = "double";

它正确存储。

你是否也可以告诉我是否正确地对我的所有阵列进行了正确操作,以及我是否正确地释放它们。

这是我所有的代码..

#include "header.h"

int main(int argc, char * argv[]){
  all * a;
  if(argc != 2){
    printf("Error: Send input file as command line argument.\n");
    return 1;
  }
  display(argv[1]);
  a = openFile(argv[1]);
  printProgram(a);
  freeMe(a);
  return 0;
}

all * openFile(char * fileName){
  FILE *fp;
  int token = 0, z = 0, x = 0, j = 0, i = 0, dotSwitch = 0, 
      hashSwitch = 0, leftPacSwitch = 0, varNCount = 0, funNCount = 0, 
      parameter = 0, parameterName = 0, rightBrace = 0, leftBrace = 0, 
      structNameSwitch = 0, insideStruct = 0, globalCount = 0, 
      funRightBrace = 0, funLeftBrace = 0, sFunCount = 0, insideParam = 0;
  all * a = malloc(sizeof(all));
  switches * s = malloc(sizeof(switches));

  //a = m(a);

  s->structSwitch = 0;
  s->functionSwitch = 0;
  s->variableSwitch = 0;
  s->commentSwitch = 0;
  s->quoteSwitch = 0;
  s->slashSwitch = 0;
  s->nameSwitch = 0;
  s->bracketSwitch = 0;
  a->hashCount = 0;

  a->cs = malloc(sizeof(cStruct *) * 100);
  a->fs = malloc(sizeof(fStruct *) * 100);
  a->globalVariableType = malloc(sizeof(char *) * 100);
  a->globalVariableName = malloc(sizeof(char *) * 100);
  a->includes = malloc(sizeof(char *) * 100);

  a->array = malloc(sizeof(char));
  a->type = malloc(sizeof(char));
  a->vName = malloc(sizeof(char));
  a->preDot = malloc(sizeof(char));
  a->prePac = malloc(sizeof(char));
a->structName = malloc(sizeof(char));

a->structCount = 0;
a->cs[a->structCount].variableCount = 0;
for(i = 0; i < 100; i++){
    a->cs[i].name = malloc(sizeof(char)*50);
    a->cs[i].variableType = malloc(sizeof(char)*50);
    a->cs[i].variableName = malloc(sizeof(char)*50);
    a->cs[i].fs = malloc(sizeof(fStruct)*50);
    a->fs[i].functionName = malloc(sizeof(char)*50);
    a->fs[i].functionType = malloc(sizeof(char)*50);
    a->fs[i].parameterName = malloc(sizeof(char)*50);
    a->fs[i].parameterType = malloc(sizeof(char)*50);

    a->globalVariableName[i] = malloc(sizeof(char)*50);
    a->globalVariableType[i] = malloc(sizeof(char)*50);
    a->includes[i] = malloc(sizeof(char)*50);

    for(x = 0; x < 100; x++){
        a->cs[i].variableType[x]  = malloc(sizeof(char)*50);
        a->cs[i].variableName[x]  = malloc(sizeof(char)*50);
        a->cs[i].fs[x].functionName = malloc(sizeof(char)*50);
        a->cs[i].fs[x].functionType = malloc(sizeof(char)*50);
        a->cs[i].fs[x].parameterType = malloc(sizeof(char)*50);
        a->cs[i].fs[x].parameterName = malloc(sizeof(char)*50);

        a->fs[i].parameterName[x] = malloc(sizeof(char)*50);
        a->fs[i].parameterType[x] = malloc(sizeof(char)*50);

       for(z = 0; z < 100; z++){
            a->cs[i].fs[x].parameterType[z] = malloc(sizeof(char)*50);
            a->cs[i].fs[x].parameterName[z] = malloc(sizeof(char)*50);
        }

    }
}


//Opening the file
fp = fopen(fileName,"r");
//If file doesnt exist then display output
if(fp == NULL){
    perror("Error while opening the file.\n");
    exit(EXIT_FAILURE);
}
else{
    printf("\nUpdated File(C):\n");
}

while((token = fgetc(fp)) != EOF){
    if(token != ' ' && token != '{' && token != '}' && token != ';' && token != '\n' && token != '(' && token != ')' && token != '/' && token != '"' && token != '*' && token != '\\' && token != ',' && token != '.' && token != '>' && token != '#'){
        a->array[j] = token;
        j++;
    }
    else{
        if(token == '/'){
            s->commentSwitch++;
            printf("/");
        }
        else if(token == '>'){
            sprintf(a->prePac,"%s%c", a->array, token);
            leftPacSwitch = 1;
        }
        else if(token == '#'){
            hashSwitch = 1;
        }
        else if(token == '.'){
            sprintf(a->preDot,"%s%c", a->array, token);
            dotSwitch = 1;
        }
        else if(token == '{' && s->structSwitch == 1 && structNameSwitch == 0){
            sprintf(a->cs[a->structCount].name,"%s", a->structName);
            //structCount++;
            rightBrace++;
            printf("%c", token);
            structNameSwitch = 1;
            insideStruct = 1;
        }
        else if (token == '{' && s->structSwitch == 1){
            rightBrace++;
            a->cs[a->structCount].rightBracket = rightBrace;

            if (s->structFunctionSwitch == 1){
                funRightBrace++;
                a->cs[a->structCount].fs[funRightBrace].rightBracket = funRightBrace;
            }
            printf("%c", token);
        }
        else if(token == '}' && s->structSwitch == 1){
            leftBrace++;

            if(rightBrace == leftBrace){
                s->structSwitch = 0;
                rightBrace = 0;
                leftBrace = 0;
                insideStruct = 0;
                sFunCount = 0;
                a->structCount++;
            }
            else if (s->structFunctionSwitch == 1){
                funLeftBrace++;
                a->cs[a->structCount].fs[funLeftBrace].leftBracket = funLeftBrace;
                if(funRightBrace == funLeftBrace){
                   s->structFunctionSwitch = 0;
                   funRightBrace = 0;
                   funLeftBrace = 0;
                }
            }
            printf("%c", token);
        }
        else if(token == '\\'){
            s->slashSwitch = 1;
            s->quoteSwitch = 0;
            printf("%s", a->array);
            printf("%c", token);

        }
        else if(token == '(' && s->variableSwitch == 1 && s->structSwitch == 0){
            s->bracketSwitch = 1;
            sprintf(a->fs[funNCount].functionType, "%s", a->type);
            sprintf(a->fs[funNCount].functionName, "%s", a->array);
            funNCount++;
            printf("%s", a->array);
            printf("%c", token);
        }
        else if(token == '(' && insideStruct == 1 && s->variableSwitch == 1){
            s->bracketSwitch = 1;
            insideParam = 0;
            printf("%s",a->array);
            printf("%c", token);
            sprintf(a->cs[a->structCount].fs[sFunCount].functionType, "%s", a->type);
            sprintf(a->cs[a->structCount].fs[sFunCount].functionName, "%s", a->array);

            s->structFunctionSwitch = 1;
            s->variableSwitch = 0;
            s->nameSwitch = 0;
            a->cs[a->structCount].functionCount = sFunCount;
        }
        else if(token == ')' && insideStruct == 1 && s->bracketSwitch == 1){
            s->bracketSwitch = 0;
            s->structFunctionSwitch = 0;
            a->cs[a->structCount].fs[sFunCount].parameterName[insideParam] = a->array;
            a->cs[a->structCount].fs[sFunCount].parameterType[insideParam] = a->type;
            printf("%d",insideParam);
            a->cs[a->structCount].fs[sFunCount].paraCount =  insideParam;
           // printf("s: %d , f: %d, i: %d\n", a->structCount,sFunCount,insideParam);
                     //  printf("%s\n", a->cs[a->structCount].fs[sFunCount].parameterName[insideParam]);

            printf("%s", a->array);
            printf("%c", token);
            sFunCount++;
        }
        else if(token == ')' && s->variableSwitch == 1){        
            sprintf(a->fs[funNCount].parameterName[a->fs[funNCount].paraCount], "%s", a->array);
            parameterName++;

            printf("%s",a->array);
            printf("%c", token);

            s->bracketSwitch = 0;
            s->variableSwitch = 0;
            parameter = 0;
        }
        else if(token == '*'){
            printf("%s", a->array);
            printf("%c", token);
        }
        else if(s->commentSwitch == 2){
            printf("%s",a->array);
            printf("%c", token);
            if(token == '\n'){
                s->commentSwitch = 0;
            }
        }
        else if(s->slashSwitch == 1){
            printf("%s",a->array);
            printf("%c", token);
            s->slashSwitch = 0;
        }
        else if(token == '"' && s->slashSwitch == 0 && s->commentSwitch == 0){
                s->quoteSwitch++;
                printf("%s",a->array);
                printf("%c", token);

            if(s->quoteSwitch == 2){
                s->quoteSwitch = 0;
            }

        }
        else if(s->quoteSwitch == 1 && token != '"'){
            printf("%s",a->array);
            printf("%c", token);
        }
        else{
            if(hashSwitch == 1 && dotSwitch == 1 && leftPacSwitch == 1){
                printf("#%s%s", a->preDot,a->prePac);
                sprintf(a->includes[a->hashCount], "%s%s", a->preDot,a->prePac);
                a->hashCount++;
                hashSwitch = 0;
                dotSwitch = 0;
                leftPacSwitch = 0;
            }
            else if(strcmp(a->array,"class") == 0  && s->commentSwitch != 2 && s->quoteSwitch != 1){
                strcpy(a->array,"struct");
                s->structSwitch = 1;
                structNameSwitch = 0;
                varNCount = 0;
            }
            else if(s->structSwitch == 1 && insideStruct == 1){
                if(strcmp(a->array,"int") == 0 || strcmp(a->array,"double") == 0 || strcmp(a->array,"float") == 0 || strcmp(a->array,"char") == 0 || strcmp(a->array,"void") == 0){
                   if(s->bracketSwitch == 0 ){
                       strcpy(a->type,a->array);
                       s->variableSwitch = 1;
                       s->nameSwitch = 0;
                   }
                   else if(s->bracketSwitch == 1){
                       //   printf("vjjkjj");
                       a->cs[0].fs[0].parameterType[4] = a->type;

                       strcpy(a->type,a->array);
                       s->variableSwitch = 0;
                       s->nameSwitch = 0;
                       s->structFunctionSwitch = 1;
                       insideParam++;

                       a->cs[a->structCount].fs[sFunCount].paraCount++;
                   }
                 // printf("var1: %d nam: %d Bra: %d fun: %d inside: %d st: %d\n", s->variableSwitch, s->nameSwitch,s->bracketSwitch, s->structFunctionSwitch, insideStruct, s->structSwitch);

                   /*else{
                       s->variableSwitch = 0;
                   }*/
                }

                //Gets all the variables in the struct
                else if(s->nameSwitch == 0 && s->variableSwitch == 1 && s->bracketSwitch == 0){
                    varNCount++;

                    strcpy(a->vName,a->array);
                    a->cs[a->structCount].variableCount = varNCount;
                    sprintf(a->cs[a->structCount].variableType[varNCount], "%s", a->type);
                    sprintf(a->cs[a->structCount].variableName[varNCount], "%s", a->vName);
                    s->variableSwitch = 0;
                }
                else if(s->nameSwitch == 0 && s->variableSwitch == 0 && s->bracketSwitch == 1 && s->structFunctionSwitch == 1){
                      a->cs[a->structCount].fs[sFunCount].parameterType[insideParam] = a->type;
                      a->cs[a->structCount].fs[sFunCount].parameterName[insideParam] = a->array;
                      if(strcmp(a->type, "double") == 0){
                          a->cs[a->structCount].fs[sFunCount].parameterType[insideParam] = "double";;
                      }
                      a->cs[1].fs[0].parameterType[3] = "double";
                      a->cs[1].fs[0].parameterName[3] = "gg";

                     printf("struct: %s\n", a->type);
                     //printf("structh: %d, Function: %d, Param: %d", a->structCount, sFunCount, insideParam);
                     //  a->cs[a->structCount].fs[sFunCount].paraCount =  insideParam;
                     //  printf("HAHA%d", a->cs[a->structCount].fs[sFunCount].paraCount);
                      s->nameSwitch = 1;
                   }


            }
            else if(s->structSwitch == 1 && structNameSwitch == 0){
                strcpy(a->structName,a->array);
            }

            else if(strcmp(a->array,"int") == 0 || strcmp(a->array,"double") == 0 || strcmp(a->array,"float") == 0 || strcmp(a->array,"char") == 0 || strcmp(a->array,"void") == 0){
                if(s->bracketSwitch == 1){
                    sprintf(a->fs[funNCount].parameterType[parameter], "%s", a->array);
                    a->fs[funNCount].paraCount = parameter;
                    parameter++;
                }
                else{
                    strcpy(a->type,a->array);
                }
                s->variableSwitch = 1;
                s->nameSwitch = 0;
            }
            else if(s->variableSwitch == 1 && s->nameSwitch == 0){
                if(s->bracketSwitch == 1){
                    sprintf(a->fs[funNCount].parameterName[a->fs[funNCount].paraCount], "%s", a->array);
                    parameterName++;
                }
                else{
                    strcpy(a->vName,a->array);
                }
                s->nameSwitch = 1;
            }
            else if(s->variableSwitch == 1 && s->nameSwitch == 1 && s->bracketSwitch == 0){
                sprintf(a->globalVariableType[globalCount], "%s", a->type);
                sprintf(a->globalVariableName[globalCount], "%s", a->vName);
                a->globalCount = globalCount;
                globalCount++;

                s->variableSwitch = 0;
                s->nameSwitch = 0;
            }
        printf("%s",a->array);
        printf("%c", token);

        }
    memset(a->array,0,strlen(a->array));
    j = 0;
    }
}

a->funCount = funNCount;

printf("%d\n\n",a->fs[2].paraCount);

printf("{ : %d\n", rightBrace);

printf("} : %d\n", leftBrace);
fclose(fp);
return a;
}

int fOrV(char * c){
int len = strlen(c), i;
//char * array = malloc(sizeof(char*));
for(i = 0;i < len; i++){
    if(c[i] == '('){
        return 1;
    }
    else if(c[i] == ')' || c[i] == ','){
        //printf("LOOL %s\n",c);
        return 2;
    }
}
return 0;
}

int display(char * fileName){
FILE *fp;
char * array;
int token = 0, totalStruct = 0, totalVariable = 0, j = 0;

array = malloc(sizeof(char));

//Opening the file
fp = fopen(fileName,"r");
//If file doesnt exist then display output
if(fp == NULL){
    perror("Error while opening the file.\n");
    exit(EXIT_FAILURE);
}
else{
    printf("File %s opened successfully\nOriginal Code(C++):\n", fileName);
}

    while((token = fgetc(fp)) != EOF){
    if(token != ' ' && token != '{' && token != '}' && token != ';' && token != '\n' && token != '(' && token != ')' && token != '/' && token != '"' && token != '*' && token != '\\' && token != ',' && token != '.' && token != '[' && token != ']'){
        array[j] = token;
        j++;
    }
    else{
        if(strcmp(array,"class") == 0){
            totalStruct++;
        }
        else if(strcmp(array,"int") == 0 || strcmp(array,"double") == 0 || strcmp(array,"float") == 0 || strcmp(array,"char") == 0 || strcmp(array,"void") == 0){
            totalVariable++;
        }
        memset(array,0,strlen(array));

        j = 0;
        printf("%s",array);

    }

        printf("%c", token);
}
fclose(fp);

return 0;
}

void printProgram(all * a){
int b, i;
for(b = 0; b < a->structCount; b++){
    printf("Struct Name: %s\n", a->cs[b].name);
    for(i = 1; i <= a->cs[b].variableCount; i++){
        printf("\tVariabe Type: %s %s\n", a->cs[b].variableType[i],a->cs[b].variableName[i]);
    }
    for(i = 0; i <= a->cs[b].functionCount; i++){
        printf("\tFunction Type: %s %s\n", a->cs[b].fs[i].functionType,a->cs[b].fs[i].functionName);

    }
}
for(i = 0; i <= a->globalCount; i++){
    printf("Global Variable: %s %s\n", a->globalVariableType[i], a->globalVariableName[i]);
}

printf("----------------------------------------------\n");

for(i = 0; i < a->hashCount; i++){
    printf("%s\n", a->includes[i]);
}
printf("\n");
for(i = 0; i <= a->globalCount; i++){
    printf("%s %s;\n", a->globalVariableType[i], a->globalVariableName[i]);
}
printf("\n");
for(b = 0; b < a->structCount; b++){
    printf("struct %s {\n", a->cs[b].name);
    for(i = 1; i <= a->cs[b].variableCount; i++){
        printf("\t%s %s;\n", a->cs[b].variableType[i],a->cs[b].variableName[i]);
    }
    for(i = 0; i <= a->cs[b].functionCount; i++){
        printf("\t%s (*%s%s)();\n", a->cs[b].fs[i].functionType,a->cs[b].name,a->cs[b].fs[i].functionName);

    }
    printf("}\n");
}
printf("WHAT: %s\n", a->cs[0].fs[0].parameterType[3]);

int x;
for(b = 0; b < a->structCount; b++){
       // printf("S: %d", b);

    for(i = 0; i <= a->cs[b].functionCount; i++){
            //printf("F: %d", i);

        printf("%s %s%s(", a->cs[b].fs[i].functionType,a->cs[b].name,a->cs[b].fs[i].functionName);

        for(x = 1; x <= a->cs[b].fs[i].paraCount; x++){
            //printf("F: %d\n", x);

            if(x == (a->cs[b].fs[i].paraCount)){
                printf("%s %s", a->cs[b].fs[i].parameterType[x],a->cs[b].fs[i].parameterName[x]);
            }
            else if(x == 1){
                printf("%s %s,", a->cs[b].fs[i].parameterType[x],a->cs[b].fs[i].parameterName[x]);
            }
            else{
                printf(" %s %s,", a->cs[b].fs[i].parameterType[x],a->cs[b].fs[i].parameterName[x]);
            }
        }
        printf("){\n");

    }
                printf("\n");

}
for(i = 0; i < a->funCount; i++){
    printf("%s %s(", a->fs[i].functionType, a->fs[i].functionName);
    for(b = 0; b <= a->fs[i+1].paraCount; b++){
        printf("%s %s ", a->fs[i+1].parameterType[b], a->fs[i+1].parameterName[b]);
    }
    printf("){\n");

}

}

void freeMe(all * a){
int i, x;
/*for(i = 0; i < 100; i++){
    free(a->cs[i].name);
    free(a->cs[i].variableType);
    free(a->cs[i].variableName);
    free(a->cs[i].fs);
    free(a->fs[i].functionName);
    free(a->fs[i].functionType);
    free(a->fs[i].parameterName);
    free(a->fs[i].parameterType);

    free(a->globalVariableName[i]);
    free(a->globalVariableType[i]);
    free(a->includes[i]);
}
free(a->cs);
free(a->fs);
free(a->globalVariableType);
free(a->globalVariableName);
free(a->includes);

free(a->array);
free(a->type);
free(a->vName);
free(a->preDot);
free(a->prePac);
free(a->structName);*/

free(a);
//free(s);
}

1 个答案:

答案 0 :(得分:0)

即使没有遗漏所有结构的#include "header.h",我也使用提供的源代码的内容来恢复嵌套数据结构并检查malloc()的所有调用。

已恢复数据 - 这里是所有反向数据结构(应该来自“header.h”)

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

typedef struct st_fStruct {
    char *functionName;
    char *functionType;
    char **parameterName;
    char **parameterType;
    int rightBracket;
    int leftBracket;
    int paraCount;
} fStruct;

typedef struct st_cStruct {
    int variableCount;
    char *name;
    char **variableType;
    char **variableName;
    fStruct *fs;
    int rightBracket;
    int functionCount;
} cStruct;

typedef struct st_all {
    int hashCount;
    cStruct *cs;
    fStruct *fs;
    char **globalVariableType;
    char **globalVariableName;
    char **includes;
    char *array;
    char *type;
    char *vName;
    char *preDot;
    char *prePac;
    char *structName;
    int structCount;
    int globalCount;
    int funCount;
} all;

typedef struct st_switches {
    int structSwitch;
    int functionSwitch;
    int variableSwitch;
    int commentSwitch;
    int quoteSwitch;
    int slashSwitch;
    int nameSwitch;
    int bracketSwitch;
    int structFunctionSwitch;
} switches;

int display(char * fileName);
all* openFile(char * fileName);
void printProgram(all* p_all);
void freeMe(all* p_all);

游戏分析 - 以下是所有提供的malloc()和更正的提案。

  

调用sizeof()malloc()个项目出错。

a->cs = malloc(sizeof(cStruct *) * 100); // must be sizeof(cStruct)
a->fs = malloc(sizeof(fStruct *) * 100); // must be sizeof(fStruct)
  

为字符串仅分配一个char时出错。

a->array = malloc(sizeof(char)); // must be (sizeof(char)*50)
a->type = malloc(sizeof(char)); // must be (sizeof(char)*50)
a->vName = malloc(sizeof(char)); // must be (sizeof(char)*50)
a->preDot = malloc(sizeof(char)); // must be (sizeof(char)*50)
a->prePac = malloc(sizeof(char)); // must be (sizeof(char)*50)
a->structName = malloc(sizeof(char)); // must be (sizeof(char)*50)
// in the display function
array = malloc(sizeof(char)); // must be (sizeof(char)*50)
  

字符串char *与字符串数组char **之间的缓解。

a->cs[i].variableType = malloc(sizeof(char)*50); // must be malloc(sizeof(char *)*100);
a->cs[i].variableName = malloc(sizeof(char)*50); // must be malloc(sizeof(char *)*100);
a->fs[i].parameterName = malloc(sizeof(char)*50); // must be malloc(sizeof(char *)*100);
a->fs[i].parameterType = malloc(sizeof(char)*50); // must be malloc(sizeof(char *)*100);
a->cs[i].fs[x].parameterType = malloc(sizeof(char)*50); // must be malloc(sizeof(char *)*100);
a->cs[i].fs[x].parameterName = malloc(sizeof(char)*50); // must be malloc(sizeof(char *)*100);
  

struct fStruct数组的大小不正确。

a->cs[i].fs = malloc(sizeof(fStruct)*50); // must be malloc(sizeof(fStruct)*100);
  

正确使用malloc()(17/33)。

all * a = malloc(sizeof(all));
switches * s = malloc(sizeof(switches));
a->globalVariableType = malloc(sizeof(char *) * 100);
a->globalVariableName = malloc(sizeof(char *) * 100);
a->includes = malloc(sizeof(char *) * 100);
for(i = 0; i < 100; i++){
    a->cs[i].name = malloc(sizeof(char)*50);
    a->fs[i].functionName = malloc(sizeof(char)*50);
    a->fs[i].functionType = malloc(sizeof(char)*50);
    a->includes[i] = malloc(sizeof(char)*50);
    ...
    for(x = 0; x < 100; x++){
        a->cs[i].variableType[x]  = malloc(sizeof(char)*50);
        a->cs[i].variableName[x]  = malloc(sizeof(char)*50);
        a->cs[i].fs[x].functionName = malloc(sizeof(char)*50);
        a->cs[i].fs[x].functionType = malloc(sizeof(char)*50);
        a->fs[i].parameterName[x] = malloc(sizeof(char)*50);
        a->fs[i].parameterType[x] = malloc(sizeof(char)*50);
        ...
        for(z = 0; z < 100; z++){
            a->cs[i].fs[x].parameterType[z] = malloc(sizeof(char)*50);
            a->cs[i].fs[x].parameterName[z] = malloc(sizeof(char)*50);