Reputation: 27
The code compiles fine and runs fine until the for loop to iterate through f_read_Prediction_Set. The segmentation fault occurs directly after that line...what am I missing?
std::map< RAddr, uint32_t >* transCoherence::getCurrentSets(uint32_t log2AddrLs, uint32_t maskSets, uint32_t log2Assoc, int pid, RAddr caddr)//function to return prediction set
{
uint32_t set;
std::map< RAddr, uint32_t >* currentSets = new std::map< RAddr, uint32_t >;
std::map< RAddr, uint32_t >* f_read_Prediction_Set = new std::map< RAddr, uint32_t >;
for(std::map<RAddr, uint32_t>::iterator it = f_read_Prediction_Set->begin(); it!=f_read_Prediction_Set->end(); ++it)
{
set = (((it->first) >> log2AddrLs) & maskSets) << log2Assoc;
if(set == caddr)
(*currentSets)[set] = 1;
}
return currentSets;
}
Upvotes: 1
Views: 386
Reputation: 76240
Let me take your code full of pointers and dynamically allocation and translate it to an error and memory leak free version:
std::map<RAddr, uint32_t> transCoherence::getCurrentSets(uint32_t log2AddrLs, uint32_t maskSets, uint32_t log2Assoc, int pid, RAddr caddr)
{
std::map<RAddr, uint32_t> currentSets;
std::map<RAddr, uint32_t> f_read_Prediction_Set;
// populate f_read_Prediction_Set
for (const auto& p : f_read_Prediction_Set) {
uint32_t set = (((p.first >> log2AddrLs) & maskSets) << log2Assoc);
if(set == caddr)
currentSets[set] = 1;
}
return currentSets;
}
so that you can forget about the pointer problem entirely.
If you worried about performance you should note that there's a little thing in C++ called RVO (Return Value Optimization) that will avoid actually copying the vector back when the function returns.
Also I can't fail to notice that you use the word "set" in your map object's names, so it's good for you to know that there's an std::set class for you to use if you desire unique keys.
Upvotes: 1