Using sizeof Correctly with Byte[]

后端 未结 3 1257
感情败类
感情败类 2021-01-15 17:06

I\'m sort of out of my depths here, but I have the following code (the real code actually has a point of course):

- (NSData*) dataTheseBytes:(Byte[]) bytes {         


        
相关标签:
3条回答
  • 2021-01-15 17:16

    You cannot pass arrays to a function. You're passing a pointer to the first element in the array of the caller.

    If you need the length of that array, you need to pass that length as a separate argument to your function, and use that instead of sizeof

    0 讨论(0)
  • 2021-01-15 17:21

    When you pass a C array as a method or C function argument, it "decays" to a pointer to the underlying type (i.e. Byte[] is actually passed as Byte *.) So the called method/function has no idea how many elements are present in the array.

    You must also pass the length of the array in order for the called code to know what you want. That's why +[NSData dataWithBytes:length:] has that second argument.

    0 讨论(0)
  • 2021-01-15 17:34

    c arrays do not embed their element count.

    this is how you would declare a method with an unspecified element count. this is not generally usable:

    `- (NSData*) dataTheseBytes:(const Byte*)bytes;`
    // or
    `- (NSData*) dataTheseBytes:(const Byte[])bytes;`
    

    a more rigid implementation could specify the element count. this is ok if you are always using the same size. example:

    enum { MONByteBufferElementCount = 23 };
    ...
    `- (NSData*) dataTheseBytes:(const Byte[MONByteBufferElementCount])bytes
    {
      return [NSData dataWithBytes:&bytes[0] length:MONByteBufferElementCount * sizeof(bytes[0])];
    }
    

    the problem with using objc messaging in this case is that the compiler may not be able to determine the appropriate selector and produce an error or warning if you have declared a selector with the same name but uses different parameters or element counts. therefore, it's safer to use a c function:

    `NSData* DataTheseBytes(const Byte bytes[MONByteBufferElementCount]) {
      return [NSData dataWithBytes:&bytes[0] length:MONByteBufferElementCount * sizeof(bytes[0])];
    }
    

    or use a more verbose name:

    `- (NSData*) dataWithMONByteBuffer:(const Byte[MONByteBufferElementCount])bytes
    {
      return [NSData dataWithBytes:&bytes[0] length:MONByteBufferElementCount * sizeof(bytes[0])];
    }
    

    in objc, it's most common to pass the length as an argument, similar to the NSData constructor you call. some part of your program will be able to determine this value (whether it is NSData, a c array or something else).

    - (NSData*) dataTheseBytes:(const Byte*)bytes length:(NSUInteger)length
    {
      return [NSData dataWithBytes:bytes length:length];
    }
    

    it's also common to see the element count, like so:

    - (NSData*) dataTheseFloats:(const float*)floats length:(NSUInteger)count
    {
      return [NSData dataWithBytes:floats length:count * sizeof(float)];
    }
    

    finally, there are of course a few corner cases. the obvious being a null terminated string:

    - (NSData*) dataWithASCIIString:(const char*)chars
    {
      return [NSData dataWithBytes:chars length:strlen(chars)];
    }
    
    0 讨论(0)
提交回复
热议问题