On 21 Sep 2013, at 23:48, Paul Scott <[email protected]> wrote:

> The "important" brace means that the tupels array is block local. You then 
> print the block local variable from outside the block, which is no longer 
> valid.
> 
Yes, tupels lives only inside the block. But:

1. tupelsP lives outside the block.
2. why does the number of things in tupels[] makes a difference?
3. why does printing the tupelsP inside the block makes a difference?
4. Why is there no problem with the same code in OS X?
5. Why only with structs? Using some double[] array shows no problems.
6. If this is NOT legal C-code, why does the compiler not complain?

This final (and most important) question: is this legal C-code, and if not, 
which C-rule gets violated?

Kind regards,

Gerriet.


>> On Sep 21, 2013, at 9:00 AM, "Gerriet M. Denkmann" <[email protected]> 
>> wrote:
>> 
>> I just created a new iOS app - universal, Master-Detail, no CoreData and 
>> added the following into application:didFinishLaunchingWithOptions:
>> 
>> // --- #define BUG_FIX1
>> 
>> #if        DEBUG
>>   NSLog(@"%s Debug build - no bugs here",__FUNCTION__);
>> #else    //    Release
>>   #ifdef BUG_FIX1
>>       NSLog(@"%s Release build with bug-fix",__FUNCTION__);
>>   #else
>>       NSLog(@"%s Release build with bug",__FUNCTION__);
>>   #endif
>> #endif    //    debug or release
>> 
>> typedef struct
>> {
>>   double    size;
>>   double    value;
>> 
>> } tupel_t;
>> 
>> tupel_t *tupelsP;
>> NSUInteger nbrOfTupels;
>> 
>> //    the parenthesis in the next line is important
>> {    
>>   tupel_t tupels[] =    //    need more than 2 tupels for the bug
>>   {
>>       { 6, 8},
>>       {48, 8},
>>       {99, 7}
>>   };
>> 
>>   tupelsP = tupels;
>>   nbrOfTupels = sizeof(tupels) / sizeof(tupel_t);
>>   #ifdef BUG_FIX1
>>       NSLog(@"%s tupelsP = %p",__FUNCTION__, tupelsP);    //    printing 
>> tupelsP fixes the bug
>>   #else
>>       NSLog(@"%s nbrOfTupels %lu",__FUNCTION__, (unsigned long)nbrOfTupels);
>>   #endif
>> }
>> 
>> NSLog(@"%s tupelsP %p first (should be 6): %g last (should be 99): 
>> %g",__FUNCTION__, 
>>   tupelsP, tupelsP->size, (tupelsP+nbrOfTupels-1)->size);
>> 
>> Then run the Release build and got:
>> 
>> tupelsP 0xbfffcaf8 first (should be 6): 2.00737e-302 last (should be 99): 
>> 2.09289e-302
>> 
>> Is this legal C-code (the compiler thinks it is)?
>> 
>> What am I doing wrong?
>> 
>> Gerriet.
>> 


_______________________________________________

Cocoa-dev mailing list ([email protected])

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:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to [email protected]

Reply via email to