I have been posting a bit about my current side project, a scripting language interpreter. I have gotten past my getline error I talked about in a previous post and am now trying to get the results from the script. I created a map of all the labels in the script with a list of every operation that happens under the list. like this map<string, list<Operation *>>. When ever I try to access the list at a given key I am returned with a bad access error. I try to access the list in the map like this

Operation *currentOP;
    std::list<Operation *> *currentLst;
 
    std::map<std::string,std::list<Operation *> >::iterator labelPtr;
    
    labelPtr = this->labelOPMap.find(currentLabel);
    *currentLst = labelPtr->second; // Problem Here

This is how I am pushing the list onto the map

if(this->hasLabel(programLine)) {
    // push the label and OPlst onto the map
    labelOPMap[label] = *OPlst;
                      
    // Get the new label
    label = this->getLabel(this->positionOfChar('[',programLine),programLine);
                    
    if(!this->labelExist(label)) {
        // Clip the label off the string
        programLine = programLine.substr(this->positionOfChar(']',programLine)+1);
                        
        // Create a new Operation List
        OPlst = new std::list<Operation *>;
    }
}

// Parse the current line of the document and return an operation object
OP = this->ProcessLine(programLine);
                
// push the Operation onto the list
OPlst->push_back(OP);

Does anyone know why I would be getting an bad access error. I am not the best with pointers either.

Recommended Answers

All 6 Replies

I have been posting a bit about my current side project, a scripting language interpreter. I have gotten past my getline error I talked about in a previous post and am now trying to get the results from the script. I created a map of all the labels in the script with a list of every operation that happens under the list. like this map<string, list<Operation *>>. When ever I try to access the list at a given key I am returned with a bad access error. I try to access the list in the map like this

Operation *currentOP;
    std::list<Operation *> *currentLst;
 
    std::map<std::string,std::list<Operation *> >::iterator labelPtr;
    
    labelPtr = this->labelOPMap.find(currentLabel);
    *currentLst = labelPtr->second; // Problem Here

This is how I am pushing the list onto the map

if(this->hasLabel(programLine)) {
    // push the label and OPlst onto the map
    labelOPMap[label] = *OPlst;
                      
    // Get the new label
    label = this->getLabel(this->positionOfChar('[',programLine),programLine);
                    
    if(!this->labelExist(label)) {
        // Clip the label off the string
        programLine = programLine.substr(this->positionOfChar(']',programLine)+1);
                        
        // Create a new Operation List
        OPlst = new std::list<Operation *>;
    }
}

// Parse the current line of the document and return an operation object
OP = this->ProcessLine(programLine);
                
// push the Operation onto the list
OPlst->push_back(OP);

Does anyone know why I would be getting an bad access error. I am not the best with pointers either.

can u post the complete program so that i can debug it in my machine

Attached is my code compressed in a zip file. Also any tips or pointers on how to write good code would be helpful since I have been teaching myself this stuff programmer.

The problem is that you have a map of type
map<string,list<pointer> >.

You are doing two thing wrong:

std::map<std::string,std::list<Operation *> >::iterator labelPtr;
labelPtr = this->labelOPMap.find(currentLabel);
// NO CHECK FOR not found e.g. labelPtr==labelOPMap.end()
// currentLst does not point to anything: it is not initialized
*currentLst = labelPtr->second; ]

These two problem guarantee that it will fail.

Attached is my code compressed in a zip file. Also any tips or pointers on how to write good code would be helpful since I have been teaching myself this stuff programmer.

Hi,
I have seen your code. I have fixed some bugs in it. U forgot to check whther an element is there or not in the maps. U directly did

this->labelOPMap.find(currentLabel)->second();

in so many places.
I have found one more error in there in the Parser.cpp file.
u have forgt to initialize the OP variable and u were directly accessing it by *OP.
There is still a bug remaining in your code. Its while executing. I have made a makefile for your code. Use it for compiling. It will be much easier. Just type "make".
Please check your code for the execution part. There is still a segmentation fault coming which I left for u to correct.

Thanks that helps a lot. I also found that I had a major error in my code also. I never actually pushed the variables onto the map, so every time I tried to accessed the map there wouldn't be anything to access.

Also I have worked with make files before just that this time I was using Xcode as my IDE and it doesn't generate a make file. Sorry I forgot to write one up to make compiling easier.

its ok.
Try to use makefile tool for compilation.
Also there is another tool called Imakefile which generates a makefile for u. U can explore those stuff.......have a nice time..... :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.