Re: C arrays as __block variables
On Sun, Jun 27, 2010 at 12:19 AM, Bill Bumgarner b...@mac.com wrote: On Jun 26, 2010, at 9:14 PM, Tony Romano wrote: That's why I asked for an example of what the op question is http://lists.apple.com/archives/cocoa-dev/2010/Jun/msg01040.html This would seem to imply that a __block variable *can* be a *fixed* length array. But when I try to write into such an array inside a block, I get a compile error, cannot access __block variable of array type inside block. void foo() { __block char barfy[100]; ^() { char b = barfy[0]; error: cannot access __block variable of array type inside block b = b; }; } void bar() { __block struct angus { char barfy[100]; } kangus; ^() { char b = kangus.barfy[0]; // compiles fine b = b; }; } The reason being that a Block_copy() or [block copy] will cause the __block variables to be moved to the heap and, thus, the compiler must know the exact size of all variables to be copied when emitting the copy helper. Sorry, could you elaborate on this point? Specifically, you seem to imply that the compiler does not know the size of __block char barfy[100]; (from foo()). Why does the compiler make any meaningful distinction between foo() and bar()? And why does wrapping the char array inside a struct cause it to work in bar(), but fail in foo()? I have yet to dig in to the pedantic details of the standard, but I'm fairly sure the standard guarantees that: 1) in foo(), sizeof(barfy) == (in bar()) sizeof(kangus) == (in bar()) sizeof(kangus.barfy) 2) in bar(), kangus == kangus.barfy == kangus.barfy[0] The way I read things, the two examples given are essentially indistinguishable semantically and mean the same thing for all practical purposes. In fact, off the top of my head, and a cursory glance at the standard, I can't find anything that makes any meaningful semantic difference between the two. ___ 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: C arrays as __block variables
- (void)_copyOrMoveURLs:(SEL) s opMove:(BOOL)op src:(NSDictionary *)URLs completionHandler:(void (^)(NSDictionary *newURLs, NSError *error))handler { __block char array1[5]; array1[0] = 'W'; NSBlockOperation * foo = [NSBlockOperation blockOperationWithBlock:^{ array1[1] = array1[0]; char b = array1[0]; NSLog(@char %c %c %c, array1[0], array1[1], b); }]; } Works fine using 3.2.2 prints out char W W W -Tony On Jun 26, 2010, at 9:19 PM, Bill Bumgarner wrote: On Jun 26, 2010, at 9:14 PM, Tony Romano wrote: That's why I asked for an example of what the op question is http://lists.apple.com/archives/cocoa-dev/2010/Jun/msg01040.html This would seem to imply that a __block variable *can* be a *fixed* length array. But when I try to write into such an array inside a block, I get a compile error, cannot access __block variable of array type inside block. void foo() { __block char barfy[100]; ^() { char b = barfy[0]; error: cannot access __block variable of array type inside block b = b; }; } void bar() { __block struct angus { char barfy[100]; } kangus; ^() { char b = kangus.barfy[0]; // compiles fine b = b; }; } The reason being that a Block_copy() or [block copy] will cause the __block variables to be moved to the heap and, thus, the compiler must know the exact size of all variables to be copied when emitting the copy helper. b.bum -Tony ___ 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: C arrays as __block variables
The reason for the __block specifier is to allow the variable to be written to. If the variable remains a const, then the complier can optimize how the block is stored. If the data is changed, much of the optimization is lost. -Tony On Jun 26, 2010, at 8:56 PM, Kyle Sluder wrote: On Sat, Jun 26, 2010 at 8:51 PM, Tony Romano tony...@hotmail.com wrote: OP posted: This would seem to imply that a __block variable *can* be a *fixed* length array. But when I try to write into such an array inside a block, I get a compile error, cannot access __block variable of array type inside block. In the example I listed below, I have a __block variable inside a block that is fixed length array and I can access it via NSLog(@char %c, array1[0]); Putting __block variables inside of blocks is completely pointless. The purpose of the __block qualifier is to mark variables in the enclosing scope to be copied into the block. --Kyle Sluder -Tony ___ 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: C arrays as __block variables
I had a situation where I was accessing a C array from inside a block. It worked fine until I upgraded to Xcode 3.2.3; then I started to get errors. -- David Catmull uncom...@uncommonplace.com http://www.uncommonplace.com/ ___ 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: C arrays as __block variables
In the example I listed below, I have a __block variable inside a block that is fixed length array and I can access it via NSLog(@char %c, array1[0]); Sure, but I'm talking about a __block variable declared outside the block: int arr[1]; ^(){ arr[0];}; // compile error __block int arr[1]; ^(){ arr[0]; }; // compile error! I think b.bum has explained this thoroughly; the workaround is: struct {int arr[1];} str; ^(){ str.arr[0];}; And now it can be made writable with __block as expected: __block struct {int arr[1];} str; ^(){ str.arr[0]=1;}; I would suggest the docs be a bit more forthcoming about this, unless it's documented and I just didn't spot it. Thx all - m. -- matt neuburg, phd = m...@tidbits.com, http://www.tidbits.com/matt/ pantes anthropoi tou eidenai oregontai phusei Among the 2007 MacTech Top 25, http://tinyurl.com/2rh4pf AppleScript: the Definitive Guide, 2nd edition http://www.tidbits.com/matt/default.html#applescriptthings Take Control of Exploring Customizing Snow Leopard http://tinyurl.com/kufyy8 RubyFrontier! http://www.apeth.com/RubyFrontierDocs/default.html TidBITS, Mac news and reviews since 1990, http://www.tidbits.com ___ 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: C arrays as __block variables
On Jun 26, 2010, at 11:09 PM, Tony Romano wrote: - (void)_copyOrMoveURLs:(SEL) s opMove:(BOOL)op src:(NSDictionary *)URLs completionHandler:(void (^)(NSDictionary *newURLs, NSError *error))handler { __block char array1[5]; array1[0] = 'W'; NSBlockOperation * foo = [NSBlockOperation blockOperationWithBlock:^{ array1[1] = array1[0]; char b = array1[0]; NSLog(@char %c %c %c, array1[0], array1[1], b); }]; } Works fine using 3.2.2prints out char W W W And it will [correctly] break in 3.2.3 (or a later compiler -- LLVM top-of-tree, for example). It is only working by coincidence. You can construct a form that doesn't work, if you are creative. b.bum ___ 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: C arrays as __block variables
On Jun 26, 2010, at 6:38 PM, Matt Neuburg wrote: The docs say: There are two further restrictions on __block variables: they cannot be variable length arrays, and cannot be structures that contain C99 variable-length arrays. This would seem to imply that a __block variable *can* be a *fixed* length array. But when I try to write into such an array inside a block, I get a compile error, cannot access __block variable of array type inside block. Who's mistaken, the compiler or the docs? Thx - m. Neither. The issue is that a block array in C is, technically, sorta-kinda variable length unless you do something to make it fixed length. I don't remember the exact details beyond that it is a subtle edge case. That something is typically to encapsulate it into a Struct. This: void foo() { char barfy[100]; ^() { char b = barfy[0]; error: cannot access copied-in variable of array type inside block b = b; }; } This compiles just fine: void bar() { struct angus { char barfy[100]; } kangus; ^() { char b = kangus.barfy[0]; b = b; }; } b.bum ___ 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: C arrays as __block variables
hmmm. Your saying this doesn't work? NSBlockOperation * foo = [NSBlockOperation blockOperationWithBlock:^{ __block char array1[5]; array1[0] = 'T'; }]; It works fine for me. Are you saying something different? -Tony On Jun 26, 2010, at 7:16 PM, Bill Bumgarner wrote: On Jun 26, 2010, at 6:38 PM, Matt Neuburg wrote: The docs say: There are two further restrictions on __block variables: they cannot be variable length arrays, and cannot be structures that contain C99 variable-length arrays. This would seem to imply that a __block variable *can* be a *fixed* length array. But when I try to write into such an array inside a block, I get a compile error, cannot access __block variable of array type inside block. Who's mistaken, the compiler or the docs? Thx - m. Neither. The issue is that a block array in C is, technically, sorta-kinda variable length unless you do something to make it fixed length. I don't remember the exact details beyond that it is a subtle edge case. That something is typically to encapsulate it into a Struct. This: void foo() { char barfy[100]; ^() { char b = barfy[0]; error: cannot access copied-in variable of array type inside block b = b; }; } This compiles just fine: void bar() { struct angus { char barfy[100]; } kangus; ^() { char b = kangus.barfy[0]; b = b; }; } b.bum ___ 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/tonyrom%40hotmail.com This email sent to tony...@hotmail.com -Tony ___ 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: C arrays as __block variables
That's going to create a new copy of the array every time the block is executed, and the array is not accessible outside of the scope of the block. Matt was asking about: __block char array1[5]; NSBlockOperation * foo = [NSBlockOperation blockOperationWithBlock:^{ array1[0] = 'T'; } Dave On Jun 26, 2010, at 8:48 PM, Tony Romano wrote: hmmm. Your saying this doesn't work? NSBlockOperation * foo = [NSBlockOperation blockOperationWithBlock:^{ __block char array1[5]; array1[0] = 'T'; }]; It works fine for me. Are you saying something different? -Tony smime.p7s Description: S/MIME cryptographic signature ___ 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: C arrays as __block variables
I understand all that. I was providing an example to seek clarity in his question. But I understand what he is asking now, thanks. -Tony On Jun 26, 2010, at 7:50 PM, Dave DeLong wrote: That's going to create a new copy of the array every time the block is executed, and the array is not accessible outside of the scope of the block. Matt was asking about: __block char array1[5]; NSBlockOperation * foo = [NSBlockOperation blockOperationWithBlock:^{ array1[0] = 'T'; } Dave On Jun 26, 2010, at 8:48 PM, Tony Romano wrote: hmmm. Your saying this doesn't work? NSBlockOperation * foo = [NSBlockOperation blockOperationWithBlock:^{ __block char array1[5]; array1[0] = 'T'; }]; It works fine for me. Are you saying something different? -Tony -Tony ___ 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: C arrays as __block variables
On Jun 26, 2010, at 7:48 PM, Tony Romano wrote: hmmm. Your saying this doesn't work? NSBlockOperation * foo = [NSBlockOperation blockOperationWithBlock:^{ __block char array1[5]; array1[0] = 'T'; }]; It works fine for me. Are you saying something different? No -- that works fine. That is a different expression than the one OP posted. b.bum ___ 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: C arrays as __block variables
Actually, I read this again. OP posted: This would seem to imply that a __block variable *can* be a *fixed* length array. But when I try to write into such an array inside a block, I get a compile error, cannot access __block variable of array type inside block. In the example I listed below, I have a __block variable inside a block that is fixed length array and I can access it via NSLog(@char %c, array1[0]); Please post the example you are having problems with. Thanks, -Tony On Jun 26, 2010, at 8:16 PM, Bill Bumgarner wrote: On Jun 26, 2010, at 7:48 PM, Tony Romano wrote: hmmm. Your saying this doesn't work? NSBlockOperation * foo = [NSBlockOperation blockOperationWithBlock:^{ __block char array1[5]; array1[0] = 'T'; }]; It works fine for me. Are you saying something different? No -- that works fine. That is a different expression than the one OP posted. b.bum -Tony ___ 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: C arrays as __block variables
On Sat, Jun 26, 2010 at 8:51 PM, Tony Romano tony...@hotmail.com wrote: OP posted: This would seem to imply that a __block variable *can* be a *fixed* length array. But when I try to write into such an array inside a block, I get a compile error, cannot access __block variable of array type inside block. In the example I listed below, I have a __block variable inside a block that is fixed length array and I can access it via NSLog(@char %c, array1[0]); Putting __block variables inside of blocks is completely pointless. The purpose of the __block qualifier is to mark variables in the enclosing scope to be copied into the block. --Kyle Sluder ___ 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: C arrays as __block variables
On Jun 26, 2010, at 8:56 PM, Kyle Sluder wrote: Putting __block variables inside of blocks is completely pointless. The purpose of the __block qualifier is to mark variables in the enclosing scope to be copied into the block. Unless you have a Block within the Block and you want to share and update the variable across both... b.bum ___ 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: C arrays as __block variables
That's why I asked for an example of what the op question is Sent from my phone, Thanks Tony On Jun 26, 2010, at 8:56 PM, Kyle Sluder kyle.slu...@gmail.com wrote: On Sat, Jun 26, 2010 at 8:51 PM, Tony Romano tony...@hotmail.com wrote: OP posted: This would seem to imply that a __block variable *can* be a *fixed* length array. But when I try to write into such an array inside a block, I get a compile error, cannot access __block variable of array type inside block. In the example I listed below, I have a __block variable inside a block that is fixed length array and I can access it via NSLog(@char %c, array1[0]); Putting __block variables inside of blocks is completely pointless. The purpose of the __block qualifier is to mark variables in the enclosing scope to be copied into the block. --Kyle Sluder ___ 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: C arrays as __block variables
On Jun 26, 2010, at 9:14 PM, Tony Romano wrote: That's why I asked for an example of what the op question is http://lists.apple.com/archives/cocoa-dev/2010/Jun/msg01040.html This would seem to imply that a __block variable *can* be a *fixed* length array. But when I try to write into such an array inside a block, I get a compile error, cannot access __block variable of array type inside block. void foo() { __block char barfy[100]; ^() { char b = barfy[0]; error: cannot access __block variable of array type inside block b = b; }; } void bar() { __block struct angus { char barfy[100]; } kangus; ^() { char b = kangus.barfy[0]; // compiles fine b = b; }; } The reason being that a Block_copy() or [block copy] will cause the __block variables to be moved to the heap and, thus, the compiler must know the exact size of all variables to be copied when emitting the copy helper. b.bum ___ 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