0

Background:
We are learning about linked lists and stacks in class. I'm having trouble understanding how/where to declare a new stack.

Problem:
My program works until the second time it needs to add a new node, and then it crashes and gives this error "Unhandled exception at... std::bad_alloc at memory location..."

After a few times debugging the program (with it crashing at that error) I'm not able to compile at all without restarting visual studios, which is pretty scary).

Code Snippets:

Source.cpp

    #include<iostream>
    #include<string>
    #include<fstream>
    #include"ListStack.h"
    using namespace std;

    void openInputFile(ifstream&);
    string delimiterMatching(ifstream&);


    void main() {

        ifstream inFile;
        openInputFile(inFile);
        string errorMessage = delimiterMatching(inFile);
        inFile.close();

    }

    void openInputFile(ifstream &inputFile) {
        //...
        string fileName;
        cout << "File path: ";
        getline(cin, fileName); //Inputing file name from user

        inputFile.open(fileName.c_str()); //Opening the file
        //...
    }

    string delimiterMatching(ifstream &myFile) {

    char myChar;
    char findThis;
    char nextChar;
    char nextNextChar;
    LLStack myStack;

    cout << "Checking file!\n";

    myFile >> myChar;
    while (!myFile.eof()) {
        if (myChar == '(' || myChar == '[' || myChar == '{') {
            if (myChar == '(') {
                findThis = ')';
            }
            else if (myChar == '[') {
                findThis = ']';
            }
            else if (myChar == '{') {
                findThis = '}';
            }
            myStack.push(myChar);
        }
        else if (myChar == ')' || myChar == ']' || myChar == '}') {
            if (myChar != findThis) {
                return "ERROR";
            }
        }
        else if (myChar == '/') {
            myFile >> nextChar;
            cout << nextChar << endl;
            if (nextChar == '*') {
                myFile >> nextChar;
                myFile >> nextNextChar;
                while (nextChar != '*' && (nextNextChar != '/')) {
                    {
                        if (myFile.eof()) {
                            return "ERROR";
                        }
                        else {
                            nextChar = nextNextChar;
                            myFile >> nextNextChar;
                        }
                    }
                }
            }
            else {
                myChar = nextChar;
                continue;
            }
        }
        else {
            //"ignore other characters"??
            myFile >> myChar;
            cout << myChar << endl;
        }
    }
 if (myStack.isEmpty()) {
        return "Error free";
    }
    else {
        return "ERROR ";
    }

    system("pause");
}

ListStack.h

#pragma once

#ifndef LL_Stack
#define LL_Stack

#include"LList.h"

class LLStack {

private:
    LList list;

public:

    void push(char el) {
        list.push_back(el);
    }

};

#endif

LList.cpp

#include<iostream>
#include"LList.h"

LList::~LList() {
    for (LLNode *p; !isEmpty();) {
        p = head->next;
        delete head;
        head = p;
    }
}

void LList::push_back(char el) { //"insert el at the end of the list"
    if (tail != 0) { //if list not empty
        tail->next = new LLNode(el); //according to debugger this is causing the problem
        tail = tail->next;
    }
    else head = tail = new LLNode(el);
}

LList.h

#pragma once

#ifndef LINKED_LIST
#define LINKED_LIST

class LLNode {
public:
    LLNode() {
        next = 0;
    }
    LLNode(char el, LLNode *ptr = 0) {

        info = el; next = ptr;
    }
    char info;
    LLNode *next;// , *prev;
};

class LList { //For access to the list
public:
    LList() {
        head = tail = 0;
    }
    ~LList();
    int isEmpty() {
        return head == 0;
    }

    void push_back(char el); //"insert el at the end of the list"

private:
    LLNode *head, *tail;
};

#endif

example.txt

 /*
 * words
 * more words and, such
 *  all the words
 *  so many words
 */


int main() { 

    for (int i = 0; i < 500; i++) { //I'm really hoping to type more words here
        if (so many words){
        ) // should be a curly brace
    }

    return 0;
}

Research/Thoughts:
I don't know what I'm doing wrong, but after a little research, I'm pretty sure it has something to do with the way I'm declaring a new stack in Source.cpp.

I found this question where someone seems to be having the exact same problem, but I'm having a really hard time understanding the answers and how they apply to my project and if I am making the same mistake.

Question:
How do I fix this error?

Community
  • 1
  • 1
StarSweeper
  • 397
  • 2
  • 4
  • 28
  • 1
    http://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered-wrong – Biffen Oct 21 '16 at 05:26
  • 2
    Questions seeking debugging help (‘**why isn't this code working?**’) must include the desired behaviour, a specific problem or error and the shortest code necessary to reproduce it **in the question itself**. Questions without **a clear problem statement** are not useful to other readers. See: [How to create a Minimal, Complete, and Verifiable example](http://stackoverflow.com/help/mcve). – Biffen Oct 21 '16 at 05:27
  • @Biffen what is wrong with my question that it doesn't meet those requirements? The desired behavior is that the program doesn't crash, I say what the specific error is and I used as little code as possible. – StarSweeper Oct 21 '16 at 05:30
  • Would you say that it's *complete* and *verifiable*? – Biffen Oct 21 '16 at 05:31
  • 2
    Without an MCVE it's really hard to say anything. But you do follow [the rules of three or five](http://en.cppreference.com/w/cpp/language/rule_of_three)? You do remember to initialize pointers on construction? Have you tried running in a debugger to locate the crash? Or using a memory debugger such as [Valgrind](http://valgrind.org/) to help you? – Some programmer dude Oct 21 '16 at 05:31
  • @Biffen I just don't know what else I could include that has anything to do with the error. I can't copy and paste the entire project because its for school. What more information is needed? – StarSweeper Oct 21 '16 at 05:35
  • @StarSweeper How about code that can be compiled and run? Did you read [this](http://stackoverflow.com/help/mcve)? – Biffen Oct 21 '16 at 05:36
  • 2
    What is `DLList`? What is `LList`? What is `DLLNode`? What do their constructors look like and do? Where's the `main` function which calls all functions? Please follow the link provided by Biffen and read about how a MCVE should be. – Some programmer dude Oct 21 '16 at 05:38
  • 1
    My crystal ball tells me this is a failure to follow the [Rule of Three](https://stackoverflow.com/questions/4172722/what-is-the-rule-of-three) case. As with any question that doesn't provide an MCVE, soothsaying and wags (wild-arse-guesses) are about all that can be offered. That was mine. Best of luck. – WhozCraig Oct 21 '16 at 05:52
  • I'm trying to edit it so I can provide more code, its going to take a while though. – StarSweeper Oct 21 '16 at 05:55
  • @Biffen edited to add code that compiles – StarSweeper Oct 21 '16 at 06:27
  • What does your constructor look like for LList, you know the place where you initialize all your member variables to nullptr ? – AndersK Oct 21 '16 at 07:11
  • @AndersK sorry, I accidently put ListStack.h twice and left LList.h out. Fixed it now – StarSweeper Oct 21 '16 at 07:17
  • Can you provide the example input file? I can't reproduce the crash, I just realize your program loops infinite if the file contains any character other than `'(', '[', '{'` – grek40 Oct 21 '16 at 08:45
  • Your stack does not have any problem, the problem is with you delimiterMatching().. – sanjay Oct 21 '16 at 09:00
  • @grek40 added example.txt – StarSweeper Oct 21 '16 at 09:09
  • @grek40 I should mention that this is incomplete code, and delimiterMatching is the part missing the most code. I can add more if its absolutley needed, but hopefully there is enough here to figure out what I'm doing wrong – StarSweeper Oct 21 '16 at 09:18
  • @StarSweeper If you can copy the code from your own question, use the example.txt from your question and get the exception from your question, then you have provided enough / complete code. If anything different happens for the question code, then it's useless. – grek40 Oct 21 '16 at 09:33
  • @grek40 I updated the code for source.cpp to include the rest of delimiterMatching. – StarSweeper Oct 21 '16 at 09:47

0 Answers0