C Board  

Go Back   C Board > General Programming Boards > C++ Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 03-17-2010, 02:01 AM   #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.
verxintRising is offline   Reply With Quote
Old 03-17-2010, 02:22 AM   #2
CSharpener
 
vart's Avatar
 
Join Date: Oct 2006
Posts: 5,556
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?
__________________
If I have eight hours for cutting wood, I spend six sharpening my axe.
vart is offline   Reply With Quote
Old 03-17-2010, 02:32 AM   #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.
verxintRising is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

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


All times are GMT -6. The time now is 12:06 AM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22