Honestly I don't see a reason to do that mess; I'm also not sure if your code would work. sizeof(arraySize)? Where is arraySize?
I think you mean the sizeof of the datatype, for example:
Code:
id myArray = (id*)malloc(p_size * sizeof(id));
Though that might be a bit difficult, I'm not sure if objective-C gives an explicit size for `id'.
NSMutableArray is your friend. It does the dirty work for you.
But seriously, if you want to do it without using Cocoa classes, you're looking at pure C. Objective-C is a subset of C, so just look up dynamic arrays for C and use it in your objective-C class. I think my included code above generally shows it, but I could be wrong. It's just that NSMutableArray makes our lives so much easier. 🙂
There's not a lot of reason to do a mutable array structure from the ground up. If you're studying Computer Science or something related, you'll definitely learn how it works, but for anything else, it's not extremely helpful.
C++ already has dynamic arrays, but they can't hold multiple data types (as far as I know).
Isn't there some circumstances in object oriented programming where using dynamic c arrays is neccessary for efficiency, because objects require too much overhead?
If so, I think it's clever to learn these ideas involved with making a dynamic array.
Isn't there some circumstances in object oriented programming where using dynamic c arrays is neccessary for efficiency, because objects require too much overhead?
Depends where your performance bottleneck is. It's a more productive use of time to fix things that you know cause performance problems (by using tools such as Shark) rather than optimising something that works 'well enough'. Often a better designed algorithm will give much better returns.
Depends where your performance bottleneck is. It's a more productive use of time to fix things that you know cause performance problems (by using tools such as Shark) rather than optimising something that works 'well enough'. Often a better designed algorithm will give much better returns.
Indeed. You can have the most heavily tuned bubble-sort in the world and it'll still be slow as molasses compared to even a bad implementation of quicksort.