When I run my application in debug mode, sometimes I get a runtime error in this function:
void ChatListHandler::seatOccupancyChanged( const std::string& userName, bool occupied, bool isSelf, bool isSelfTable, int tableNo, int seatNo, int numPlayersAtTable )
{
if(!isSelf && (isInGroup(userName,IN_GAME_GROUP) || isInGroup(userName,IN_LOBBY_GROUP)))
{
if(occupied)
{
movePlayer(userName,IN_GAME_GROUP);
}
else
{
movePlayer(userName,IN_LOBBY_GROUP);
}
}
}
bool ChatListHandler::isInGroup( const std::string& name, GroupTypeEnum group )
{
for(size_t i = 0; i < m_groups.size(); ++i)
{
if(m_groups[i].second == group)
{
if(m_groups[i].first->getList())
{
for(agui::ListItem::iterator it =
m_groups[i].first->getList()->getItemsBegin(); it !=
m_groups[i].first->getList()->getItemsEnd(); ++it)
{
if((*it).first.text == name)
{
return true;
}
}
}
break;
}
}
return false;
m_list->repositionGroups();
}
It crashes on:
if((*it).first.text == name)
I get:
Unhandled exception at 0x5fd1942c (msvcp90d.dll) in my.exe: 0xC0000005: Access violation reading location 0x00000040.
The call stack looks like:
Thanks
I can't be sure without seeing all the code, but my guess is that there error has to do with these lines:
for(agui::ListItem::iterator it =
m_groups[i].first->getList()->getItemsBegin(); it !=
m_groups[i].first->getList()->getItemsEnd(); ++it)
Unless your call to getList() is always returning exactly the same list every time (that is, a pointer to the same list, not a copy), you could be getting iterators over different lists. This would mean that the check it != m_groups[i].first->getList()->getItemsEnd() would always be false, since the iterators come from different lists. In that case, your iterator could walk off the end of the list, so the dereference would cause a crash.
Hope this helps!
Related
I have a set where each element in the set is of type shared_ptr, I want to remove an element from the set, in eclipse the element was actually removed but when I test this in bash with valgrind I get a lot of invalid size mistakes ...
So this made me think that maybe there is a different way to remove element of type shared_ptr ?
Every element in the peoplePointer is a class of certain person:
typedef std::shared_ptr<person> peoplePointer;
class AA {
std::set<peoplePointer> setOfPeople;
public:
// function getName() return name of the person (person is another class)
void removeSomeonefromA(const std::string& name) {
for (std::set<peoplePointer>::iterator it = setOfPeople.begin();it != setOfPeople.end(); it++) {
if(name == (*it).get()->getName()) {
setOfPeople.erase((it));
}
}
}
};
Idea inspired by remove_if equivalent for std::map.
If you are able to use a C++11 or later compiler, you can use:
void removeSomeonefromA(const string& name)
{
for (set<peoplePointer>::iterator it = setOfPeople.begin(); it != setOfPeople.end(); /* Empty */ )
{
if(name == (*it).get()->getName())
{
it = setOfPeople.erase(it);
}
else
{
++it;
}
}
}
If you are required to use a prior compiler version, you can use:
void removeSomeonefromA(const string& name)
{
for (set<peoplePointer>::iterator it = setOfPeople.begin(); it != setOfPeople.end(); /* Empty */ )
{
if(name == (*it).get()->getName())
{
setOfPeople.erase(it++);
}
else
{
++it;
}
}
}
I have a problem when I try to run this code
void MyClass::setUp(){
list->clear();
Iterator i = ctr.getAll().iterator();
while (i.valid()) {
list->addItem(QString::fromStdString(i.elem().getNr()));
i.next();
}
}
When I exit the function an error occurs:
Debug Assertion Failed!
File:C:\Program Files \Microsoft Visual Studio 14.0\ VC\ include\
xmemory0 line:106
Expression: "(_Ptr_user&(_BIG_ALLOCATION_ALIGNMENT -1))==0 " &&0
I am trying to iterate through a custom list
The Iterator class is this:
void Iterator::first(){
while (pos < ht.nrElem && ht.list[pos] == nullptr)
pos++;
if (pos < ht.nrElem)
current = ht.list[pos];
}
Iterator::Iterator(const HashTable & ht): ht { ht }{
pos = 0;
first(); // current will refer the first element of the list
}
void Iterator::next(){
current = current->getNext();
if (current == nullptr) {
pos++;
first();
}
}
bool Iterator::valid(){
return (pos < ht.nrElem) && (current != nullptr);
}
Car& Iterator::elem() const{
return current->getCar();
}
For one thing, Iterator::first and thus the constructor do not neccessarily initialize current. Second, in Iterator::Iterator(const HashTable & ht): ht { ht } does it mean that Iterator has a member ht , and the parameter is also called ht? It is a bad practice. Never call parameters of member functions the same name as class fields.
I am writing two functions in a program to check if a string has an assigned numeric code to its structure array or if the given numeric code has an assigned string in the same structure array. Basically, if I only know one of the two, I can get the other. I wrote the following:
int PrimaryIndex::check_title_pos(std::string title) {
bool findPos = true;
if (findPos) {
for (int s = 1; s <= 25; s++) {
if (my_list[s].title == title) {
return s;
}
}
} else {
return -1;
}
}
std::string PrimaryIndex::check_title_at_pos(int pos) {
bool findTitle = true;
if (findTitle) {
for (int p = 1; p <= 25; p++) {
if (my_list[p].tag == pos) {
return my_list[p].title;
}
}
} else {
return "No title retrievable from " + pos;
}
}
However, it says not all control paths have a return value. I thought the else {} statement would handle that but it's not. Likewise, I added default "return -1;" and "return "";" to the appropriate functions handling int and string, respectively. That just caused it to error out.
Any idea on how I can keep this code, as I'd like to think it works but cant test it, while giving my compiler happiness? I realize through other searches that it sees conditions that could otherwise end in no returning values but theoretically, if I am right, it should work fine. :|
Thanks
In the below snippet, if s iterates to 26 without the inner if ever evaluating to true then a return statement is never reached.
if (findPos) {
for (int s = 1; s <= 25; s++) {
if (my_list[s].title == title) {
return s;
}
}
}
I am currently working on a bit of code that will search within a vector of type Person (which I have defined in the code and will show if needed). If it finds the person, it returns their name. This is currently working, but if it does not find the person, it is supposed to return a Null pointer. The problem is, I cannot figure out how to make it return a Null pointer! It just keeps either crashing the program every time.
Code:
Person* lookForName(vector<Person*> names, string input)
{
string searchName = input;
string foundName;
for (int i = 0; i < names.size(); i++) {
Person* p = names[i];
if (p->getName() == input) {
p->getName();
return p; //This works fine. No problems here
break;
} else {
//Not working Person* p = NULL; <---Here is where the error is happening
return p;
}
}
}
You could use std::find_if algorithm:
Person * lookForName(vector<Person*> &names, const std::string& input)
{
auto it = std::find_if(names.begin(), names.end(),
[&input](Person* p){ return p->getName() == input; });
return it != names.end() ? *it : nullptr; // if iterator reaches names.end(), it's not found
}
For C++03 version:
struct isSameName
{
explicit isSameName(const std::string& name)
: name_(name)
{
}
bool operator()(Person* p)
{
return p->getName() == name_;
}
std::string name_;
};
Person * lookForName(vector<Person*> &names, const std::string& input)
{
vector<Person*>::iterator it = std::find_if(names.begin(), names.end(),
isSameName(input));
return it != names.end() ? *it : NULL;
}
If the name you are searching for is not at the first element, then you are not searching in the rest of the elements.
You need to do something like -
for (int i = 0; i<names.size(); i++){
Person* p = names[i];
if (p->getName() == input) {
return p;
// Placing break statement here has no meaning as it won't be executed.
}
}
// Flow reaches here if the name is not found in the vector. So, just return NULL
return NULL;
As Chris suggested, try using std::find_if algorithm.
Looks like you just have to return Null, nullptr, or 0.
codeproject
Just use following code:
return NULL;
I am trying to use STL list in a project but i have the following problem.
I want my list to store a struct. For example this one
struct mystruct
{
int x;
int y;
};
Then i am using an iterator to access every struct in the list like this.
list<mystruct> L;
list<mystruct>::iterator lit;
for(lit=L.begin();lit!=L.end();lit++)
{
if(lit->x==1) cout << "<NUM," << lit->x << "> ";
if(lit->y==2) cout << "<ID," << lit->y << "> ";
}
This works but i want to get one struct at a time so i made this func
mystruct Myclass::next(void)
{
if(lit!=L.end())
{
lit++;
}
return *lit;
}
but i get an error after running it and i cannot understand why this happens.
Any ideas what is going wrong?
mystruct Myclass::next(void)
{
if(lit!=L.end())
{
lit++;
}
return *lit;
}
You increment unless you already are at the end, but the dereferencing happens every time, regardless of if you are at the end or not. To help around that problem, consider returning a pointer, and then a 0 pointer if you are at the end.
mystruct* Myclass::next(void)
{
if(lit!=L.end() && ++lit != L.end())
{
// dereference to get the struct, and then return the address of the struct
return &*lit;
}
return 0;
// or nullptr in C++0x
}
And then check agains 0 (or nullptr) in the code where you use Myclass::next.
If you're writing next() that returns an object (rather than pointer), then I think you also need to write has_next() function which you should call to inspect if there is item in the list or not, before calling next(). Something like this:
bool has_next()
{
list<mystruct>::iterator temp = lit;
return ++temp != L.end();
}
mystruct Myclass::next(void)
{
if( !has_next())
{
throw "end of the list is reached";
}
++lit;
return *lit;
}
//usage
while(myClassInstance.has_next())
{
mystruct s = myClassInstance.next();
//work with s
}
Or if you decide to return pointer to mystruct from next(), then has_next() is not so needed. You can write this:
mystruct * Myclass::next(void)
{
++lit;
if( lit == L.end() )
return NULL;
return &(*lit);
}
The problem is here :
mystruct Myclass::next(void)
{
if(lit!=L.end())
{
lit++;
}
return *lit;
}
First how is lit defined?
Second, if lit is equal to L.end() you should return some default value, not dereference it, because if you do, you are causing an undefined behaviour. If you are lucky, your program will crash.