2

I just found that there's no LinkedList implementation in Objective-C, but I need one. So I am going to use std::list(or there's a better alternative?).

My question is: what should I care about memory management under ARC and non-ARC?

4
  • 1
    You can indeed use std::list thanks to gcc/clang's Objective-C++ mode. You'll need to switch to the .mm filename extension to enable it, and you'll probably want to avoid using C++ types in your header files, or all files importing those headers will need to change to .mm as well. If the type parameters to std::list do not use objective-C class types, you're fine. If they are, and you aren't using ARC, you'll need to make sure you retain/release correctly. I don't know how it works with ARC but I suspect you should selectively disable ARC for your .mm files. Commented Mar 6, 2012 at 11:21
  • Thank you @pmjordan, this info helps a lot, it sheds some light on how I can use C++ in Objective-C(I am new to the language^_^). Commented Mar 6, 2012 at 11:42
  • What's wrong with NSMutableArray? Commented Mar 6, 2012 at 12:02
  • Hi, @JeremyP, NSMutableArray is great, but I didn't know much about it 1 hour ago, I thought it was implemented as a raw array, which turned out to be wrong. Ridiculous Fish tells the truth. Commented Mar 6, 2012 at 12:11

2 Answers 2

2

You should manually take care of the memory management, since std::list is a C++ container. Or you can use NSMutableArray and treat it like a linked list, append new elements with insertObject:atIndex: for the last index, iterate through it with an iterator, removeLastObject, etc.

Sign up to request clarification or add additional context in comments.

7 Comments

I can't read the source code of NSMutableArray, but from its name, I can imagine that it is implemented as holding an internal array and putting objects in that array, which is not efficient to do operations like removeLastObject or insertObject:atIndex. By manually taking care of the memory management, do you mean I will have to retain each object before I add it to the list and release all objects in the list when before I delete the list?
It may be implemented as a deque for example.
It's not implemented as a raw C array, if that's what you mean.
I would seriously try using NSMutableArray first; switch to something else if performance is measurably an issue.
Ok, I will try NSMutableArray. I don't think performance will be an issue for the senario in which I am going to use a LinkedList, cause it will not hold that many objects. Thank you @Wevah, and @Alexander~
|
2

You might be able to use a custom C++ smart pointer (in Objective-C++):

template<class X>
class objc_ptr {
private:
  X* ptr;
public:
  ~objc_ptr() {
    if(ptr!=NULL) [ptr release];
  }
  objc_ptr() {
  }
  objc_ptr(X* x) {
    this.ptr = x;
    if(x!=NULL) [x retain];
  }
  // TODO, copy constructor, operator= ...
};

Then you could use: std::list>

Or using boost intrusive_ptr:

void intrusive_ptr_add_ref(NSObject *x) {
  [x retain];
}
void intrusive_ptr_release(NSObject *x) {
  [x release];
}

std::list<boost::intrusive_ptr<NSFooBar>> list = ...;

1 Comment

looks kinda overkill for my problem, but it's a good answer though, Thank you!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.