Thread: Program crashes when trying to assign a value to a class variable

  1. #1
    Registered User
    Join Date
    Dec 2009
    Posts
    16

    Program crashes when trying to assign a value to a class variable

    find.cpp
    Code:
    void find(vector<macros> &m, file &f)
    {
        char *c_file;
        c_file=new char[f.oldLength()];
        f.oldFile().copy(c_file, f.oldLength());
        for(int x=0;x<f.oldLength();x++)
        {
            if(c_file[x]=='#')
            {
                x++;
                if(isValidMacro(c_file,x))
                {
                    x+=5;
                    f.addMacro();
                    m.push_back(macros());
                    m[f.numOfMacros()].atLoc(x);
                    m[f.numOfMacros()].toSymbol(getSymbol(c_file,x));
                }
            }
        }
    }
    
    string getSymbol(char *c_file, int &pos)
    {
        stringstream ss_symbol;
        string symbol;
        do
        {
            pos++;
        }while(c_file[pos]!=' ');
        do
        {
            ss_symbol<<c_file[pos];
            pos++;
        }while(c_file[pos]!='('&&c_file[pos]!=' ');
        ss_symbol>>symbol;
        return symbol;
    }
    macros.cpp (implementation file for the macros class, this function causes the crash)
    Code:
    void macros::toSymbol(string newSymbol)
    {
        pSymbol=newSymbol;
    }
    macros.h (class declaration)
    Code:
    class macros
    {
        public:
            macros();
            ~macros();
            void toSymbol(string newSymbol), toToken(string newToken), atLoc(int location);
    
            string symbol(), token(), args(int num);
            int numOfArgs(), loc();
        private:
            string pSymbol, pToken, *pArgs;
            int pNumOfArgs, pLocation;
    };
    Starting with the find() function, everything appears to work fine. The Code::Blocks debugger stopped working for me and I'm not quite as good at using gdb manually.. But I can tell it's the line pSymbol=newSymbol; that's causing the crash. I did some checking and similar functions in the macro class cause a crash during variable assignment. I have no idea what this is about.

    Thanks.

  2. #2
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    1. why are you using char array instead of vector of chars?
    2. why are you using operator[] instead of at() to access your macros vector
    3. after first m.push_back(macros()); your vector contains 1 element I suppose. What is the value of f.numOfMacros() - is it zero at this time?
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  3. #3
    Registered User
    Join Date
    Dec 2009
    Posts
    16
    Quote Originally Posted by vart View Post
    1. why are you using char array instead of vector of chars?
    2. why are you using operator[] instead of at() to access your macros vector
    3. after first m.push_back(macros()); your vector contains 1 element I suppose. What is the value of f.numOfMacros() - is it zero at this time?

    1. Never thought of that but it's a good idea. I never had a problem with using pointers for dynamic arrays, so I always did that.
    2. Didn't know the at() function existed/was preferred.
    3. This is probably an issue, f.numOfMacros() would be returning 1 right now. That solved the problem, but I don't understand why it was crashing on the line in the class function instead of the line that tried to call the nonexistent part of the vector.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. I need the code of few programs in c++.plzzzzz help...plzzz
    By NAVINKR20 in forum C++ Programming
    Replies: 1
    Last Post: 05-08-2009, 09:13 AM
  3. Need help with a class variable.
    By RealityFusion in forum C++ Programming
    Replies: 11
    Last Post: 10-20-2005, 10:26 AM
  4. fopen();
    By GanglyLamb in forum C Programming
    Replies: 8
    Last Post: 11-03-2002, 12:39 PM