2

I'm getting a segfault when I run this code and I'm not sure why. Commenting out a particular line (marked below) removes the segfault, which led me to believe that the recursive use of the iterator "i" may have been causing trouble, but even after changing it to a pointer I get a segfault.

void executeCommands(string inputstream, linklist<linklist<transform> > trsMetastack)
{
int * i=new int;
(*i) = 0;
while((*i)<inputstream.length())
{
    string command = getCommand((*i),inputstream);
    string cmd = getArguments(command,0);
    //cout << getArguments(command,0) << " " << endl;


    if (cmd=="translate")
    {

        transform trs;
        trs.type=1;
        trs.arguments[0]=getValue(getArguments(command,2));
        trs.arguments[1]=getValue(getArguments(command,3));
        ((trsMetastack.top)->value).push(trs);
        executeCommands(getArguments(command,1),trsMetastack);
    }

    if (cmd=="group")
    { 
        //make a NEW TRANSFORMS STACK, set CURRENT stack to that one
        linklist<transform> transformStack;
        trsMetastack.push(transformStack);


        //cout << "|" << getAllArguments(command) << "|" << endl;
        executeCommands(getAllArguments(command),trsMetastack); // COMMENTING THIS LINE OUT removes the segfault

    }

    if (cmd=="line")
    { //POP transforms off of the whole stack/metastack conglomeration and apply them.


        while ((trsMetastack.isEmpty())==0)
        {
            while ((((trsMetastack.top)->value).isEmpty())==0)   //this pops a single _stack_ in the metastack
            { transform tBA = ((trsMetastack.top)->value).pop();
                cout << tBA.type << tBA.arguments[0] << tBA.arguments[1];
            }
            trsMetastack.pop();
        }


    }

"Metastack" is a linked list of linked lists that I have to send to the function during recursion, declared as such:

    linklist<transform> transformStack;
    linklist<linklist<transform> > trsMetastack;
    trsMetastack.push(transformStack);


    executeCommands(stdinstring,trsMetastack);

The "Getallarguments" function is just meant to extract a majority of a string given it, like so:

    string getAllArguments(string expr) // Gets the whole string of arguments
    {
        expr = expr.replace(0,1," "); 
        int space = expr.find_first_of(" ",1);
        return expr.substr(space+1,expr.length()-space-1);
    }

And here is the linked list class definition.

    template <class dataclass> 
    struct linkm {
        dataclass value;     //transform object, point object, string... you name it
        linkm *next;
    };

    template <class dataclass> 
    class linklist 
    {
    public:
        linklist()
        {top = NULL;}
        ~linklist() 
        {}
        void push(dataclass num)
        {
            cout << "pushed";
            linkm<dataclass> *temp = new linkm<dataclass>;
            temp->value = num;
            temp->next = top;
            top = temp;
        }   
        dataclass pop()
        {

            cout << "pop"<< endl;
            //if (top == NULL) {return dataclass obj;}
            linkm<dataclass> * temp;
            temp = top;
            dataclass value;
            value = temp->value;
            top = temp->next;
            delete temp;
            return value;
        }
        bool isEmpty()
        {
            if (top == NULL) 
            return 1;
            return 0;
        }
        //  private:
        linkm<dataclass> *top; 
    };

Thanks for taking the time to read this. I know the problem is vague but I just spent the last hour trying to debug this with gdb, I honestly dunno what it could be.

xyzzy
  • 23
  • 1
  • 4
  • 3
    If you don't know why you get a segfault - run the program under gdb, or use generated coredump... –  Mar 13 '12 at 01:03
  • I tried that. gdb said top* was empty, but other than that it was giving me memory addresses and I'm not experienced at all with it. :/ – xyzzy Mar 13 '12 at 01:07
  • [Don’d use pointers for `i` here](https://twitter.com/#!/klmr/status/177173159836008448). Please fix this first. – Konrad Rudolph Mar 13 '12 at 01:19
  • getAllArguments assumes lots of things about the string argument. You assume it has at least one character. You are also assuming that the string contains a space, and you are storing the result of string::find_first_of in an int, which IS NOT a size_t. We don't have the getCommand nor the getArguments functions, and we don't have your test input, therefore it's pretty hard to spot the problem this way. – mfontanini Mar 13 '12 at 01:23
  • Is that `transform`, as in `std::transform`? – Beta Mar 13 '12 at 01:27
  • You might want to reduce to code to a minimal snippet that reproduce the problem, or to debug it using `gdb`. Use `bt` in `gdb` to print your stack. – phoeagon Feb 16 '13 at 06:17

1 Answers1

1

It could be anything, but my wild guess is, ironically: stack overflow. You might want to try passing your data structures around as references, e.g.:

void executeCommands(string &inputstream, linklist<linklist<transform> > &trsMetastack)

But as Vlad has pointed out, you might want to get familiar with gdb.

Daniel Roethlisberger
  • 6,958
  • 2
  • 41
  • 59
  • Aw, I'm still no good at passing things by reference. After changing those to both & references, I got a few errors. Tried fixing them, no dice. Something needed an rvalue when it was getting an lvalue, I tried changing how the function worked... it got messy. I then tried changing the above to pointers and after changing everything else to handle pointers, my code wouldn't output at all. :( – xyzzy Mar 13 '12 at 21:35