I have declared double pointer in main and allocate memory like this
char **group_name;
group_name = realloc( NULL, 1);
group_name[0] = realloc(NULL ,20);
I have passed this array to a function,
group_count(object, count, group_name);
which uses realloc. it work fines until it populates first four reallocations, but at fifth it gives error.
libc detected *** ./textfileread.exe: realloc(): invalid next size: 0x08643008
int group_count(struct friends obj[], char cn, char **grp_nm)
{
int i=0,j=0;
int grp_cn=0;
char check=0;
strcpy(grp_nm[0],obj[0].group);
grp_cn++;
grp_count++;
for(i=1;i<cn;i++) {
for(j=0;j<grp_cn;j++) {
if(strcmp(grp_nm[j],obj[i].group)==0)
check=1;
}
if(check==0) {
grp_cn++;
grp_count++;
printf("\t%d\n",grp_cn);
grp_nm = realloc( grp_nm, grp_cn); //at grp_cn=5 allocation gives error
printf("\t%d\n",grp_nm);
if(grp_nm == NULL) printf("\t%d\n",grp_cn); // this 'if' didnt run, means no NULL return
grp_nm[grp_cn-1] = realloc(NULL ,20);
strcpy(grp_nm[grp_cn-1],obj[i].group);
}
check=0;
}
}
the output of printf("\t%d\n",grp_nm); is given below, after this at fifth iteration of reallocation
2
140783624
3
140783624
4
140783624
5
*** glibc detected *** ./textfileread.exe: realloc(): invalid next size: 0x099c8008 ***
======= Backtrace: =========
/lib/i386-linux-gnu/libc.so.6(+0x6b961)[0x17b961]
lib/i386-linux-gnu/libc.so.6(+0x6f1ad)[0x17f1ad]
/lib/i386-linux-gnu/libc.so.6(realloc+0xe9)[0x180579]
./textfileread.exe[0x804934e]
./textfileread.exe[0x8048b42]
/lib/i386-linux-gnu/libc.so.6(__libc_start_main+0xe7)[0x126e37]
./textfileread.exe[0x8048751]
======= Memory map: ========
00110000-0026a000 r-xp 00000000 08:02 1570626 /lib/i386-linux-gnu/libc-2.13.so
after 5 in output on the screen, address should display as it displayed after 4. but it didnt, so why at 5 it give error?