Merl
Merl

Reputation: 96

can't free double pointer

my problem is I have to free a double pointer. I already tried it:

char** files = malloc(sizeof(char*) * files_int);
for( i=1; i <= files_int ; i++)
        {
            files[i] = malloc(500);
            //fill with data...
        }
//call function with needs the double pointer
functionA(files);

//free first array
for(x=1; x <= max_files ; x++){
   free(files[x]);
   }
//free second array
free(files);

I am always getting a glibc detected double free or corruption (out) error.

What am I doing wrong?

Upvotes: 0

Views: 271

Answers (1)

chqrlie
chqrlie

Reputation: 144770

Arrays in C are 0 based:

for( i=1; i <= files_int ; i++)
    files[i] = malloc(500);

causes a buffer overflow, possibly corrupting the memory allocation system, and you do not allocate files[0].

Upvotes: 3

Related Questions