-1

So I wrote my 2 structs in a file:

struct _Variable {
    char *variableName;
    char *arrayOfElements;
    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 I also implemented a init method for each:

Variable* initVariable(char *variableName, char *arrayOfElements,
        int32_t address) {
    Variable* initializedVariable = malloc(sizeof(Variable));
    if (initializedVariable != NULL ) {
        initializedVariable->variableName = variableName;
        initializedVariable->arrayOfElements = arrayOfElements;
        initializedVariable->address = address;
        return initializedVariable;
    } else {
        return NULL ;
    }
}

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

now I would like to write a freeVariable(Variable* variable) method and a freeVariableVector(VariableVector* variableVector) method. But I don't know how?

3
  • 1
    What's the difference with stackoverflow.com/questions/20143630/… ? Nov 22, 2013 at 11:34
  • Now that i know I was doing it wrong I have no idea how to write the free functions
    – letter Q
    Nov 22, 2013 at 11:35
  • For your own sake, I recommend writing shorter names! initializedVariableVector is very hard to read and the meaning of var_vec as a local variable in a 5 line functions is equally clear, but is much easier to read!
    – Shahbaz
    Nov 22, 2013 at 11:44

1 Answer 1

0

Just call free on the pointer you want to free. As answered in your other nearly identical question.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.