Note that there are some explanatory texts on larger screens.

plurals
  1. POWrong values stored and other
    text
    copied!<p>I made this program:<br></p> <p><strong>llist:</strong></p> <pre><code>typedef struct linked_list { int val; int n; struct linked_list *next; }llist; </code></pre> <p><strong>addelem function:</strong></p> <pre><code>void addelem(llist *list,int val) { llist *tmp=(llist *)malloc(sizeof(llist)); //create new element tmp-&gt;val=val; //assign value to tmp tmp-&gt;next=NULL; //set next to NULL // printf("addelem entered\n"); if(list-&gt;next!=NULL) //if this is not the last element { addelem(list-&gt;next,val); //recursion } else { tmp-&gt;n=list-&gt;n + 1; list-&gt;next=(llist *)malloc(sizeof(llist)); //allocate memory list-&gt;next=tmp; //add to list } // printf("addelem exited\n"); free(tmp); } </code></pre> <p><strong>remelem function:</strong></p> <pre><code>void remelem(llist *list) { int f=0; if(list-&gt;n==0 &amp;&amp; list-&gt;next==NULL) goto dontremove; if(1) { if(list-&gt;next!=NULL) { if(list-&gt;next-&gt;next==NULL) { f=1; } remelem(list-&gt;next); } else { free(list); } if(f==1) { list-&gt;next=NULL; } } else { dontremove: printf("cant be removed even if the next message is 'Element removed.'\n"); } //end } </code></pre> <p><strong>show function:</strong> </p> <pre><code>void show(llist *list) { printf("Element number: %d\nElement Value: %d\n",list-&gt;n,list-&gt;val); if(list-&gt;next!=NULL) { show(list-&gt;next); } else { printf("\nTotal number of elems: %d\n",list-&gt;n);; } } </code></pre> <p><strong>main function:</strong></p> <pre><code>int main() { int i,n,ch; llist *list=(llist *)malloc(sizeof(llist)); list-&gt;next=NULL; list-&gt;n=0; list-&gt;val=0; menu: //menu label printf("1.Add element.\n"); printf("2.Remove element.\n3.Show elements\n0.Exit.\n"); scanf("%d",&amp;ch); switch(ch) { case 1: printf("\nEnter value.\n"); scanf("%d",&amp;n); addelem(list,n); printf("Element added\n"); break; case 2: remelem(list); printf("\nElement removed.\n"); break; case 3: show(list); break; case 0: goto end; //end break; } goto menu; //show menu again end: //end label return 0; } </code></pre> <p>Output:</p> <pre><code>1.Add element. 2.Remove element. 3.Show elements 0.Exit. 1 Enter value. 22 Element added 1.Add element. 2.Remove element. 3.Show elements 0.Exit. 1 Enter value. 4 Element added 1.Add element. 2.Remove element. 3.Show elements 0.Exit. 2 Element removed. 1.Add element. 2.Remove element. 3.Show elements 0.Exit. 3 Element number: 0 Element Value: 0 Element number: 1 Element Value: 161701936 Total number of elems: 1 </code></pre> <p>the values are different from the ones I typed,. My first question is that why does this happen?(probably a silly mistake) and how can i fix it to print correct value.</p> <p><br> <br> and here is another output:</p> <pre><code>1.Add element. 2.Remove element. 3.Show elements 0.Exit. 1 Enter value. 22 Element added 1.Add element. 2.Remove element. 3.Show elements 0.Exit. 1 Enter value. 23 Element added 1.Add element. 2.Remove element. 3.Show elements 0.Exit. 2 Element removed. 1.Add element. 2.Remove element. 3.Show elements 0.Exit. 1 Enter value. 22 Segmentation fault (core dumped) </code></pre> <p>In this, first I add an element, then I remove it,but if I add the element again I get segmentation fault, Can anyone explain why does this happen and how to fix it?.</p> <p>And if at first I add an element and I remove it, then this is what I get:</p> <pre><code>*** glibc detected *** ./llist: double free or corruption (fasttop): 0x0855b018 *** ======= Backtrace: ========= certain memory addresses ======= Memory map: ======== 08048000-08049000 r-xp 00000000 08:01 1969473 /path/to/this/prog 08049000-0804a000 r--p 00000000 08:01 1969473 /path/to/this/prog 0804a000-0804b000 rw-p 00001000 08:01 1969473 /path/to/this/prog 0855b000-0857c000 rw-p 00000000 00:00 0 [heap] b758e000-b75aa000 r-xp 00000000 08:01 3802036 /lib/i386-linux-gnu/libgcc_s.so.1 b75aa000-b75ab000 r--p 0001b000 08:01 3802036 /lib/i386-linux-gnu/libgcc_s.so.1 b75ab000-b75ac000 rw-p 0001c000 08:01 3802036 /lib/i386-linux-gnu/libgcc_s.so.1 b75c3000-b75c4000 rw-p 00000000 00:00 0 b75c4000-b7767000 r-xp 00000000 08:01 3805265 /lib/i386-linux-gnu/libc-2.15.so b7767000-b7768000 ---p 001a3000 08:01 3805265 /lib/i386-linux-gnu/libc-2.15.so b7768000-b776a000 r--p 001a3000 08:01 3805265 /lib/i386-linux-gnu/libc-2.15.so b776a000-b776b000 rw-p 001a5000 08:01 3805265 /lib/i386-linux-gnu/libc-2.15.so b776b000-b776e000 rw-p 00000000 00:00 0 b7782000-b7787000 rw-p 00000000 00:00 0 b7787000-b7788000 r-xp 00000000 00:00 0 [vdso] b7788000-b77a8000 r-xp 00000000 08:01 3805277 /lib/i386-linux-gnu/ld-2.15.so b77a8000-b77a9000 r--p 0001f000 08:01 3805277 /lib/i386-linux-gnu/ld-2.15.so b77a9000-b77aa000 rw-p 00020000 08:01 3805277 /lib/i386-linux-gnu/ld-2.15.so bf969000-bf98a000 rw-p 00000000 00:00 0 [stack] Aborted (core dumped) </code></pre> <p>can anyone please tell why these errors have occured?</p> <p>I use Ubuntu 12.04</p> <p>Thank You</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload