C++ Program crashes after asking for input from user

0

Ok so I have a program here that works fine mostly except for when the insertion function is called the user is asked for input. As soon as I get to this part of the code the program completely exits(stopped responding).

From debugging I get the error: Unhandled exception at 0x00C06C39 in ConsoleApplication8.exe: 0xC0000005: Access violation reading location 0x00000004.

int insertion(container** pointerToHead) {
    int i = 0;
    class container *newNode = NULL, *iterator = NULL, *follower = NULL;
    class person *newPerson = NULL;
    newNode = (class container*) malloc(sizeof(class container));
    if (newNode = NULL) {
        cout << "Fatal Error: Out of Memory. Exiting now." << endl;
        return 0;
    }
    else {
        newPerson = (class person*) malloc(sizeof(class person));
        if (newPerson = NULL) {
            cout << "Fatal Error: Out of Memory. Exiting now." << endl;
            return 0;
        }
        else {
            cout << "Enter the name:\n";
            cin >> newPerson->name;
            cout << "Enter the phone number:\n";
            cin >> newPerson->phone, sizeof(newPerson->phone);
            cout << "Enter the email:\n";
            cin >> newPerson->email;
            newNode->plink = newPerson;
            if (*pointerToHead = NULL) {
                *pointerToHead = newNode;
                (*pointerToHead)->next = NULL;
                return 0;
            }
            else {
                if (strcmp(newPerson->name, (*pointerToHead)->plink->name) < 0) {
                    newNode->next = *pointerToHead;
                    *pointerToHead = newNode;
                    return 0;
                }
                iterator = *pointerToHead;
                follower = iterator;
                while (iterator != NULL) {
                    if (strcmp(newPerson->name, iterator->plink->name) < 0) {
                        newNode->next = iterator;
                        follower->next = newNode;
                        return 0;
                    }
                    follower = iterator;
                    iterator = iterator->next;
                }
                follower->next = newNode;
                newNode->next = NULL;
                return 0;
            }
        }
    }
    return 0;
};

The debugging told me that the error starts from the line containing: cin >> newPerson->name;

This is my first program in which I use C++ so I am not very familiar with the language(I have been using C before this assignment). Any help as to why I keep getting an error would be appreciated.

c++
linked-list
asked on Stack Overflow Mar 29, 2015 by Rakeen Huq

1 Answer

3

I've found at least one issue. Take a look at this snippet:

if (newNode = NULL) {
        cout << "Fatal Error: Out of Memory. Exiting now." << endl;
        return 0;
    }
else {
    ...

Notice anything wrong? You used the assignment operator = instead of the comparison operator ==. So newNode is set to NULL. This evaluates to false, so the body of the if statement is skipped in favor of the else statement. Now, if you attempt to dereference newNode (which you do), you'll get a segmentation fault because newNode points to invalid memory.

answered on Stack Overflow Mar 29, 2015 by Will Resond

User contributions licensed under CC BY-SA 3.0