Thread: Linked List & Seg Fault

  1. #1
    Registered User lord's Avatar
    Join Date
    Dec 2006
    Posts
    61

    Linked List & Seg Fault

    CODE: http://pastebin.com/d685a1173

    I can't seem to figure out why head==NULL is never true (line 58) and why line 74 produces a seg. fault...

    thanks in advance for any help...

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    The member "head" is never initialized in poly2 and thus causes a seg fault.
    The int coeff, int expon) constructor never initializes the members; only the default constructor does.
    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.

  3. #3
    Registered User lord's Avatar
    Join Date
    Dec 2006
    Posts
    61
    Is there anyway I can make the default constructor initialize the members for all the overloaded functions?

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Is there anyway I can make the default constructor initialize the members for all the overloaded functions?
    You need to fix the other constructor instead.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User lord's Avatar
    Join Date
    Dec 2006
    Posts
    61
    I fixed it as follows: http://pastebin.com/d3542c5db

    What's the main thing I am doing wrong?

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    You didn't fix it.
    Member "head" is still never initialized in the int, int constructor.
    Try applying a little logic to the problem.
    What are you expecting will happen?
    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.

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    To begin with, the correct way to initialise the member variables in the default constructor is by using an initialisation list:
    Code:
    polyType::polyType() : head(NULL), currNode(NULL), currPtr(NULL) {}
    Now, in your constructor that takes two int arguments, head must surely be either NULL or uninitialised, so I am not sure why you check if head is equal to NULL. You can still initialise head to NULL using the initialisation list if you want, but there is no need to check if it is NULL.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. linked list question
    By mikeman in forum C Programming
    Replies: 1
    Last Post: 11-30-2008, 01:56 PM
  2. Duplicating value of pointer to linked list
    By zephyrcat in forum C Programming
    Replies: 14
    Last Post: 01-22-2008, 03:19 PM
  3. singly linked to doubly linked
    By jsbeckton in forum C Programming
    Replies: 10
    Last Post: 11-06-2005, 07:47 PM
  4. Linked List
    By jpipitone in forum C Programming
    Replies: 4
    Last Post: 03-30-2003, 09:27 PM
  5. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM