Re: dataWithBytesNoCopy:length:freeWhenDone
On 2011-10-09, at 05:05, Quincey Morris wrote: >> What am I doing wrong? Shouldn't the instance created using >> dataWithBytesNoCopy:length:freeWhenDone simply hold the same bytes as >> the originally provided ones? > > Presumably you're using 'dataWithBytesNoCopy:length:freeWhenDone:NO', since > 'dataWithBytesNoCopy:length:freeWhenDone:YES' would be invalid in the > scenario you describe. Right. > I'd guess that, with the 'NO' parameter, NSMutableData copies the data > anyway. Mutating the data would become treacherous if the underlying memory > doesn't belong to the NSMutableData object that's trying to mutate it. That would explain, although the "NoCopy" is in that case moot then. > So, really all you could do is change individual bytes, and you scarcely need > subsidiary NSMutableData objects for that -- simple C pointers would do. > (Note, however, that you'd be using interior pointers, which would open you > up to a range of possible memory management issues, depending on which memory > model you're using.) I see. And agree. > What are you trying to get the subsidiary NSMutableData objects to do for you > that C pointers won't? No - I mean I can do all those things I need to do by laying a set of structs over the array of bytes I receive within the original NSMutableData instance. But what I tried to achieve was to do it somewhat more "OO", although sharing the payload between various instances seems to be contradicting it ;-) And I also wanted to minimise the overhead of passing and copying large amounts of data around in order to do the manipulations, while at the end having a need to reconstruct the original stream (file format) from multiple objects. Regards, -- SD!___ Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) Please do not post admin requests or moderator comments to the list. Contact the moderators at cocoa-dev-admins(at)lists.apple.com Help/Unsubscribe/Update your Subscription: http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com This email sent to arch...@mail-archive.com
Re: dataWithBytesNoCopy:length:freeWhenDone
On 2011-10-09, at 05:50, Ken Thomases wrote: >> I'd guess that, with the 'NO' parameter, NSMutableData copies the data >> anyway. > > This is actually documented. In the Binary Data Programming Guide, in the > article Working With Binary Data[1], it says: > >> However, if you create an NSData object with one of the methods whose name >> includes NoCopy (such as dataWithBytesNoCopy:length:), the bytes are not >> copied. Instead, the data object takes ownership of the bytes passed in as >> an argument and frees them when the object is released. (NSMutableData >> responds to these methods, too, but the bytes are copied anyway and the >> buffer is freed immediately.) I see. I even read this document but it seems I was reading too fast and I missed the part in parentheses. Thank you for pointing out. Although I believe this should rather be placed in the class documentation.. Regards, -- SD!___ Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) Please do not post admin requests or moderator comments to the list. Contact the moderators at cocoa-dev-admins(at)lists.apple.com Help/Unsubscribe/Update your Subscription: http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com This email sent to arch...@mail-archive.com
dataWithBytesNoCopy:length:freeWhenDone
Hello group, I have a large NSMutableData, which I divide into some logical structure elements creating new NSMutableData instances, which are supposed to hold a subset of bytes the large instance holds. Yet I want all the manipulations done on the bytes of the "small" instances to be reflected in the "large" one. I tried creating the "small" instances using dataWithBytesNoCopy:length:freeWhenDone: but it doesn't seem to work as expected - mutableBytes of the newly created instance point elsewhere than the original mutableBytes. What am I doing wrong? Shouldn't the instance created using dataWithBytesNoCopy:length:freeWhenDone simply hold the same bytes as the originally provided ones? Hoping for some help. Thanks in advance. ___ Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) Please do not post admin requests or moderator comments to the list. Contact the moderators at cocoa-dev-admins(at)lists.apple.com Help/Unsubscribe/Update your Subscription: http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com This email sent to arch...@mail-archive.com