0

I have been given a task to replace all uint_8 * to NSMutableData. In the existing code, a lot of pointer arithmetic is done on uint8_t and then passed onto other APIs which also accept uint8_t *.

However, when replacing uint_8 * with NSMutableData, how do I pointer arithmetic on NSMutableData?

One approach is

  1. convert NSMutableData to uint8_t *.
  2. do pointer arithmetic on uint8_t *.
  3. convert uint8_t * back to NSMutableData and pass it to other APIs.

however, when converting uint8_t * to NSMutableData, I do not have length.

How to solve this?

Old code

func(uint8_t *p);

func2() {
    uint8_t *p; //points to some piece of memory
    func(p+10); //pass  base address+ 10 bytes
}

New Code

func(NSMutableData *p);

func2() {
    NSMutableData *data = [[NSMutableData alloc] 
    initWithLength:size];
    // now, how to pass data + 10 bytes to func
    func(???)
}
  • Update your question with some concrete examples. And remember that `NSMutableData` extends `NSData`. `NSData` has the `bytes` property. – HangarRash Nov 17 '22 at 03:13
  • 1
    Don't replace, implement again. Does `func` read or write the data? – Willeke Nov 17 '22 at 05:26
  • func2 modifies the data – user2515165 Nov 17 '22 at 05:45
  • 3
    You can use `NSRange` to read specific part of `NS(Mutable)Data`. It mightt be better to update all your code inistead of relying on "C" pointers for that. But without more info, it's hard to tell if that's the corect soluttion. What you could also do, is to "remove" the first part of `NS(Mutable)Data` if "read"... – Larme Nov 17 '22 at 08:25

0 Answers0