Nemo
Nemo

Reputation: 25

munmap_chunk():invalid pointer

in header file is declared

typedef struct htab {
size_t size;
size_t arr_size;
struct htab_item *arr[];
}htab_t;

and have function

htab_t *htab_init(size_t)
{
   htab_t *table = malloc(sizeof(htab_t)+n*(sizeof(struct htab_item*)));
   if (table == NULL)
   {
       fprintf(stderr,"Error: allocation failed\n");
       return NULL;
   }

at malloc line it throws error: munmap_chunk():invalid pointer can anyone pls explain why is this happening? edit: n is for size of arr in struct i ran this through gdb and when the line reaches this malloc it throws me out and says pointer error so it cannot even reach funcion "free"

Upvotes: 0

Views: 430

Answers (1)

Zan Lynx
Zan Lynx

Reputation: 54355

What is happening is that the malloc library code often does not realize that it has corrupted data structures until later in the program. So the location of a crash or error message does not always correspond to the actual mistake.

Glibc has malloc debugging modes enabled by environment variables, but I find valgrind to be more useful all around.

Upvotes: 0

Related Questions