简体   繁体   English

使用C中的链接列表实现Eratosthenes的筛选(分段错误)

[英]Implementing Sieve of Eratosthenes using Linked List in C (Segmentation Fault error)

I'm attempting to implement the Sieve algorithm in C for a class. 我正在尝试为一个类在C中实现Sieve算法。 I'm not asking for this assignment to be done for me. 我不是要为我完成这项任务。 I've got my functions written out already, but am currently getting a Segmentation Fault error. 我已经写出了函数,但是当前遇到了Segmentation Fault错误。 I'm not 100% sure what that is. 我不是100%知道那是什么。 Here is my code so far, can anyone see where this error comes from? 到目前为止,这是我的代码,任何人都可以看到此错误的来源吗?

#define EXTERN

#include <stdio.h>
#include <stdlib.h>
#include "header.h"

void clearAll() {
    int i, j;
    seg *p;
    p = head;
    for(i = 0; i < NSegs; i++) {
            p = p -> next;
            for(j = 0; j < 256; j++) {
                    p -> bits[j] = 0;
            }
    }
}
int setBit(int n) {
   int segment, index, hold, pos, i;
   seg *p;
   p = head;

   segment = n/256;
   hold = n;
   while(hold > 65) {
        hold = hold - 65;
        index++;
   }
   pos = (hold - 1)/2;

   for(i = 0; i < segment; i++) {
        p = p -> next;
        p->bits[index] = p->bits[index] | (1 << pos);
   }
}

    int testBitIs0(int n) {
   int segment, index, hold, pos, i, r;
   seg *p;
   p = head;
   segment = n/256;
   hold = n;
   while(hold > 65) {
        hold = hold - 65;
        index++;
   }
   pos = (hold - 1)/2;
   printf("%d, %d, %d ", segment, index, pos);
   for(i = 0; i < segment; i++) {
         p = p -> next;
        r = p->bits[index] & (1 << pos);
    }
}

void sieveOfE( int N )
{
   int i, j, k;


   k = 1;   // Start with 2 to find all primes

   while ( k <= N )
   {
        for ( i = k; i <= N; i++){
          if(i % 2 == 0) {
                break;
           }
          if ( testBitIs0(i) ){
              break;
          }
        }

        for ( j = 2*i; j <= N; j = j + i ){
          setBit(j);
         }
         k = i+1;
    } 
 }

int countPrimes(int n){
    int count, i;
    count = 0;
    for(i = 0; i <= n; i++) {
       if(testBitIs0(i) ){
            count++;
       }
    }
    return count;
}

int printPrimes(int n){
        int i;
        for(i = 0; i <= n; i++) {
            if(testBitIs0(i) ){
                printf("%d ", i);
           }
        }
        printf("\n\n");

} 

The Linked List has already been properly initialized in the main and header files. 链接列表已在主文件和头文件中正确初始化。 The initialization came from a skeleton file and should not be edited. 初始化来自框架文件,不应进行编辑。 But each linked list node contains a bit array and a pointer to the next node. 但是每个链接列表节点都包含一个位数组和一个指向下一个节点的指针。

Give a programmer a fix for a segfault and you'll feed him for a day. 给程序员修复段错误的方法,您就可以养活他一天。 Teach a programmer to use a debugger and he'll feed himself for a lifetime. 教程序员使用调试器,他将终身供职。

If you run your program under a debugger, it will trap the segfault crash at the line of code which caused it and you can examine the call stack. 如果在调试器下运行程序,它将在导致该错误的代码行中捕获segfault崩溃,并且您可以检查调用堆栈。 If you use the debugger, the bt or backtrace command will display your stack. 如果使用调试器,则btbacktrace命令将显示您的堆栈。

Here is a GDB turorial . 这是GDB的花絮

As pointed out in the comments, segfault generally occurs when you try to dereference an invalid pointer, be it uninitialized, corrupted or wrong for any number of reasons. 正如评论中指出的那样,当您尝试取消引用无效的指针(由于多种原因未初始化,损坏或错误)时,通常会发生段错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM