0

I created a C array like this:

unsigned char colorComps[] = {2, 3, 22,   55, 9, 1};

which I want to pass to an initializer of an Objective-C object.

So I think I have to put the array on the heap:

size_t arrayByteSize = numColorCompVals * sizeof(unsigned char);
unsigned char *colorCompsHeap = (unsigned char*)malloc(arrayByteSize);

Then I have to write my first "stack memory array" to the heap array in for loop:

for (int i = 0; i < numColorCompVals; i++) {
   colorCompsHeap[i] = colorComps[i];
}

Side question: Is there a more elegant solution to avoid the for-loop step?

And then I pass it to the method:

defined as

- (id)initWithColorCompsC:(unsigned char *)colorCompsHeap;

TheObject *obj = [[TheObject alloc] initWithColorCompsC:colorCompsHeap];

TheObject has a property to hold the C-array:

@property (nonatomic, assign) unsigned char *colorComps;

And in -dealloc I free it:

free(_colorComps);

This is in theory. I use ARC for Objective-C. Am I doing this correct or is there a better way?

Proud Member
  • 40,078
  • 47
  • 146
  • 231

3 Answers3

2

This seems fine, however...

  1. Don't cast the return value of malloc();
  2. and don't reinvent memcpy(). Instead of your for loop, write memcpy(dest, src, size).
Community
  • 1
  • 1
  • Even better - use `memcpy` with a C99 compound literal array. Much more consise. – Richard J. Ross III Aug 07 '13 at 14:04
  • @RichardJ.RossIII Yeah, [I am aware of them](http://stackoverflow.com/questions/17896743/initialization-of-structure-with-ternary-operator/17896781#17896781) (I just didn't think this was the question here.) –  Aug 07 '13 at 14:06
  • Richard can you explain what you mean with a C99 compound literal array? – Proud Member Aug 07 '13 at 14:08
  • According to the documentation memcpy is for chars. What about other data types? http://www.tutorialspoint.com/c_standard_library/c_function_memcpy.htm – Proud Member Aug 07 '13 at 14:31
  • @ProudMember: `memcpy` copies data. It is not picky about data type. The documentation you link to clearly specifies that it takes `void *` arguments, not `char` or `char *`. – Stephen Canon Aug 07 '13 at 14:32
  • @ProudMember It's not for `char`s. It's for anything. (That's why it accepts `void *` and `const void *`.) –  Aug 07 '13 at 14:33
  • In the linked example: memcpy(dest, src, strlen(src)+1); - why add one byte? – Proud Member Aug 07 '13 at 14:39
  • @ProudMember For the 0-terminator. –  Aug 07 '13 at 14:42
1

If TheObject is going to free the array, then its init method should be the one to make the copy, NOT the caller. That way each instance of TheObject make its own copy and frees its own copy, it owns that copy.

Also, then it doesn't matter where the parameter to the init comes from, stack or heap. It won't matter if the init method makes a copy of it.

Use memcpy to make the copy, with sizeof the destination array, like this for the .m file:

@interface PTTEST ()
@property (nonatomic, assign) unsigned char *colorComps;
@end

@implementation PTTEST

- (void)dealloc
{
    free(_colorComps);
}

- (id)initWithColorCompsC:(unsigned char *)colorComps
       numberOfColorComps:(unsigned)numberOfColorComps
{
    self = [super init];
    if (self) {
        // compute size based on sizeof the first element (in case
        // the element type get changed later, this still works right)
        size_t arraySize = sizeof(colorComps[0]) * numberOfColorComps;

        _colorComps = malloc(arraySize);

        memcpy(_colorComps, colorComps, arraySize);
    }
    return self;
}

@end
progrmr
  • 75,956
  • 16
  • 112
  • 147
0

Doesn't seems correct to me, you are filling colorCompsHeap(an array of unsigned chars) with the values of colorComps[] (an array of doubles)

unsigned char u = 0.2;

printf("%f\n", (double)u);

Outputs 0.000000

David Ranieri
  • 39,972
  • 7
  • 52
  • 94
  • @DavidRF Then it was someone else, and he **still** didn't leave any explanation. –  Aug 07 '13 at 14:37