简体   繁体   English

从调用到破坏malloced结构的Seg Fault

[英]Seg Fault from call to destroy malloced struct

So when I call the following function I get a seg fault: 因此,当我调用以下函数时,我得到一个seg错误:

void destroyVariableVector(VariableVector* variableVector) {
    if (variableVector) {
        free(variableVector->variables); // <== Seg Fault here
        free(variableVector);
    }
}

And this is what my structs look like: 这就是我的结构:

struct _Variable {
    char *variableName;
    char *arrayOfElements;
    int type;
    int32_t address;
};
typedef struct _Variable Variable;

struct _VariableVector {
    int size; // elements full in array
    int capacity; // total available elements
    Variable *variables;
};
typedef struct _VariableVector VariableVector;

and here are their init methods: 这是他们的init方法:

Variable* initVariable(char *variableName, char *arrayOfElements,
        int32_t address, int type) {
    Variable* initialVariable = malloc(sizeof(*initialVariable));
    if (initialVariable != NULL ) {
        initialVariable->variableName = strdup(variableName);
        initialVariable->arrayOfElements = strdup(arrayOfElements);
        initialVariable->address = address;
        initialVariable->type = type;
    }
    return initialVariable; // may be NULL
}

VariableVector* initVariableVector() {
    VariableVector* initialVariableVector = malloc(sizeof(VariableVector));
    if (initialVariableVector != NULL ) {
        initialVariableVector->size = 0;
        initialVariableVector->capacity = VECTOR_INITIAL_CAPACITY;
        initialVariableVector->variables = malloc(
                sizeof(Variable) * VECTOR_INITIAL_CAPACITY);
    }
    return initialVariableVector;
}

Can anyone explain how I am getting a seg fault when I call my destroyVariableVector() method??? 当我调用我的destroyVariableVector()方法时,任何人都可以解释我是如何得到一个段错的吗???

Here is the code that calls the above functions: 以下是调用上述函数的代码:

VariableVector* variableVector = initVariableVector();
// add some variables to variableVector
writeOutVariables(variableVector, outputFilePointer);
destroyVariableVector(variableVector);

where the method writeOutVariables looks like: writeOutVariables方法的位置如下:

void writeOutVariables(VariableVector *variableVector, FILE *outputFilePointer) {
    // write out all variables within the variableVector to the output file
    int variableVectorSize = variableVector->size;
    int i = 0;
    // start from the first variable in the varaibleVector to the last variable
    while (i < variableVectorSize) {
        //  0 = label; 1 = variable ascii string; 2 = variable number array;
        int currentType = variableVector->variables->type;
        if (currentType == 1) {
            writeToFileASCIICharArrayInReverseOrder(
                    variableVector->variables->arrayOfElements,
                    outputFilePointer);
            i++;
        } else if (currentType == 2) {
            char currentNumberArray[MAXIMUM_LINE_LENGTH + 1]; // + 1 for terminating char
            strcpy(currentNumberArray,
                    variableVector->variables->arrayOfElements);

            char* currentNumber = strtok(currentNumberArray, " ,\t\n");
            while (currentNumber != NULL ) {
                // you have not yet reached the end of the numberArray
                int integer = atoi(currentNumber);
                writeToFileIntegerAs32bits(integer, outputFilePointer);
                currentNumber = strtok(NULL, " ,\t\n");
            }
            i++;
        } else {
            i++;
        }

        variableVector->variables++; // move on to next variable
    }
}

In writeOutVariables , you're incrementing variableVector->variables to iterate through your list. writeOutVariables ,您将递增variableVector->variables以遍历列表。 However, this because it's a pointer, you're incrementing the actual value and hence moving the pointer beyond the end. 但是,这是因为它是一个指针,你正在递增实际值,因此将指针移到末尾之外。 Then, when you try to free variables, you're actually freeing memory that wasn't previously malloced. 然后,当你尝试释放变量时,你实际上正在释放以前没有被malloced的内存。

Instead, initialize a pointer to variableVector->variables inside writeOutVariables so you don't clobber that pointer. 相反,在writeOutVariables初始化一个指向variableVector->variables的指针,这样你就不会破坏那个指针。

void writeOutVariables(VariableVector *variableVector, FILE *outputFilePointer) {
    Variable *tmpVariables = variableVector->varaibles;
    // The original code
    // ...
    tmpVariables++;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM