Linked List & Seg Fault

This is a discussion on Linked List & Seg Fault within the C++ Programming forums, part of the General Programming Boards category; 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 ...

  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をこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,167
    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.
    For information on how to enable C++11 on your compiler, look here.
    よく聞くがいい!私は天才だからね! ^_^

  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
    20,955
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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をこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,167
    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.
    For information on how to enable C++11 on your compiler, look here.
    よく聞くがいい!私は天才だからね! ^_^

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    20,955
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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, 12:56 PM
  2. Duplicating value of pointer to linked list
    By zephyrcat in forum C Programming
    Replies: 14
    Last Post: 01-22-2008, 02:19 PM
  3. singly linked to doubly linked
    By jsbeckton in forum C Programming
    Replies: 10
    Last Post: 11-06-2005, 06:47 PM
  4. Linked List
    By jpipitone in forum C Programming
    Replies: 4
    Last Post: 03-30-2003, 08: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

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