Re: Is it a bad idea to invoke "init..." over and over on the same object?
Re: Is it a bad idea to invoke "init..." over and over on the same object?
- Subject: Re: Is it a bad idea to invoke "init..." over and over on the same object?
- From: Andrei Tchijov <email@hidden>
- Date: Wed, 2 Nov 2005 10:25:53 -0500
Daniel (and Jack)
Thanks for quick reply. I did consider use of
"dataWithBytes:length:" but I was trying to avoid allocation of new
NSData object altogether. How about this aproach:
...
int bigIoBufferSize = xxx;
ioData = [[ NSMutableData alloc ] initWithCapacity: bigIoBufferSize ];
...
- (NSData*) readData {
char* bigIoBuffer = [ ioData mutableBytes ];
...
read data into bigIoBuffer
...
[ ioData setLength: bytesRead ];
return ioData;
}
...
In this scenario question is:
- Can we expect that NSMutableData will not re-allocate "bytes" as a
result of "setLength" if "length" is always less or equal initial
capacity?
On Nov 2, 2005, at 10:14 AM, Daniel Jalkut wrote:
Andrei - it's definitely a bad idea to init an object over and
over. You should be allocating a new NSData object here every time
you need one. An appropriate convenience method can be found in the
"dataWithBytes:"family of class methods on NSData. E.g.:
return [NSData dataWithBytes:bigIoBuffer length:bytesRead];
Daniel
On Nov 2, 2005, at 9:57 AM, Andrei Tchijov wrote:
What I am trying to achieve is to have NSData object which wraps
IO buffer. For example:
...
int bigIoBufferSize = xxx;
char* bigIoBuffer = (char*) malloc( bigIoBufferSize );
NSData* ioData = [ NSData alloc ];
...
- (NSData*) readData {
...
read data into bigIoBuffer
save number of bytes read in bytesRead
...
return [ ioData initWithBytesNoCopy: bigIoBuffer length:
bytesRead ];
}
...
- (void) copyData {
NSData* dataIn = [ self readData ];
[ self writeData: dataIn ];
}
...
So questions are:
1) Is it legal to do initWithBytesNoCopy:length: more then once
on the same object?
2) If it is, will NSData just forget about "current" bytes and
change internal pointers to point to new one or will it try to
free it?
You comments are greatly appreciated,
Andrei Tchijov
Leaping Bytes, LLC
web: www.leapingbytes.com
email: email@hidden
AIM: leapingbytes
Google: email@hidden
ICQ: 151229319
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
sweater.com
This email sent to email@hidden
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden