c - free( ptr ) error - invalid size -


i have c program seems performing ok unless call free on pointer.

#include <stdio.h> #include <sys/types.h> #include <unistd.h> #include <stdlib.h>  int main() {     int i;     gid_t *list;      int num_groups = getgroups(0, null);      list = malloc(num_groups * sizeof(list));      if (getgroups(num_groups, list) != -1)     {         (i = 0; < num_groups; i++)         {             printf("%d ", *list++);         }     }     free(list); // free(): invalid size: 0x0000000002448040 ***     return 0; } 

i've seen lots of similar posts refer invalid next size, not invalid size.

i'm newbie when comes c - can't work out what's going on.

you changing list *list++ , therefore free invalid. need free original pointer obtained malloc.


as dasblinkenlight notes, you're not allocating memory right way. think going idiom, forgot *:

list = malloc(num_groups * sizeof *list);                                   ^ 

Comments

Popular posts from this blog

c# - How Configure Devart dotConnect for SQLite Code First? -

java - Copying object fields -

c++ - Clear the memory after returning a vector in a function -