Question

I'm dealing with a struct that contains an int and an array of ints

here are my two functions:

//stores array of char numbers in array of ints
//returns pointer to SomeStruct
SomeStruct *fromString(char *str){

    /*get string length*/
    int length = strlen(str);


    SomeStruct *huge;
    huge = malloc(sizeof(SomeStruct));

    /* incase malloc() fails */
    if ( huge == NULL) {
        return NULL;
    }
    huge->length = length;
    huge->digits = malloc((sizeof(int))*length);

    /*store chars as ints*/
    int i;
    for(i=0;i<length;i++){
        if (str[i] == ""){
            huge->digits[i] = 0;
        }
        if (str[i] == '0'){
            huge->digits[i] = 0;
        }
        if (str[i] == '1'){
            huge->digits[i] = 1;
        }
        if (str[i] == '2'){
            huge->digits[i] = 2;
        }
        if (str[i] == '3'){
            huge->digits[i] = 3;
        }
        if (str[i] == '4'){
            huge->digits[i] = 4;
        }
        if (str[i] == '5'){
            huge->digits[i] = 5;
        }
        if (str[i] == '6'){
            huge->digits[i] = 6;
        }
        if (str[i] == '7'){
            huge->digits[i] = 7;
        }
        if (str[i] == '8'){
            huge->digits[i] = 8;
        }
        if (str[i] == '9'){
            huge->digits[i] = 9;
        }
    }

    return huge;
}

and

//frees mem
//returns NULL ptr to struct
SomeStruct *destroy(SomeStruct *p){


    /*free each cell*/
    int i;
    for(i=0;i<getLength(p);i++){
        free(p->digits[i]);
    }
    /*free p  & return NULL*/
    free(p);
    p=NULL;

    return p;
}

and heres my sample main()

int main(){

    SomeStruct *z;
    z = malloc(sizeof(SomeStruct));

    z = fromString("100100987654321");

    //printcheck
    int i;
    for(i=0;i<getLength(z);i++){
      printf("%d", z->digits[i]);
    }
    printf("\n");

    destroy(z);

    return 0;
}

After I compile, I get the proper printout of 100100987654321, but once its hits the destroy function i get the following error: malloc: * error for object 0x2: pointer being freed was not allocated.

I played around with the destroy function and free(p) alone doesn't throw an error, but when i try to free anything within the array (p->digits) i get that error message. I assume the error has to do with my malloc within the fromString function, but how? thanks

Was it helpful?

Solution

The problem is this:

/*free each cell*/
int i;
for(i=0;i<getLength(p);i++){
    free(p->digits[i]);
}

You don't want to free each cell, you want to free all of them at once. The above should be replaced with

free(p->digits);

Remember how you allocated the memory was:

huge->digits = malloc((sizeof(int))*length);

You didn't allocate a different pointer for each element of digits, so no need to free every element separately.

If you are using unix/linux system, In terminal:

man 3 malloc

man 3 free

you will get more information about this.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top