struct node pointer dereference printing bug/difficulty

This is a discussion on struct node pointer dereference printing bug/difficulty within the C++ Programming forums, part of the General Programming Boards category; I'm doing a file reading. The contents of the file is saved into a subject node called head. In order ...

  1. #1
    Registered User
    Join Date
    Jul 2012
    Location
    physical layer
    Posts
    2

    struct node pointer dereference printing bug/difficulty

    I'm doing a file reading. The contents of the file is saved into a subject node called head. In order to perform dynamic node creation, I equated the temp = *head (on line 48). Upon printing, calling print(head) on line 34, the program crashed.

    I debugged it and found a segmentation fault on printing so I added line 50 and 51 for manual debugging. The contents of ctemp can be printed, but not the original node *head. Clearly it is not updated.

    What's the problem with line 48? Why can't the value of head be changed? What do you think am I doing wrong?

    *added txt && cpp attachments
    Code:
    #include <iostream>
    #include <fstream>
    #include <cstring>
    #include <cstdlib>
    using namespace std;
    
    typedef struct cell {
        int row, bit;
        struct cell *next;
    }cell;
    typedef struct course{
        int col;
        char name[3];
        struct cell* rcell;
        struct course *next;
    }course;
    typedef struct subject{
        int col;
        char name[3];
        struct student* student;
        struct subject *next;
    }subject;
    typedef struct student{
        int index;
        char name[3];
        struct student *next;
    }student;
    void readfile(subject**);
    void print(subject*);
    void _free(subject**);
    int main(void) {
        subject* head = NULL;
        readfile(&head);
        print(head);
        _free(&head);
        free(head);
    }
    
    void readfile(subject** head) {
        int column = 0, row = 0, ctotal = 0;
        char cname[3], sname[3], c;
        ifstream ifile("schedule.txt");
        subject *ctemp = NULL;
        student *stemp = NULL;
        if(ifile.is_open()){
            ctemp = *head;
            while(ifile >> cname) {
                ctemp = new subject;
                strcpy(ctemp->name, cname);
                cout << "\nctemp " << ctemp->name << " ";
                cout << "\nhead "<< (*head)->name << " ";
                ctemp->col = column++;
                stemp = ctemp->student;
                while(ifile >> sname) {
                    c = ifile.get();
                    if(c == '\n') break;
                    stemp = new student;
                    stemp->index = row++;
                    strcpy(stemp->name, sname);
                    cout << stemp->name << " ";
                    stemp->next = NULL;
                    stemp = stemp->next;
                }
                row = 0;
                ctemp->next = NULL;
                ctemp = ctemp->next;
                ctotal++;
            }
        } else {
            cout << "\nfile error";
        }
        ifile.close();
    }
    
    void print(subject *head) {
        while(head) {
            cout << endl << head->name << " ";
            while(head->student) {
                cout << head->student->name << " ";
                head->student = head->student->next;
            }
            head = head->next;
        }
    }
    void _free(subject** head) {
        subject* ctemp = NULL;
        student* stemp = NULL;
        while(*head) {
            ctemp = *head;
            while((*head)->student) {
                    stemp = ctemp->student;
                    (*head)->student = (*head)->student->next;
                    stemp->next = NULL;
                    free(stemp);
            }
            *head = (*head)->next;
            ctemp->next = NULL;
            free(ctemp);
        }
    }
    Attached Files Attached Files

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,742
    Well first of all, to store "ABC", you need at least 4 elements in your char arrays.
    And since this is C++, you should be using std::string in place of char arrays.

    The second point is that you must NOT call free() on objects allocated with new.
    malloc / calloc / realloc -> free
    new -> delete
    new[ ] -> delete [ ]
    Never mix and match allocators and deallocators.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Jul 2012
    Location
    physical layer
    Posts
    2
    Quote Originally Posted by Salem View Post
    Well first of all, to store "ABC", you need at least 4 elements in your char arrays.
    And since this is C++, you should be using std::string in place of char arrays.

    The second point is that you must NOT call free() on objects allocated with new.
    malloc / calloc / realloc -> free
    new -> delete
    new[ ] -> delete [ ]
    Never mix and match allocators and deallocators.
    Thanks for the heads up, I tend to mix things up all the time. I changed the size to 4 and tried strings(though discouraging due to memory size).

    I don't think these have impact on the code flow. Still I am having the same error SIGSEGV segmentation fault on that line.

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,742
    Use a debugger!
    Code:
    $ g++ -W -Wall -g foo.cpp
    $ gdb ./a.out 
    GNU gdb (Ubuntu/Linaro 7.3-0ubuntu2) 7.3-2011.08
    Copyright (C) 2011 Free Software Foundation, Inc.
    License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
    This is free software: you are free to change and redistribute it.
    There is NO WARRANTY, to the extent permitted by law.  Type "show copying"
    and "show warranty" for details.
    This GDB was configured as "x86_64-linux-gnu".
    For bug reporting instructions, please see:
    <http://bugs.launchpad.net/gdb-linaro/>...
    Reading symbols from /home/sc/Documents/a.out...done.
    (gdb) run
    Starting program: /home/sc/Documents/a.out 
    
    ctemp CO1 
    
    Program received signal SIGSEGV, Segmentation fault.
    __strlen_sse42 () at ../sysdeps/x86_64/multiarch/strlen-sse4.S:32
    32	../sysdeps/x86_64/multiarch/strlen-sse4.S: No such file or directory.
    	in ../sysdeps/x86_64/multiarch/strlen-sse4.S
    (gdb) where
    #0  __strlen_sse42 () at ../sysdeps/x86_64/multiarch/strlen-sse4.S:32
    #1  0x00007ffff7b6d801 in std::basic_ostream<char, std::char_traits<char> >& std::operator<< <std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*) () from /usr/lib/x86_64-linux-gnu/libstdc++.so.6
    #2  0x0000000000400e7c in readfile (head=0x7fffffffe188) at foo.cpp:51
    #3  0x0000000000400d30 in main () at foo.cpp:33
    (gdb) frame 2
    #2  0x0000000000400e7c in readfile (head=0x7fffffffe188) at foo.cpp:51
    51				cout << "\nhead "<< (*head)->name << " ";
    (gdb) print head
    $1 = (subject **) 0x7fffffffe188
    (gdb) print *head
    $2 = (subject *) 0x0
    (gdb)
    head doesn't point anywhere yet.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    22,887
    Start using references...
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. problem with dual dereference of a struct pointer
    By dan_paul in forum C Programming
    Replies: 5
    Last Post: 11-12-2011, 09:13 PM
  2. null pointer dereference
    By qwertylurker in forum C Programming
    Replies: 3
    Last Post: 03-14-2011, 01:06 AM
  3. Suspicious dereference of pointer
    By subhashish1213 in forum C++ Programming
    Replies: 5
    Last Post: 09-10-2009, 09:19 AM
  4. Pointer dereference
    By taurus in forum C Programming
    Replies: 1
    Last Post: 11-09-2008, 07:41 AM
  5. Dereference pointer to void pointer to member
    By phil in forum C Programming
    Replies: 5
    Last Post: 04-20-2005, 12:54 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21