Thread: Segmentation fault!

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #13
    Registered User
    Join Date
    Nov 2011
    Posts
    69
    Code:
    typedef struct books {
        char title[TITLE_LEN];
        double price;
        struct books *nxt_price;
        struct books *nxt_title;
    } booksT;
    
    
         // Each line is scanned, then 'insert' is called.     
          for (i = 0, pos = 0; (feof(f) == 0); i++) {
            
            fseek(f, pos, SEEK_CUR);
            fscanf(f, "%s %lf", temp_title, &temp_price);
            insert(titles, prices, temp_title, temp_price);
            pos++;
        }
    
        //New node is created and inserted.
    void insert (booksT *titles[26], booksT *prices[10], char tmp_title[], double tmp_price) {
        booksT *newnode, *curr, *prev;
        int index;
        
        newnode = (booksT*)malloc(sizeof(booksT));
        if (newnode == NULL) {
            printf("Memory allocation error.\n");
            exit(1);
        }
        
        strcpy(newnode->title, tmp_title);
        newnode->price = tmp_price;
        
        // Placement according to title.   
        index = newnode->title[0] - 'A';
        if (titles[index] == NULL) {
            titles[index] = newnode;
        }
        else {
            curr = (booksT*)malloc(sizeof(booksT));
            if (curr == NULL) {
                printf("Memory allocation error.\n");
                exit(1);
            }
            
            prev = (booksT*)malloc(sizeof(booksT));
            if (prev == NULL) {
                printf("Memory allocation error.\n");
                exit(1);
            }
            
            strcpy(curr->title, newnode->title);
            
            for (prev = NULL, curr = titles[index]; (curr->nxt_title != NULL) && (strcmp(curr->title, curr->nxt_title->title) < 0); prev = curr, curr = curr->nxt_title);
            
            curr->nxt_title = newnode;
            free(curr);
        }
        
        // Placement according to price.
        index = (int)newnode->price / 10;
        
        if (prices[index] == NULL) {
            prices[index] = newnode;
        }
        else {
            curr = (booksT*)malloc(sizeof(booksT));
            if (curr == NULL) {
                printf("Memory allocation error.\n");
                exit(1);
            }
            
            prev = (booksT*)malloc(sizeof(booksT));
            if (prev == NULL) {
                printf("Memory allocation error.\n");
                exit(1);
            }
            
            curr->price = newnode->price;
            
            for (prev = NULL, curr = prices[index]; (curr->nxt_price != NULL) && (curr->price < curr->nxt_price->price); prev = curr, curr = curr->nxt_price);
            
            curr->nxt_price = newnode;
            free(curr);
        }
    }
    I talked to my Programming teacher today and he still insisted on casting malloc so I'm going to go with that. This is the current progress of the code (the relevant part of it).
    "curr" (ex-"runner") is supposed to find exactly where the new node is supposed to be placed, and then with the use of "prev" which will point to the node before "curr", the new node must be inserted. I seem to be getting it wrong.
    Last edited by Xpl0ReRChR; 01-11-2012 at 11:07 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Segmentation fault
    By sirsmilealot in forum C Programming
    Replies: 12
    Last Post: 02-10-2010, 01:26 PM
  2. segmentation fault??
    By snappleapple in forum C Programming
    Replies: 9
    Last Post: 04-27-2007, 11:56 PM
  3. Segmentation Fault
    By warfang in forum C++ Programming
    Replies: 9
    Last Post: 04-23-2007, 01:42 AM
  4. segmentation fault when using gcc
    By stodd04 in forum C Programming
    Replies: 6
    Last Post: 02-14-2005, 07:34 PM
  5. segmentation fault and memory fault
    By Unregistered in forum C Programming
    Replies: 12
    Last Post: 04-02-2002, 11:09 PM