views:

53

answers:

2

I create an array, similar to classic C (not NSArray or one of it's children) - something like BOOL i[5];. And I want to make all its values to be equal to NO.

First of all, I didn't found any information about initial values of such arrays (I know that in classic C they will be undefined, but don't know exactly about Objective-C. I found info about classes and its inner data [after allocation, without initialization], but not about simple data types).

And the second, if I should set array values manually - should I use memset(...); or something different?

To prevent possible questions... I want to use this construction as array of temporary boolean flags and don't think that it is proved to use something like NSArray here.

A: 

If that BOOL i[5] is an ivar of an ObjC class, its content will be initialized (memset-ed) to 0.

As described in the +alloc method:

... memory for all other instance variables is set to 0.


To set the array to all NO in other situations, you could use

memset(i, 0, sizeof(i));
KennyTM
Yes. And if it's not you can explicitly `memset` it to zero to for `NO` as the default.
calmh
A: 

Even if it is 0-ed instead of undef like in C, a proper initialization should set all values explicitly since you can't know if NO is 0 (it is, but hypotethically...!), so simply:

for(j = 0; j < 5; j++) i[j] = NO;

could do the job.

ShinTakezou