user8821501
user8821501

Reputation:

munmap_chunk(): invalid pointer (error in while condition)

i have unknown error in my code after i test my code by printing i know that function that cause the error :

void clearTrainSet(){

        struct trainSet * curSet=trnHead;
        struct trainSet * tmp;
puts("z");
        while(curSet!=NULL){
puts("0");
                tmp=curSet;
puts("1");
                curSet=curSet->next;
puts("2");
                free(tmp->input);
puts("3");
                free(tmp->output);
puts("4");
                free(tmp);
puts("x");
        }
        trnHead=NULL;
        if(filename!=NULL){
                free(filename);
                filename=NULL;
        }
puts("c");
}

after testing the program the result is:

./neuromz -new 1 2 3 -name dsd
Network initialized successfully.
z
*** Error in `./neuromz': munmap_chunk(): invalid pointer: 0x00007ffde363d2c0 ***
Aborted (core dumped)

so the error in while's condition

Upvotes: 1

Views: 557

Answers (1)

babon
babon

Reputation: 3774

As mentioned in your comment, it is evident that you were passing such a pointer to free which was not returned by malloc, calloc or realloc. This invokes UB (in fact, IMO, you were quite lucky to get a segmentation fault. It could have been worse). This is what the man page says about void free(void *ptr);:

The free() function frees the memory space pointed to by ptr which must have been returned by a previous call to malloc(), calloc() or realloc(). Otherwise, or if free(ptr) has already been called before, undefined behavior occurs. If ptr is NULL, no operation is performed.

Upvotes: 2

Related Questions