简体   繁体   中英

Sigbus on std::map.find

I have got a map defined inside a class in header file like this:

std::map<int, char> mCompletedIds;

I can do with a vector here, as I just need to store the ids which have been completed. But to have fast find, I am using map. So, I always put second argument of pair as 0.

Now, In one of the fns. of class, I am doing find.

std::map<int, char>::iterator it = mCompletedIds.find(id); //id is defined above

On this statement, I am getting SIGBUS. Is it because map is empty at the moment? Can anyone pls. help me understand the reason.

Thanks, sg

If you just want a store of numbers you can use std::set<int>

To see if a value is present use

std::set<int> mCompletedIds;
bool found = mCompletedIds.count(id) != 0; 

Your SIGBUS error will usually be caused by bad alignment or some other corruption that happened in your code, and a tool like valgrind may indicate to you where your real error is.

std::set is what you need : cppRef

int id = 0;

std::set<int> idList;
std::set<int>::iterator it = idList.find(id); //see also count function
if (it != idList.end()) { // find will return set::end if the element is not found
    // id is in your list
} else {
    // id isn't in your list
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM