1

So from what I've read from other seg fault errors, it deals with accessing memory you shouldn't have access to (bad memory, or didn't use malloc()). For some reason there is no seg fault error when I make 2 nodes, but anything else gets a seg fault error. If someone one could please explain where I am going wrong in this program that would be extremely helpful. Thanks.

    #include "header.h"
    #include <stdlib.h>
    #include <stdio.h>
    #include <math.h>
    int main(int argc, char *argv[]) {

        int N; 
        //int oddsonly = (BITS_PER_SEG*2) - 1; 
        int NSegs;
        int numOfPrimes;

        if (argc == 2) sscanf(argv[1],"%d",&N);
            else scanf("%d",&N);
            NSegs = (int)ceil(N/(float)odds_only);
            printf("This is the number of segments made: %d\n", NSegs);
        //this is how we make a doubly linked list 
        //void makelinkedlist(){
        //this is how we make a doubly linked list 
        //void makelinkedlist(){
        int i; 
        seg *node; 
        seg *current;
        //head = (seg*)malloc(sizeof(seg));
        head = NULL;
        for(i = 1; i < NSegs; i++ ) { 
            if(i == 1) {
                node = (seg*)malloc(sizeof(seg)); 

                node->prev = NULL; 
                node->next = NULL;
                head = node;
            }//if
            else{
                current = (seg*)malloc(sizeof(seg)); 
                current = head; 
                while(current->next != NULL){
                    current = current->next; 
                }//while
                node = current->next; 
                node->prev = current; 
                node->next = NULL;    
                }//else
            }//for
        printf("Done allocating %d nodes\n",i); '

and my header file has this:

    #ifndef EXTERN
    #define EXTERN extern
    #endif

    #define SIZE_OF_SEG      256       // 256 int per segment
    #define odds_only   16383
    #define BITS_PER_SEG     (8*SIZE_OF_SEG*sizeof(int))


    typedef struct _seg {  /* definition of new type "seg" */
        int  bits[256];
        struct _seg  *next,*prev;        
          }seg  ;

    EXTERN int NSegs;

    EXTERN seg *head;             // This variable will point to the 
                          // start of the linked list of segments !!!

    //EXTERN void clearAll( );            // Uses head to find the "bit array" (list)
    EXTERN void sieveOfE( int N );      // Uses head to find the "bit array" (list)
    EXTERN int  countPrimes( int N );   // Uses head to find the "bit array" (list)
    EXTERN void printPrimes( int N );   // Uses head to find the "bit array" (list)
JGV
  • 5,037
  • 9
  • 50
  • 94
chris123
  • 197
  • 3
  • 13
  • Please, [Don't cast the return value of `malloc()`](http://stackoverflow.com/questions/605845/do-i-cast-the-result-of-malloc). And don't use `sscanf()` for that, use `strtol()` passing a non `NULL` parameter to `endptr` in order to check if the value is numeric or not. And check the return value of `scanf()` or your program could invoke *Undefined Behavior*. Don't arbitrarily cast `ceil()` to `int` read about the reason why it returns `float`. Check the returned pointer from `malloc()` for `NULL`. – Iharob Al Asimi Sep 19 '15 at 22:28

1 Answers1

1

In your for-loop, in the first iteration head is set to a new node. In the second iteration, something strange is done:

current = (seg*)malloc(sizeof(seg)); 
current = head; 

Now you've allocated some space, but you have overwritten the pointer to it (this is a memory leak). Afterwards it goes wrong:

while(current->next != NULL){
    current = current->next; 
}//while

node = current->next; 
node->prev = current; 

Here, node is set to NULL so you're writing into the null address, hence the core dump.

Michael D
  • 678
  • 3
  • 11
  • THANK YOU! so current->next is not pointing to anything since I haven't malloc()'ed the node. I added current->next = (seg*)malloc(sizeof(seg)); and I am not getting the mistake anymore, is this what you were hinting at or was this random luck. Thank you for your time and help. – chris123 Sep 19 '15 at 23:10