{"id":5727,"date":"2014-04-06T10:29:57","date_gmt":"2014-04-06T10:29:57","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2014\/04\/06\/implementing-sieve-of-eratosthenes-using-linked-list-in-c-segmentation-fault-error-collection-of-common-programming-errors\/"},"modified":"2014-04-06T10:29:57","modified_gmt":"2014-04-06T10:29:57","slug":"implementing-sieve-of-eratosthenes-using-linked-list-in-c-segmentation-fault-error-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2014\/04\/06\/implementing-sieve-of-eratosthenes-using-linked-list-in-c-segmentation-fault-error-collection-of-common-programming-errors\/","title":{"rendered":"Implementing Sieve of Eratosthenes using Linked List in C (Segmentation Fault error)-Collection of common programming errors"},"content":{"rendered":"<p>I&#8217;m attempting to implement the Sieve algorithm in C for a class. I&#8217;m not asking for this assignment to be done for me. I&#8217;ve got my functions written out already, but am currently getting a Segmentation Fault error. I&#8217;m not 100% sure what that is. Here is my code so far, can anyone see where this error comes from?<\/p>\n<pre><code>#define EXTERN\n\n#include \n#include \n#include \"header.h\"\n\nvoid clearAll() {\n    int i, j;\n    seg *p;\n    p = head;\n    for(i = 0; i &lt; NSegs; i++) {\n            p = p -&gt; next;\n            for(j = 0; j &lt; 256; j++) {\n                    p -&gt; bits[j] = 0;\n            }\n    }\n}\nint setBit(int n) {\n   int segment, index, hold, pos, i;\n   seg *p;\n   p = head;\n\n   segment = n\/256;\n   hold = n;\n   while(hold &gt; 65) {\n        hold = hold - 65;\n        index++;\n   }\n   pos = (hold - 1)\/2;\n\n   for(i = 0; i &lt; segment; i++) {\n        p = p -&gt; next;\n        p-&gt;bits[index] = p-&gt;bits[index] | (1  65) {\n        hold = hold - 65;\n        index++;\n   }\n   pos = (hold - 1)\/2;\n   printf(\"%d, %d, %d \", segment, index, pos);\n   for(i = 0; i &lt; segment; i++) {\n         p = p -&gt; next;\n        r = p-&gt;bits[index] &amp; (1<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>I&#8217;m attempting to implement the Sieve algorithm in C for a class. I&#8217;m not asking for this assignment to be done for me. I&#8217;ve got my functions written out already, but am currently getting a Segmentation Fault error. I&#8217;m not 100% sure what that is. Here is my code so far, can anyone see where [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-5727","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/5727","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=5727"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/5727\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=5727"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=5727"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=5727"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}