Note that there are some explanatory texts on larger screens.

plurals
  1. POGetting Segmentation Fault in C program
    text
    copied!<p>I am getting segmentation fault on line 8 in the code below.</p> <pre><code>typedef struct _my_struct { int pArr[21]; int arr1[8191]; int arr2[8191]; int m; int cLen; int gArr[53]; int dArr[8191]; int data[4096]; int rArr[53]; int eArr[1024]; }; void *populate_data(void *arg) { 1 register int mask =1, iG; 2 struct _my_struct *var ; 3 var = arg; // arg is passed as initialized struct variable while creating thread 4 var-&gt;m = 13; 5 var-&gt;arr2[var-&gt;m] = 0; 6 for (iG = 0; iG &lt; var-&gt;m; iG++) { 7 var-&gt;arr2[iG] = mask; 8 var-&gt;arr1[var-&gt;arr2[iG]] = iG; 9 if (var-&gt;pArr[iG] != 0) // pArr[]= 1011000000001 10 var-&gt;arr2[var-&gt;m] ^= mask; 11 mask &lt;&lt;= 1; 12 } 13 var-&gt;arr1[var-&gt;arr2[var-&gt;m]] = var-&gt;m; 14 mask &gt;&gt;= 1; 15 for (iG = var-&gt;m+ 1; iG &lt; var-&gt;cLen; iG++) { 16 if (var-&gt;arr2[iG - 1] &gt;= mask) 17 var-&gt;arr2[iG] = var-&gt;arr2[var-&gt;m] ^ ((var-&gt;arr2[iG- 1] ^ mask) &lt;&lt; 1); 18 else 19 var-&gt;arr2[iG] = var-&gt;arr2[iG- 1] &lt;&lt; 1; 20 var-&gt;arr1[var-&gt;arr2[iG]] = iG; 21 } 22 var-&gt;arr1[0] = -1; } </code></pre> <p>Here is the thread function:</p> <pre><code>void main() { unsigned int tid; struct _my_struct *instance = NULL; instance = (struct _my_struct *)malloc(sizeof(_my_struct )); start_thread(&amp;tid , 119312, populate_data, instance ); } int start_thread(unsigned int *tid, int stack_size, void * (*my_function)(void *), void *arg) { pthread_t ptid = -1; pthread_attr_t pattrib; pthread_attr_init(&amp;pattrib); if(stack_size &gt; 0) { pthread_attr_setstacksize(&amp;pattrib, stack_size); } else { pthread_attr_destroy(&amp;pattrib); return -1; } pthread_create(&amp;ptid, &amp;pattrib, my_function, arg); pthread_attr_destroy(&amp;pattrib); return 0; } </code></pre> <p>Once I debug it through gdb, got this error, </p> <pre><code>Program received signal SIGSEGV, Segmentation fault. [Switching to Thread 0x7ffdfec80700 (LWP 22985)] 0x0000000000401034 in populate_data (arg=0x7fffffffe5d8) at Queue.c:19 19 var-&gt;arr1[var-&gt;arr2[iG]] = iG; </code></pre> <p>and its backtrace is:</p> <pre><code>#0 0x0000000000401034 in populate_data (arg=0x7fffffffe5d8) at Queue.c:159 #1 0x00007ffff7bc6971 in start_thread () from /lib/libpthread.so.0 #2 0x00007ffff792292d in clone () from /lib/libc.so.6 #3 0x0000000000000000 in ?? () </code></pre> <p>However, I'm unable to correct the error.</p> <p>Anyhelp is really appreciated.</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