Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c > #3855 > unrolled thread

Non-constant initializers

Started byFred the Freshwater Catfish <usenet.fred@bdtd.invalid>
First post2011-05-12 22:59 +0000
Last post2011-05-13 23:57 +0000
Articles 14 on this page of 94 — 22 participants

Back to article view | Back to comp.lang.c

This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by below is the oldest one visible, not the original post.


Contents

  Non-constant initializers Fred the Freshwater Catfish <usenet.fred@bdtd.invalid> - 2011-05-12 22:59 +0000
    Re: Non-constant initializers Ian Collins <ian-news@hotmail.com> - 2011-05-13 11:07 +1200
      Re: Non-constant initializers Fred the Freshwater Catfish <usenet.fred@bdtd.invalid> - 2011-05-12 23:17 +0000
        Re: Non-constant initializers Ian Collins <ian-news@hotmail.com> - 2011-05-13 11:20 +1200
          Re: Non-constant initializers Fred the Freshwater Catfish <usenet.fred@bdtd.invalid> - 2011-05-12 23:30 +0000
            Re: Non-constant initializers Ian Collins <ian-news@hotmail.com> - 2011-05-13 11:34 +1200
              Re: Non-constant initializers Keith Thompson <kst-u@mib.org> - 2011-05-12 16:52 -0700
                Re: Non-constant initializers Fred the Freshwater Catfish <usenet.fred@bdtd.invalid> - 2011-05-14 00:13 +0000
                  Re: Non-constant initializers Seebs <usenet-nospam@seebs.net> - 2011-05-14 01:28 +0000
                    Re: Non-constant initializers Uncle Steve <stevet810@gmail.com> - 2011-05-14 13:52 -0400
                  Re: Non-constant initializers kym@kymhorsell.com - 2011-05-14 01:54 +0000
                    Re: Non-constant initializers Columbus sailed the ocean China Blue <chine.bleu@yahoo.com> - 2011-05-14 01:07 -0700
                      Re: Non-constant initializers kym@kymhorsell.com - 2011-05-14 09:22 +0000
                        Re: Non-constant initializers Fred the Freshwater Catfish <usenet.fred@bdtd.invalid> - 2011-05-14 17:14 +0000
              Re: Non-constant initializers Fred the Freshwater Catfish <usenet.fred@bdtd.invalid> - 2011-05-14 00:11 +0000
    Re: Non-constant initializers Shao Miller <sha0.miller@gmail.com> - 2011-05-12 18:08 -0500
      Re: Non-constant initializers Uncle Steve <stevet810@gmail.com> - 2011-05-12 19:22 -0400
        Re: Non-constant initializers Shao Miller <sha0.miller@gmail.com> - 2011-05-12 18:53 -0500
          Re: Non-constant initializers Uncle Steve <stevet810@gmail.com> - 2011-05-14 13:59 -0400
            Re: Non-constant initializers Eric Sosman <esosman@ieee-dot-org.invalid> - 2011-05-14 14:19 -0400
              Re: Non-constant initializers Shao Miller <sha0.miller@gmail.com> - 2011-05-14 15:04 -0500
              Re: Non-constant initializers Uncle Steve <stevet810@gmail.com> - 2011-05-14 15:10 -0400
                Re: Non-constant initializers James Kuyper <jameskuyper@verizon.net> - 2011-05-14 16:01 -0400
                  Re: Non-constant initializers Uncle Steve <stevet810@gmail.com> - 2011-05-14 16:57 -0400
            Re: Non-constant initializers Shao Miller <sha0.miller@gmail.com> - 2011-05-14 14:30 -0500
              Re: Non-constant initializers Uncle Steve <stevet810@gmail.com> - 2011-05-14 15:25 -0400
                Re: Non-constant initializers Keith Thompson <kst-u@mib.org> - 2011-05-14 13:05 -0700
                  Re: Non-constant initializers Uncle Steve <stevet810@gmail.com> - 2011-05-14 17:00 -0400
                Re: Non-constant initializers Shao Miller <sha0.miller@gmail.com> - 2011-05-14 16:14 -0500
                  Re: Non-constant initializers Shao Miller <sha0.miller@gmail.com> - 2011-05-14 16:49 -0500
                  Re: Non-constant initializers Uncle Steve <stevet810@gmail.com> - 2011-05-14 18:21 -0400
                    Re: Non-constant initializers Shao Miller <sha0.miller@gmail.com> - 2011-05-14 21:26 -0500
                      Re: Non-constant initializers Uncle Steve <stevet810@gmail.com> - 2011-05-15 14:01 -0400
                Re: Non-constant initializers Eric Sosman <esosman@ieee-dot-org.invalid> - 2011-05-14 17:45 -0400
                  Re: Non-constant initializers Uncle Steve <stevet810@gmail.com> - 2011-05-14 18:09 -0400
                    Re: Non-constant initializers Ben Bacarisse <ben.usenet@bsb.me.uk> - 2011-05-15 04:01 +0100
                      Re: Non-constant initializers Uncle Steve <stevet810@gmail.com> - 2011-05-15 13:26 -0400
                        Re: Non-constant initializers Ben Bacarisse <ben.usenet@bsb.me.uk> - 2011-05-16 02:32 +0100
                Re: Non-constant initializers Seebs <usenet-nospam@seebs.net> - 2011-05-15 03:59 +0000
                  Re: Non-constant initializers Uncle Steve <stevet810@gmail.com> - 2011-05-16 04:49 -0400
                    Re: Non-constant initializers pete <pfiland@mindspring.com> - 2011-05-16 07:27 -0400
                      Re: Non-constant initializers Uncle Steve <stevet810@gmail.com> - 2011-05-16 09:24 -0400
                        Re: Non-constant initializers Seebs <usenet-nospam@seebs.net> - 2011-05-16 19:10 +0000
                          Re: Non-constant initializers Uncle Steve <stevet810@gmail.com> - 2011-05-16 21:23 -0400
                    Re: Non-constant initializers Seebs <usenet-nospam@seebs.net> - 2011-05-16 19:10 +0000
                      Re: Non-constant initializers Uncle Steve <stevet810@gmail.com> - 2011-05-16 21:16 -0400
                        Re: Non-constant initializers James Kuyper <jameskuyper@verizon.net> - 2011-05-16 21:37 -0400
                          Re: Non-constant initializers Uncle Steve <stevet810@gmail.com> - 2011-05-16 22:42 -0400
                        Re: Non-constant initializers Seebs <usenet-nospam@seebs.net> - 2011-05-17 02:40 +0000
                          Re: Non-constant initializers Uncle Steve <stevet810@gmail.com> - 2011-05-16 22:50 -0400
                            Re: Non-constant initializers Seebs <usenet-nospam@seebs.net> - 2011-05-17 04:14 +0000
                              Re: Non-constant initializers Uncle Steve <stevet810@gmail.com> - 2011-05-17 17:37 -0400
                                Re: Non-constant initializers Seebs <usenet-nospam@seebs.net> - 2011-05-17 22:22 +0000
                                  Re: Non-constant initializers Uncle Steve <stevet810@gmail.com> - 2011-05-17 18:33 -0400
                                    Re: Non-constant initializers Malcolm McLean <malcolm.mclean5@btinternet.com> - 2011-05-18 01:58 -0700
                                      Re: Non-constant initializers Uncle Steve <stevet810@gmail.com> - 2011-05-18 12:45 -0400
                                    Re: Non-constant initializers "io_x" <a@b.c.invalid> - 2011-05-18 19:30 +0200
                                      Re: Non-constant initializers Dr Nick <3-nospam@temporary-address.org.uk> - 2011-05-18 20:01 +0100
                                    Re: bug-free C code (was: Non-constant initializers) Seebs <usenet-nospam@seebs.net> - 2011-05-19 02:21 +0000
                                      Re: bug-free C code (was: Non-constant initializers) Keith Thompson <kst-u@mib.org> - 2011-05-18 20:00 -0700
                                        Re: bug-free C code (was: Non-constant initializers) Seebs <usenet-nospam@seebs.net> - 2011-05-19 03:14 +0000
                                          Re: bug-free C code (was: Non-constant initializers) Keith Thompson <kst-u@mib.org> - 2011-05-19 07:49 -0700
                                            Re: bug-free C code (was: Non-constant initializers) Seebs <usenet-nospam@seebs.net> - 2011-05-19 15:16 +0000
                                    Re: bug-free C code Ben Pfaff <blp@cs.stanford.edu> - 2011-05-18 20:02 -0700
                                      Re: bug-free C code Keith Thompson <kst-u@mib.org> - 2011-05-18 20:09 -0700
                                        Re: bug-free C code Eric Sosman <esosman@ieee-dot-org.invalid> - 2011-05-18 23:24 -0400
                                          Re: bug-free C code Angel <angel+news@spamcop.net> - 2011-05-19 06:39 +0000
                                            Re: bug-free C code Keith Thompson <kst-u@mib.org> - 2011-05-19 08:05 -0700
                                              Re: bug-free C code Angel <angel+news@spamcop.net> - 2011-05-19 16:22 +0000
                                                Re: bug-free C code Keith Thompson <kst-u@mib.org> - 2011-05-19 11:36 -0700
                                                  Re: bug-free C code Ben Pfaff <blp@cs.stanford.edu> - 2011-05-19 12:21 -0700
                                                    Re: bug-free C code Keith Thompson <kst-u@mib.org> - 2011-05-19 13:12 -0700
                                                      Re: bug-free C code gazelle@shell.xmission.com (Kenny McCormack) - 2011-05-19 21:52 +0000
                                                        Re: bug-free C code Angel <angel+news@spamcop.net> - 2011-05-19 22:16 +0000
                                                          Re: bug-free C code Shao Miller <sha0.miller@gmail.com> - 2011-05-19 17:25 -0500
                                                          Re: bug-free C code Kenneth Brody <kenbrody@spamcop.net> - 2011-05-20 13:32 -0400
                                                  Re: bug-free C code gazelle@shell.xmission.com (Kenny McCormack) - 2011-05-19 21:51 +0000
                                                    Re: bug-free C code Kenneth Brody <kenbrody@spamcop.net> - 2011-05-20 13:30 -0400
                                    Re: bug-free C code "Joel C. Salomon" <joelcsalomon@gmail.com> - 2011-05-18 23:13 -0400
                                      Re: bug-free C code Dr Nick <3-nospam@temporary-address.org.uk> - 2011-05-19 08:23 +0100
        Re: Non-constant initializers Seebs <usenet-nospam@seebs.net> - 2011-05-13 00:15 +0000
        Re: Non-constant initializers "io_x" <a@b.c.invalid> - 2011-05-13 11:01 +0200
      Re: Non-constant initializers Columbus sailed the ocean China Blue <chine.bleu@yahoo.com> - 2011-05-12 16:30 -0700
    Re: Non-constant initializers Columbus sailed the ocean China Blue <chine.bleu@yahoo.com> - 2011-05-12 16:15 -0700
      Re: Non-constant initializers Fred the Freshwater Catfish <usenet.fred@bdtd.invalid> - 2011-05-12 23:28 +0000
    Re: Non-constant initializers Joe Pfeiffer <pfeiffer@cs.nmsu.edu> - 2011-05-12 18:01 -0600
      Re: Non-constant initializers Keith Thompson <kst-u@mib.org> - 2011-05-12 18:15 -0700
        Re: Non-constant initializers Joe Pfeiffer <pfeiffer@cs.nmsu.edu> - 2011-05-12 22:56 -0600
          Re: Non-constant initializers Seebs <usenet-nospam@seebs.net> - 2011-05-13 05:18 +0000
            Re: Non-constant initializers Joe Pfeiffer <pfeiffer@cs.nmsu.edu> - 2011-05-13 09:10 -0600
              Re: Non-constant initializers Seebs <usenet-nospam@seebs.net> - 2011-05-13 20:56 +0000
        Re: Non-constant initializers Tim Rentsch <txr@alumni.caltech.edu> - 2011-05-18 23:47 -0700
          Re: Non-constant initializers Keith Thompson <kst-u@mib.org> - 2011-05-19 08:15 -0700
      Re: Non-constant initializers Fred the Freshwater Catfish <usenet.fred@bdtd.invalid> - 2011-05-13 23:57 +0000

Page 5 of 5 — ← Prev page 1 2 3 4 [5]


#3878

FromSeebs <usenet-nospam@seebs.net>
Date2011-05-13 00:15 +0000
Message-ID<slrnisot5r.24cc.usenet-nospam@guild.seebs.net>
In reply to#3865
On 2011-05-12, Uncle Steve <stevet810@gmail.com> wrote:
> Essentially, I'm looking to seed a few critical global variables with
> run-time computed values.  Obviously, this will be the product of a
> function call.  I would like to declare:

> int foops[] = howmuchfoo(void);

> and have everything work as I expect.  Pie in the sky for non-recent
> UN*Xes?

IMHO, incoherent.  When, EXACTLY, do you want "howmuchfoo()" to run?
Do you anticipate that main() will have been called yet?  Do you expect
the C library to be available?

The reason the singleton hack exists is that what you want here is something
that's only meaningfully specified in C++.

-s
-- 
Copyright 2011, all wrongs reversed.  Peter Seebach / usenet-nospam@seebs.net
http://www.seebs.net/log/ <-- lawsuits, religion, and funny pictures
http://en.wikipedia.org/wiki/Fair_Game_(Scientology) <-- get educated!
I am not speaking for my employer, although they do rent some of my opinions.

[toc] | [prev] | [next] | [standalone]


#3900

From"io_x" <a@b.c.invalid>
Date2011-05-13 11:01 +0200
Message-ID<4dccf30f$0$18248$4fafbaef@reader2.news.tin.it>
In reply to#3865
"Uncle Steve" <stevet810@gmail.com> ha scritto nel messaggio
news:e68b19561ee2543-31bc7@gmail.com...
> On Thu, May 12, 2011 at 06:08:46PM -0500, Shao Miller wrote:
>> On 5/12/2011 5:59 PM, Fred the Freshwater Catfish wrote:
>> >...I'd like to use non-constant
>> >initializers for some global variables.  I'm pretty sure I can get it
>> >to work the way I want under gcc, but what of other compilers?  The
>> >gcc documentation in info format says that ISO C99 allows this, and so
>> >I expect some compilers built in the 90's will support the feature,
>> >but to what extent is this true?
>>
>> Could you please share an example of what kind of non-constant you'd be
>> inclined to initialize a static-duration, file-scoped object with?  Are
>> you talking about using a function's return value?  Are you talking
>> about using the address of something whose address you don't know at
>> some point in time?
>
> Essentially, I'm looking to seed a few critical global variables with
> run-time computed values.  Obviously, this will be the product of a
> function call.  I would like to declare:
>
> int foops[] = howmuchfoo(void);
>
> and have everything work as I expect.  Pie in the sky for non-recent
> UN*Xes?


int foops[] ; // = howmuchfoo(void);
int *foops1  ;
int *foops2  ;
int *foops3  ;

int  initializeGloabals(void)
{foops=howmuchfoo();
 if(foops==0)  return 0;
 foops1=howmuchfoo1();
 if(foops1==0)
   {la;
    free(foops);
    return 0;
   }
 foops2=howmuchfoo2();
 if(foops2==0)
   {la1;
    free(foops1);
    goto la;
   }
 foops3=howmuchfoo3();
 if(foops3==0)
   {la2;
    free(foops2);
    goto la1;
   }
 etc
}

void freeGlobals(void)
{free(foops);free(foops1);free(foops2);
 free(foops3);
 etc
}
int main(void)
{int  i=initializeGloabals();
 if(i==0) goto end;
 ....
 ...
 ...
 freeGloabals();
end:
return 0;
}

>
>
> Regards,
>
> Uncle Steve
>
> -- 
> Should a professional politician be charged with molestation if he
> kisses babies while attending political rallies?
>


[toc] | [prev] | [next] | [standalone]


#3868

FromColumbus sailed the ocean China Blue <chine.bleu@yahoo.com>
Date2011-05-12 16:30 -0700
Message-ID<chine.bleu-95715D.16300512052011@reserved-multicast-range-not-delegated.example.com>
In reply to#3859
In article <iqhpa0$n31$1@dont-email.me>, Shao Miller <sha0.miller@gmail.com> 
wrote:

> On 5/12/2011 5:59 PM, Fred the Freshwater Catfish wrote:
> > ...I'd like to use non-constant
> > initializers for some global variables.  I'm pretty sure I can get it
> > to work the way I want under gcc, but what of other compilers?  The
> > gcc documentation in info format says that ISO C99 allows this, and so
> > I expect some compilers built in the 90's will support the feature,
> > but to what extent is this true?
> 
> Could you please share an example of what kind of non-constant you'd be 
> inclined to initialize a static-duration, file-scoped object with?  Are 
> you talking about using a function's return value?  Are you talking 
> about using the address of something whose address you don't know at 
> some point in time?

This is allowed in C++. The compiler writes out an initialisation section to the 
object file. The loader then puts all these initialisation sections together and 
runs them before running main(). It would be a simple extension to gcc since g++ 
and gcc use the same code generator.

-- 
Damn the living - It's a lovely life.           I'm whoever you want me to be.
Silver silverware - Where is the love?       At least I can stay in character.
Oval swimming pool - Where is the love?    Annoying Usenet one post at a time.
Damn the living - It's a lovely life.                              In 1492....

[toc] | [prev] | [next] | [standalone]


#3860

FromColumbus sailed the ocean China Blue <chine.bleu@yahoo.com>
Date2011-05-12 16:15 -0700
Message-ID<chine.bleu-C88A78.16153612052011@reserved-multicast-range-not-delegated.example.com>
In reply to#3855
In article <OgcdY5.Lcq.Cst0A@btdt.invalid>,
 Fred the Freshwater Catfish <usenet.fred@bdtd.invalid> wrote:

> Hey guys,
> 
> I've been writing some code that I would prefer to be maximally
> portable.  As I have mostly used Linux, gcc is pretty much the only C
> compiler I've ever used under UN*X.  I'd like to use non-constant
> initializers for some global variables.  I'm pretty sure I can get it
> to work the way I want under gcc, but what of other compilers?  The
> gcc documentation in info format says that ISO C99 allows this, and so
> I expect some compilers built in the 90's will support the feature,
> but to what extent is this true?
> 
> What I mean to learn is the general prevalence of this C feature.  If
> there's no support for certain well-known platforms (maybe SunOS or
> OSF/1, whatever) I may consider a different solution to the original
> problem, but I'd like to know what I'm getting in to before I start
> trying to read the docs for every bloody C compiler that ever hit the
> 'net in the last twenty years.

I use an alternative strategy: if I want to implement
        static double XYZ = nonconstant-expression;

I instead do
        #define XYZ (*XYZ_())
        static double *XYZ_(void) {
            static double xyz;
            static bool initialised = false;
            LOCK
                if (!initialised) {
                    xyz = nonconstant-expression;
                    initialised = true;
                }
            UNLOCK
            return &xyz;
        }

XYZ will be an lvalue just like any other variable; it gets initialised on the 
first use; the initialisation can be any complicated as desired, using any 
dependencies desired, without depending on the compiler and loader to sneak in 
the initialisation code in the right order.

-- 
Damn the living - It's a lovely life.           I'm whoever you want me to be.
Silver silverware - Where is the love?       At least I can stay in character.
Oval swimming pool - Where is the love?    Annoying Usenet one post at a time.
Damn the living - It's a lovely life.                              In 1492....

[toc] | [prev] | [next] | [standalone]


#3867

FromFred the Freshwater Catfish <usenet.fred@bdtd.invalid>
Date2011-05-12 23:28 +0000
Message-ID<i1u9RB.8qd.j2hEc@btdt.invalid>
In reply to#3860
On Thu, May 12, 2011 at 04:15:36PM -0700, Columbus sailed the ocean China Blue wrote:
> In article <OgcdY5.Lcq.Cst0A@btdt.invalid>,
>  Fred the Freshwater Catfish <usenet.fred@bdtd.invalid> wrote:
> 
> > Hey guys,
> > 
> > I've been writing some code that I would prefer to be maximally
> > portable.  As I have mostly used Linux, gcc is pretty much the only C
> > compiler I've ever used under UN*X.  I'd like to use non-constant
> > initializers for some global variables.  I'm pretty sure I can get it
> > to work the way I want under gcc, but what of other compilers?  The
> > gcc documentation in info format says that ISO C99 allows this, and so
> > I expect some compilers built in the 90's will support the feature,
> > but to what extent is this true?
> > 
> > What I mean to learn is the general prevalence of this C feature.  If
> > there's no support for certain well-known platforms (maybe SunOS or
> > OSF/1, whatever) I may consider a different solution to the original
> > problem, but I'd like to know what I'm getting in to before I start
> > trying to read the docs for every bloody C compiler that ever hit the
> > 'net in the last twenty years.
> 
> I use an alternative strategy: if I want to implement
>         static double XYZ = nonconstant-expression;
> 
> I instead do
>         #define XYZ (*XYZ_())
>         static double *XYZ_(void) {
>             static double xyz;
>             static bool initialised = false;
>             LOCK
>                 if (!initialised) {
>                     xyz = nonconstant-expression;
>                     initialised = true;
>                 }
>             UNLOCK
>             return &xyz;
>         }
> 
> XYZ will be an lvalue just like any other variable; it gets initialised on the 
> first use; the initialisation can be any complicated as desired, using any 
> dependencies desired, without depending on the compiler and loader to sneak in 
> the initialisation code in the right order.

Hey, is that how you ancients did things in the 80's era?  I hope
there's a better way.



Fred the Freshwater Catfish

[toc] | [prev] | [next] | [standalone]


#3875

FromJoe Pfeiffer <pfeiffer@cs.nmsu.edu>
Date2011-05-12 18:01 -0600
Message-ID<1boc37h3il.fsf@snowball.wb.pfeifferfamily.net>
In reply to#3855
Fred the Freshwater Catfish <usenet.fred@bdtd.invalid> writes:
>
> I've been writing some code that I would prefer to be maximally
> portable.  As I have mostly used Linux, gcc is pretty much the only C
> compiler I've ever used under UN*X.  I'd like to use non-constant
> initializers for some global variables.  I'm pretty sure I can get it
> to work the way I want under gcc, but what of other compilers?  The
> gcc documentation in info format says that ISO C99 allows this, and so
> I expect some compilers built in the 90's will support the feature,
> but to what extent is this true?

Where does the documentation say this is allowed by C99?  The C99
standard says

  All the expressions in an initializer for an object that has static
  storage duration shall be constant expressions or string literals.

(Section 6.7.8 paragraph 4)
-- 
"It used to be that the USA was pretty good at producing stuff teenaged 
boys could lose a finger or two playing with."	-- James Nicoll

[toc] | [prev] | [next] | [standalone]


#3881

FromKeith Thompson <kst-u@mib.org>
Date2011-05-12 18:15 -0700
Message-ID<lnei43ienr.fsf@nuthaus.mib.org>
In reply to#3875
Joe Pfeiffer <pfeiffer@cs.nmsu.edu> writes:
> Fred the Freshwater Catfish <usenet.fred@bdtd.invalid> writes:
>> I've been writing some code that I would prefer to be maximally
>> portable.  As I have mostly used Linux, gcc is pretty much the only C
>> compiler I've ever used under UN*X.  I'd like to use non-constant
>> initializers for some global variables.  I'm pretty sure I can get it
>> to work the way I want under gcc, but what of other compilers?  The
>> gcc documentation in info format says that ISO C99 allows this, and so
>> I expect some compilers built in the 90's will support the feature,
>> but to what extent is this true?
>
> Where does the documentation say this is allowed by C99?  The C99
> standard says
>
>   All the expressions in an initializer for an object that has static
>   storage duration shall be constant expressions or string literals.
>
> (Section 6.7.8 paragraph 4)

I think this is the relevant section of the gcc document:

    6.23 Non-Constant Initializers
    ==============================
    
    As in standard C++ and ISO C99, the elements of an aggregate
    initializer for an automatic variable are not required to
    be constant expressions in GNU C.  Here is an example of an
    initializer with run-time varying elements:
    
         foo (float f, float g)
         { 
           float beat_freqs[2] = { f-g, f+g };
           /* ... */
         }
    
C90 requires constant expression for all initializers.  C99 permits
non-constant expressions, but only for automatic variables.

-- 
Keith Thompson (The_Other_Keith) kst-u@mib.org  <http://www.ghoti.net/~kst>
Nokia
"We must do something.  This is something.  Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"

[toc] | [prev] | [next] | [standalone]


#3887

FromJoe Pfeiffer <pfeiffer@cs.nmsu.edu>
Date2011-05-12 22:56 -0600
Message-ID<1b1v03jj0h.fsf@snowball.wb.pfeifferfamily.net>
In reply to#3881
Keith Thompson <kst-u@mib.org> writes:

> Joe Pfeiffer <pfeiffer@cs.nmsu.edu> writes:
>> Fred the Freshwater Catfish <usenet.fred@bdtd.invalid> writes:
>>> I've been writing some code that I would prefer to be maximally
>>> portable.  As I have mostly used Linux, gcc is pretty much the only C
>>> compiler I've ever used under UN*X.  I'd like to use non-constant
>>> initializers for some global variables.  I'm pretty sure I can get it
>>> to work the way I want under gcc, but what of other compilers?  The
>>> gcc documentation in info format says that ISO C99 allows this, and so
>>> I expect some compilers built in the 90's will support the feature,
>>> but to what extent is this true?
>>
>> Where does the documentation say this is allowed by C99?  The C99
>> standard says
>>
>>   All the expressions in an initializer for an object that has static
>>   storage duration shall be constant expressions or string literals.
>>
>> (Section 6.7.8 paragraph 4)
>
> I think this is the relevant section of the gcc document:
>
>     6.23 Non-Constant Initializers
>     ==============================
>     
>     As in standard C++ and ISO C99, the elements of an aggregate
>     initializer for an automatic variable are not required to
>     be constant expressions in GNU C.  Here is an example of an
>     initializer with run-time varying elements:
>     
>          foo (float f, float g)
>          { 
>            float beat_freqs[2] = { f-g, f+g };
>            /* ... */
>          }
>     
> C90 requires constant expression for all initializers.  C99 permits
> non-constant expressions, but only for automatic variables.

The OP said he wanted non-constant initializers for global variables,
not for automatic variables.
-- 
"It used to be that the USA was pretty good at producing stuff teenaged 
boys could lose a finger or two playing with."	-- James Nicoll

[toc] | [prev] | [next] | [standalone]


#3888

FromSeebs <usenet-nospam@seebs.net>
Date2011-05-13 05:18 +0000
Message-ID<slrnispfhv.2h1e.usenet-nospam@guild.seebs.net>
In reply to#3887
(Followups to comp.lang.c only, this has nothing to do with comp.arch.)

On 2011-05-13, Joe Pfeiffer <pfeiffer@cs.nmsu.edu> wrote:
> Keith Thompson <kst-u@mib.org> writes:
>>     6.23 Non-Constant Initializers
>>     ==============================

> The OP said he wanted non-constant initializers for global variables,
> not for automatic variables.

Yes.  But the question was where in the gcc docs there was anything about
C99 allowing non-constant initializers.  The answer is, there.  Only, as
you note, it doesn't say it about globals.

-s
-- 
Copyright 2011, all wrongs reversed.  Peter Seebach / usenet-nospam@seebs.net
http://www.seebs.net/log/ <-- lawsuits, religion, and funny pictures
http://en.wikipedia.org/wiki/Fair_Game_(Scientology) <-- get educated!
I am not speaking for my employer, although they do rent some of my opinions.

[toc] | [prev] | [next] | [standalone]


#3904

FromJoe Pfeiffer <pfeiffer@cs.nmsu.edu>
Date2011-05-13 09:10 -0600
Message-ID<1bmxiqfxfg.fsf@snowball.wb.pfeifferfamily.net>
In reply to#3888
Seebs <usenet-nospam@seebs.net> writes:

> (Followups to comp.lang.c only, this has nothing to do with comp.arch.)
>
> On 2011-05-13, Joe Pfeiffer <pfeiffer@cs.nmsu.edu> wrote:
>> Keith Thompson <kst-u@mib.org> writes:
>>>     6.23 Non-Constant Initializers
>>>     ==============================
>
>> The OP said he wanted non-constant initializers for global variables,
>> not for automatic variables.
>
> Yes.  But the question was where in the gcc docs there was anything about
> C99 allowing non-constant initializers.  The answer is, there.  Only, as
> you note, it doesn't say it about globals.

As the guy who asked the question ("where in the GCC docs?"), I did
indeed mean non-constant initializers for globals when I said "this".
To restore the relevant context you deleted:

> Joe Pfeiffer <pfeiffer@cs.nmsu.edu> writes:
>> Fred the Freshwater Catfish <usenet.fred@bdtd.invalid> writes:
>>> I've been writing some code that I would prefer to be maximally
>>> portable.  As I have mostly used Linux, gcc is pretty much the only C
>>> compiler I've ever used under UN*X.  I'd like to use non-constant
>>> initializers for some global variables.  I'm pretty sure I can get it
>>> to work the way I want under gcc, but what of other compilers?  The
>>> gcc documentation in info format says that ISO C99 allows this, and so
>>> I expect some compilers built in the 90's will support the feature,
>>> but to what extent is this true?
>>
>> Where does the documentation say this is allowed by C99?  The C99
>> standard says

-- 
"It used to be that the USA was pretty good at producing stuff teenaged 
boys could lose a finger or two playing with."	-- James Nicoll

[toc] | [prev] | [next] | [standalone]


#3917

FromSeebs <usenet-nospam@seebs.net>
Date2011-05-13 20:56 +0000
Message-ID<slrnisr47r.c2e.usenet-nospam@guild.seebs.net>
In reply to#3904
On 2011-05-13, Joe Pfeiffer <pfeiffer@cs.nmsu.edu> wrote:
> As the guy who asked the question ("where in the GCC docs?"), I did
> indeed mean non-constant initializers for globals when I said "this".

Yeah.  I think that the rationale of the response was roughly:
	* Obviously, it doesn't
	* But there is a section of the docs which refers to C99 and
	  to non-constant initializers
	* Presenting that could explain where the confusion came from
	  that led someone to think that gcc's docs said that C99
	  allowed this

-s
-- 
Copyright 2011, all wrongs reversed.  Peter Seebach / usenet-nospam@seebs.net
http://www.seebs.net/log/ <-- lawsuits, religion, and funny pictures
http://en.wikipedia.org/wiki/Fair_Game_(Scientology) <-- get educated!
I am not speaking for my employer, although they do rent some of my opinions.

[toc] | [prev] | [next] | [standalone]


#4295

FromTim Rentsch <txr@alumni.caltech.edu>
Date2011-05-18 23:47 -0700
Message-ID<kfnfwobw5is.fsf@x-alumni2.alumni.caltech.edu>
In reply to#3881
Keith Thompson <kst-u@mib.org> writes:

> Joe Pfeiffer <pfeiffer@cs.nmsu.edu> writes:
>> Fred the Freshwater Catfish <usenet.fred@bdtd.invalid> writes:
>>> I've been writing some code that I would prefer to be maximally
>>> portable.  As I have mostly used Linux, gcc is pretty much the only C
>>> compiler I've ever used under UN*X.  I'd like to use non-constant
>>> initializers for some global variables.  I'm pretty sure I can get it
>>> to work the way I want under gcc, but what of other compilers?  The
>>> gcc documentation in info format says that ISO C99 allows this, and so
>>> I expect some compilers built in the 90's will support the feature,
>>> but to what extent is this true?
>>
>> Where does the documentation say this is allowed by C99?  The C99
>> standard says
>>
>>   All the expressions in an initializer for an object that has static
>>   storage duration shall be constant expressions or string literals.
>>
>> (Section 6.7.8 paragraph 4)
>
> I think this is the relevant section of the gcc document:
>
>     6.23 Non-Constant Initializers
>     ==============================
>     
>     As in standard C++ and ISO C99, the elements of an aggregate
>     initializer for an automatic variable are not required to
>     be constant expressions in GNU C.  Here is an example of an
>     initializer with run-time varying elements:
>     
>          foo (float f, float g)
>          { 
>            float beat_freqs[2] = { f-g, f+g };
>            /* ... */
>          }
>     
> C90 requires constant expression for all initializers.  [snip]

What I think you mean is C90 requires constant expressions for
all initializers used for aggregates and unions.  C90 allows
initializers for automatic variables of scalar type to be
expressions that aren't constant expressions.

[toc] | [prev] | [next] | [standalone]


#4331

FromKeith Thompson <kst-u@mib.org>
Date2011-05-19 08:15 -0700
Message-ID<lnoc2y3eoa.fsf@nuthaus.mib.org>
In reply to#4295
Tim Rentsch <txr@alumni.caltech.edu> writes:
> Keith Thompson <kst-u@mib.org> writes:
[...]
>> C90 requires constant expression for all initializers.  [snip]
>
> What I think you mean is C90 requires constant expressions for
> all initializers used for aggregates and unions.  C90 allows
> initializers for automatic variables of scalar type to be
> expressions that aren't constant expressions.

Actually I *meant* what I wrote; I was just wrong (I forgot about
non-constant initializers for automatic scalars).  Thanks for the
correction.

-- 
Keith Thompson (The_Other_Keith) kst-u@mib.org  <http://www.ghoti.net/~kst>
Nokia
"We must do something.  This is something.  Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"

[toc] | [prev] | [next] | [standalone]


#3922

FromFred the Freshwater Catfish <usenet.fred@bdtd.invalid>
Date2011-05-13 23:57 +0000
Message-ID<YXyngC.YOA.LjnYe@btdt.invalid>
In reply to#3875
On Thu, May 12, 2011 at 06:01:54PM -0600, Joe Pfeiffer wrote:
> Fred the Freshwater Catfish <usenet.fred@bdtd.invalid> writes:
> >
> > I've been writing some code that I would prefer to be maximally
> > portable.  As I have mostly used Linux, gcc is pretty much the only C
> > compiler I've ever used under UN*X.  I'd like to use non-constant
> > initializers for some global variables.  I'm pretty sure I can get it
> > to work the way I want under gcc, but what of other compilers?  The
> > gcc documentation in info format says that ISO C99 allows this, and so
> > I expect some compilers built in the 90's will support the feature,
> > but to what extent is this true?
> 
> Where does the documentation say this is allowed by C99?  The C99
> standard says
> 
>   All the expressions in an initializer for an object that has static
>   storage duration shall be constant expressions or string literals.
> 
> (Section 6.7.8 paragraph 4)

I figured out my problem.  First, the docs:
	
	File: gcc-4.5.info, Node: Initializers, Next: Compound Literals, Prev:
	Pointer Arith,  Up: C Extensions
	
	6.23 Non-Constant Initializers
	==============================
	
	As in standard C++ and ISO C99, the elements of an aggregate
	initializer for an automatic variable are not required to be constant
	expressions in GNU C.  Here is an example of an initializer with
	run-time varying elements:
	
	     foo (float f, float g)
	     {
	       float beat_freqs[2] = { f-g, f+g };
	       /* ... */
	     }
	


 1 #include <stdio.h>
 2 #include <fcntl.h>
 3 #include <stdlib.h>

 5 int * initialize(void)
 6 {
 7 	int *p;
 8	int fd;
 9
 10	fd = open("/dev/urandom", O_RDONLY);
 11	if(-1 == fd) {
 12		perror("open()");
 13		exit(1);
 14	}
 15
 16	read(fd, p, sizeof(int *));
 17	
 18	return(p);
 19 }
 20
 21 main(int argc, char ** argv)
 22 {
 23 	int * glob = initialize();
 24
 25	fprintf(stdout, "glob = %d\n", glob);
 26
 27	exit(0);
 28 }

So I thought I could move the "glob = initialize()" call out of main()
and have a convenient way to set up global variables.  This is the
problem of designing vaporware when there are a lot of details to
manage.  I'll just have to arrange to load/save my globals with a
little bit more work.  Anyhow, my initial question was related to
efforts to avoid using compilerisms which might lead to portability
issues.  As my project approaches alpha-release material, I'm trying
to plan ahead enough to avoid massive rewrites in stuff that could
limit portability.

Sorry for my misinterpretation of gcc capabilities.



Fred the Freshwater Catfish

[toc] | [prev] | [standalone]


Page 5 of 5 — ← Prev page 1 2 3 4 [5]

Back to top | Article view | comp.lang.c


csiph-web