Tries to use std :: list in objective-c program

I am trying to use std :: list in my objective-c program and have runtime problems.

I have (edit for short)

#import <list> ... @interface Foo { std::list<Bar *> *myList; } ... @implementation Foo -(void)loopList { myList = new std::list<Bar *>; for (std::list<Bar *>::iterator ii; ii != myList->end(); ++ii) { // Do nothing here } } 

Everything compiles fine, but I get EXC_BAD_ACCESS during the execution of the for loop. I don't do anything with the content, I just loop them (for now).

I would use NSMultableArray, but I need to quickly insert and remove elements during iteration, which is not possible. Also, I would like to use stl for performance issues.

I have my sourcecode.cpp.objcpp files installed.

Thanks in advance.

+4
source share
2 answers

This line:

 for (std::list<Bar *>::iterator ii; ii != myList->end(); ++ii) 

Need to become:

 for (std::list<Bar *>::iterator ii = myList->begin(); ii != myList->end(); ++ii) 

Your version of ii never assigns a value, so it has nothing to iterate over. Based on the code you posted, it is not clear why, but because of this, you later try to dereference the wrong memory address and why you throw an exception (EXC_BAD_ACCESS).

+4
source

I don't know about the C object, but in C ++ you need to tell the iterator which iterate the list instance:

 for (std::list<Bar *>::iterator ii = myList->begin(); ii != myList->end(); ++ii) 
+1
source

Source: https://habr.com/ru/post/1301273/


All Articles