-1

I have a program that returns an error after I've compiled it in g++, then run it in a Unix environment. The error is that my program says 'Segmentation error' before it can do anything. This error is occurring when I try to set a new nodes data pointer equal to something. I know this because when I test this code in Visual Studio when I try to check if(curr->data == ch); with curr being a pointer, data being the char element that the list is made up of, and ch being the char passed into the bool LinkedList::find(char ch) function, Visual Studio breaks (stops) at that line of code. For context, here's part of my header file (with if(curr->data == ch); towards the end):

#include <ostream>

class LinkedList
{
public:
        LinkedList();
        ~LinkedList();

        bool find(char ch);
private:
    struct node
    {
            node();
            char data;
            node * next;
    };
    node * head;
    node * curr;
    node * prev;
};
LinkedList::LinkedList() : head(nullptr), curr(nullptr), prev(nullptr);
LinkedList::node::node() : data('\0'), next(nullptr);
LinkedList::~LinkedList()
{
    if (!head) // head is null and so list is empty
    {
            return; //nothing to delete
    }

    for(curr = head; head; /* head isn't NULL*/ delete curr /*delete first element*/)
    {
            curr = head;  // set curr to head of list
            head = curr->next;  // move head over to next element (or make it null)
    }
}
bool LinkedList::find(char ch)
{
        if(head)
        {
                for(curr = head; curr && curr->data != ch; curr = curr->next);
                if(curr->data == ch)
                {
                        //std::cout << "'" << ch << "' is in the list." << std::endl;
                        return true;
                }
                else
                {
                        //std::cout << "'" << ch << "' isn't in the list." << std::endl;
                        return false;
                }
                //std::cout << "The list is empty" << std::endl;
                return false;
        }
        else
        {
                //std::cout << "The list is empty" << std::endl;
                return false;
        }
}

I wish I could give you guys more context, but I have no idea how to fix this. I thought a char and a pointer to a char were the same type; after all, Visual Studio doesn't break when the for loop checks: curr->data != ch;.

Larrimus
  • 211
  • 1
  • 2
  • 9
  • A `char` is not the same as a pointer. But `curr->data` is not a pointer, it's a `char`. Why do you think it's a pointer? – melpomene Oct 01 '15 at 21:12
  • 1
    @melpomene Sorry, I ran the program in a Unix environment. Also, this error just made me assume that `curr->data` and `char` must be different types. – Larrimus Oct 01 '15 at 21:13
  • 1
    @ melpomene In my Unix environment, the program says Segmentation fault. – Larrimus Oct 01 '15 at 21:17
  • OK, so what happened when you ran the debugger? Also, how are we supposed to find a bug in your code without seeing your code? – melpomene Oct 01 '15 at 21:18
  • How did you get from "Segmentation fault" to "these types must be different"? – melpomene Oct 01 '15 at 21:18
  • 1
    @melpomene I simply assumed that the types must be different without any evidence. The program compiled fine, so the debugger in Unix said nothing. The debugger in Visual studio just said "Unhandled exception at 0x013A63A9 in program.exe: 0xC0000005: Access violation reading location 0X00000000." and stopped at the line of the code I specified. – Larrimus Oct 01 '15 at 21:27
  • That error message says "You just tried to read from where a null pointer points" - reading location 0 is a fairly big clue. – Alan Stokes Oct 01 '15 at 21:29
  • Also: always include the full error message in the question. – Alan Stokes Oct 01 '15 at 21:32
  • 1
    "*The program compiled fine, so the debugger in Unix said nothing.*" - This makes no sense. If it didn't compile, you wouldn't have an executable to debug. You can only debug programs that compile. – melpomene Oct 01 '15 at 21:32
  • 1
    @melpomene I know, but as an inexperienced programmer, I was trying to be pedantic as it seemed that I needed to be for people to understand me on this site. – Larrimus Oct 01 '15 at 21:37
  • "Pedantic" does not mean "wrong". – melpomene Oct 01 '15 at 21:39

1 Answers1

1
for(curr = head; curr && curr->data != ch; curr = curr->next);

has two exit conditions. One is you find a node with ch. The other is curr == NULL. The next line after the loop exits

if(curr->data == ch)

tests the data member of a curr that might be NULL. Tears in rain... Time to die....

In order to test data, the program first has to find and read it. Finding it is easy, it is at most a few bytes at best after memory address 0, but reading it is problematic. Your program does not own address 0 and cannot read it, triggering an access violation.

By convention, the first chunk of memory is reserved and invalid to make trapping invalid accesses like this one easy. Rather than returning a garbage answer and appearing to function, the program typically crashes. But it might not if convention is not followed.

user4581301
  • 33,082
  • 7
  • 33
  • 54
  • 1
    If it were NULL, wouldn't the check return false? If not, can you explain why that isn't the case. I would have assumed if `curr->data` was equal to `NULL` it wouldn't be equal to `ch`. – Larrimus Oct 01 '15 at 21:29
  • 1
    Nope. It would be a null pointer access and give you a message a lot like "Access violation reading location 0X00000000" – user4581301 Oct 01 '15 at 21:29
  • 1
    Thank you for the help. I know I seem like an idiot, that's because I am when it comes to programming. I've taken several C++ classes (which I've done well in) and yet I'm still terrible at it. – Larrimus Oct 01 '15 at 21:33
  • @Larrimus Programming isn't everything. I know some excellent architects, folks who can lay out logic and program hierarchies like gods, but can't be trusted to write usable code. They are rare though, and it's smurfing hard to work your way up from the trenches without solid coding skills, so I recommend practice and [reading a few good books](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). – user4581301 Oct 01 '15 at 21:40