0

There's this instance variable in my objective-c class:

ALuint source;

I need to have an mutable array of OpenAL Sources, so in this case probably I need a mutable C-array.

But how would I create one? There are many questions regarding that:

1) How to create an mutable C-array?

2) How to add something to that mutable C-array?

3) How to remove something from that mutable C-array?

4) What memory management pitfalls must I be aware of? Must i free() it in my -dealloc method?

1
  • What do you need it for? Why are you assuming you need a C-style array? By the way, i don't think that community wiki fits here. Commented May 4, 2010 at 19:50

1 Answer 1

2

I’d keep things simple. ALuint is some kind of int, so that you can easily wrap it using NSNumber and stick it in an ordinary NSMutableArray:

ALuint bar = …;
NSMutableArray *foo = [NSMutableArray array];
[foo addObject:[NSNumber numberWithInt:bar]];

// and later
ALuint source = [[foo lastObject] intValue];
Sign up to request clarification or add additional context in comments.

3 Comments

good point... however, would be good to know how to do something in c, when performance matters. allocacting objective-c objects is very heavy.
It is not. It can make a difference when you’re writing a particle engine with dozens of thousands of particles to be recalculated at 50 fps. In the remaining 99.9 % of cases the bottleneck is going to be elsewhere.
That's not really an answer to the question. I came here looking for an answer to the same question as per the title, and this doesn't help :(. Apple's API's frequently require C arrays as input, and in many cases you need a mutable array. I was looking for something better than "cast void* pointers all over the place and look forward to hard crashing your app one day"

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.