Re: An Array of structs
On Nov 27, 2009, at 7:48 AM, Alastair Houghton wrote: > You *can* have non-constant elements in struct initialisers in C, *but* only > in a context where there is code that will execute at runtime. C++ eliminates most of these restrictions, btw. And you can make your code C++ simply by changing the suffix from ".m" to ".mm". That said, I've found that putting Obj-C object pointers in C/C++ structs can be very painful, due to refcounting issues. It's easy to copy the structs around, but doing so doesn't bump the refcounts of the objects they point to, so you can end up with dangling pointers to dealloced objects. (There are ways around this but they involve building C++ smart pointer classes to manage the Obj-C refcounts...) —Jens___ 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: An Array of structs
On 27 Nov 2009, at 13:30, Jeremy Pereira wrote: > You can't have non constant elements in struct initialisers, testy is not a > constant. The error message spells out exactly what the issue is. You *can* have non-constant elements in struct initialisers in C, *but* only in a context where there is code that will execute at runtime. So struct foo { int foo; }; int fn (int anInt) { struct foo fooArray[] = { { anInt }, { anInt + 1 } }; } is quite legitimate, while as you rightly point out extern int anInt; struct foo { int foo; } struct foo fooArray[] = { { anInt }, { anInt + 1 } }; isn't. In the case of C89/C90, this is an extension provided by GNU C and by some other compilers. It's a formal part of C99, however (see §6.7.8 ¶4 - "All the expressions in an initializer for an object that has static storage duration shall be constant expressions or string literals."). In C++, however, the standard says that it's allowed wherever, including for objects that have static storage (see §8.5 ¶2), so in C++, both of those pieces of code should compile. There are some caveats, however, in that the order of initialisation of objects is not specified; the standard does guarantee that, if they haven't yet been initialised dynamically, they'll have been temporarily initialised to zeroes (§8.5 ¶6). Given that there are additionally some issues surrounding the implementation of this feature (e.g. it may be necessary to call a special function to perform these initialisations on some platforms if the module in question doesn't have a conventional main() function, for whatever reason), it's probably best to avoid relying on this ability. Kind regards, Alastair. -- http://alastairs-place.net ___ 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: An Array of structs
On 27 Nov 2009, at 11:35, John Love wrote: > > I get "Initializer element is not constant. This pertains to the NSString* > because if I directly substitute the following, no compile error happens: > YearAmt gTest[] = {{@"testy", 10} > > What fundamental pertaining to C or C++ am I not getting? You can't have non constant elements in struct initialisers, testy is not a constant. The error message spells out exactly what the issue is. This is not, by the way, an NSString specific issue. The following gives the same error typedef struct Foo { int foo; } Foo; int anInt = 6; Foo fooArray[] = {{ anInt }}; Use of the "const" keyword doesn't help. > > > John Love > Touch the Future! Teach! > > > > ___ > > 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/adc%40jeremyp.net > > This email sent to a...@jeremyp.net __ This email has been scanned by the MessageLabs Email Security System. For more information please visit http://www.messagelabs.com/email __ ___ 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: An Array of structs
On 27/11/2009, at 10:35 PM, John Love wrote: > NSString *testy = @"testy"; > YearAmt gTest[] = {{testy, 10} /*, + others */}; > > I get "Initializer element is not constant. This pertains to the NSString* > because if I directly substitute the following, no compile error happens: > YearAmt gTest[] = {{@"testy", 10} > > What fundamental pertaining to C or C++ am I not getting? The error tells you - the initialiser is not constant. Sure, it's constant in the first line, but 'testy' is a variable, so you are trying to use a variable as a constant initializer in the second line. That isn't allowed. If you think about how constant initializers in C actually work, at the machine code/assembler level, it should be clear why it isn't allowed. --Graham ___ 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
An Array of structs
In my .h file, I have: typedef struct YearAmt { NSString *year; int amount; } YearAmt; extern NSString *testy; extern YearAmt gTest[]; In the corresponding .m file: NSString *testy = @"testy"; YearAmt gTest[] = {{testy, 10} /*, + others */}; I get "Initializer element is not constant. This pertains to the NSString* because if I directly substitute the following, no compile error happens: YearAmt gTest[] = {{@"testy", 10} What fundamental pertaining to C or C++ am I not getting? John Love Touch the Future! Teach! ___ 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