I have a linked list which I am trying to read in reverse, however I'm having problems with doing so.
This is my struct.
typedef struct node{
int x;
struct node *next;
}node;
The problem is with my reading function. This is the function:
void read(){
node *iter;
while(r->next!=NULL){
iter=r;
while(iter->next!=NULL) iter=iter->next;
printf("%d",iter->x);
free(iter);
}
printf("%d",r->x);
}
I iterate to the end of the list, I then read the data and delete it afterwards. And looping it. But it's not working. Could anyone please explain to me what I'm doing wrong? Also output of the program is:
* Error in `./final': double free or corruption (fasttop): 0x00000000022e24a0 * ======= Backtrace: ========= /lib/x86_64-linux-gnu/libc.so.6(+0x777e5)[0x7faccbe7b7e5] /lib/x86_64-linux-gnu/libc.so.6(+0x8037a)[0x7faccbe8437a] /lib/x86_64-linux-gnu/libc.so.6(cfree+0x4c)[0x7faccbe8853c] ./final[0x400942] ./final[0x400a06] /lib/x86_64-linux-gnu/libc.so.6(__libc_start_main+0xf0)[0x7faccbe24830] ./final[0x4006b9] ======= Memory map: ======== 00400000-00401000 r-xp 00000000 08:01 8257622 /home/r4g3/Desktop/2U/final 00600000-00601000 r--p 00000000 08:01 8257622
/home/r4g3/Desktop/2U/final 00601000-00602000 rw-p 00001000 08:01 8257622 /home/r4g3/Desktop/2U/final 022d0000-02302000 rw-p 00000000 00:00 0
[heap] 7facc4000000-7facc4021000 rw-p 00000000 00:00 0 7facc4021000-7facc8000000 ---p 00000000 00:00 0 7faccb8e5000-7faccb8fb000 r-xp 00000000 08:01 39326162
/lib/x86_64-linux-gnu/libgcc_s.so.1 7faccb8fb000-7faccbafa000 ---p 00016000 08:01 39326162
/lib/x86_64-linux-gnu/libgcc_s.so.1 7faccbafa000-7faccbafb000 rw-p 00015000 08:01 39326162
/lib/x86_64-linux-gnu/libgcc_s.so.1 7faccbafb000-7faccbc03000 r-xp 00000000 08:01 39321605
/lib/x86_64-linux-gnu/libm-2.23.so 7faccbc03000-7faccbe02000 ---p 00108000 08:01 39321605
/lib/x86_64-linux-gnu/libm-2.23.so 7faccbe02000-7faccbe03000 r--p 00107000 08:01 39321605
/lib/x86_64-linux-gnu/libm-2.23.so 7faccbe03000-7faccbe04000 rw-p 00108000 08:01 39321605
/lib/x86_64-linux-gnu/libm-2.23.so 7faccbe04000-7faccbfc4000 r-xp 00000000 08:01 39321685
/lib/x86_64-linux-gnu/libc-2.23.so 7faccbfc4000-7faccc1c4000 ---p 001c0000 08:01 39321685
/lib/x86_64-linux-gnu/libc-2.23.so 7faccc1c4000-7faccc1c8000 r--p 001c0000 08:01 39321685
/lib/x86_64-linux-gnu/libc-2.23.so 7faccc1c8000-7faccc1ca000 rw-p 001c4000 08:01 39321685
/lib/x86_64-linux-gnu/libc-2.23.so 7faccc1ca000-7faccc1ce000 rw-p 00000000 00:00 0 7faccc1ce000-7faccc340000 r-xp 00000000 08:01 13631793
/usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.21 7faccc340000-7faccc540000 ---p 00172000 08:01 13631793
/usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.21 7faccc540000-7faccc54a000 r--p 00172000 08:01 13631793
/usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.21 7faccc54a000-7faccc54c000 rw-p 0017c000 08:01 13631793
/usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.21 7faccc54c000-7faccc550000 rw-p 00000000 00:00 0 7faccc550000-7faccc576000 r-xp 00000000 08:01 39321617
/lib/x86_64-linux-gnu/ld-2.23.so 7faccc74f000-7faccc755000 rw-p 00000000 00:00 0 7faccc774000-7faccc775000 rw-p 00000000 00:00 0 7faccc775000-7faccc776000 r--p 00025000 08:01 39321617
/lib/x86_64-linux-gnu/ld-2.23.so 7faccc776000-7faccc777000 rw-p 00026000 08:01 39321617
/lib/x86_64-linux-gnu/ld-2.23.so 7faccc777000-7faccc778000 rw-p 00000000 00:00 0 7ffd85b05000-7ffd85b26000 rw-p 00000000 00:00 0
[stack] 7ffd85b97000-7ffd85b9a000 r--p 00000000 00:00 0
[vvar] 7ffd85b9a000-7ffd85b9c000 r-xp 00000000 00:00 0
[vdso] ffffffffff600000-ffffffffff601000 r-xp 00000000 00:00 0
[vsyscall] 10Aborted
P.S: "r" is my global root variable