[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.c

Non-constant initializers

Fred the Freshwater Catfish

5/12/2011 10:59:00 PM

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.

Thanks if you can shed any light on this subject.



Fred the Freshwater Catfish

94 Answers

Ian Collins

5/12/2011 11:07:00 PM

0

On 05/13/11 10:59 AM, Fred the Freshwater Catfish 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?

How do you derive the initialiser values?

--
Ian Collins

Shao Miller

5/12/2011 11:09:00 PM

0

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?

China Blue Veins

5/12/2011 11:16:00 PM

0

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....

Fred the Freshwater Catfish

5/12/2011 11:17:00 PM

0

On Fri, May 13, 2011 at 11:07:09AM +1200, Ian Collins wrote:
> On 05/13/11 10:59 AM, Fred the Freshwater Catfish 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?
>
> How do you derive the initialiser values?

It should end up being a function call most of the time.



Fred the Freshwater Catfish

Ian Collins

5/12/2011 11:21:00 PM

0

On 05/13/11 11:17 AM, Fred the Freshwater Catfish wrote:
> On Fri, May 13, 2011 at 11:07:09AM +1200, Ian Collins wrote:
>> On 05/13/11 10:59 AM, Fred the Freshwater Catfish 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?
>>
>> How do you derive the initialiser values?
>
> It should end up being a function call most of the time.

Then you either want C++, or a singleton fiddle as posted else-thread.

--
Ian Collins

Uncle Steve

5/12/2011 11:23:00 PM

0

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?



Regards,

Uncle Steve

--
Should a professional politician be charged with molestation if he
kisses babies while attending political rallies?

Fred the Freshwater Catfish

5/12/2011 11:29:00 PM

0

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

China Blue Veins

5/12/2011 11:30:00 PM

0

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....

Fred the Freshwater Catfish

5/12/2011 11:31:00 PM

0

On Fri, May 13, 2011 at 11:20:30AM +1200, Ian Collins wrote:
> On 05/13/11 11:17 AM, Fred the Freshwater Catfish wrote:
> >On Fri, May 13, 2011 at 11:07:09AM +1200, Ian Collins wrote:
> >>On 05/13/11 10:59 AM, Fred the Freshwater Catfish 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?
> >>
> >>How do you derive the initialiser values?
> >
> >It should end up being a function call most of the time.
>
> Then you either want C++, or a singleton fiddle as posted else-thread.

Sorry, I'm not using C++.



Fred the Freshwater Catfish

Ian Collins

5/12/2011 11:34:00 PM

0

On 05/13/11 11:30 AM, Fred the Freshwater Catfish wrote:
> On Fri, May 13, 2011 at 11:20:30AM +1200, Ian Collins wrote:
>> On 05/13/11 11:17 AM, Fred the Freshwater Catfish wrote:
>>> On Fri, May 13, 2011 at 11:07:09AM +1200, Ian Collins wrote:
>>>> On 05/13/11 10:59 AM, Fred the Freshwater Catfish 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?
>>>>
>>>> How do you derive the initialiser values?
>>>
>>> It should end up being a function call most of the time.
>>
>> Then you either want C++, or a singleton fiddle as posted else-thread.
>
> Sorry, I'm not using C++.

But you require a C++ feature...

You could compile all but main() as C and compile the source file
containing main() and your initialisers as C++.

--
Ian Collins