Handling 'initializer element not constant' error

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Flash Gordon

    #16
    Re: Handling 'initializer element not constant' error

    Richard Tobin wrote, On 07/05/08 23:29:
    In article <290288be-6c58-49e1-9bef-ec4e3e5de7ca@l4 2g2000hsc.googl egroups.com>,
    <vippstar@gmail .comwrote:
    >
    >I have not read much on alternative solutions to errno in C, so is
    >there a better solution?
    >Returning the error code is not possible (like pthreads) and an extra
    >parameter to every function that can fail would be quite annoying.
    >
    The C standard recognises the problems with errno and doesn't require
    it to be an identifier. It just has to be a macro that produces a
    modifiable lvalue. So a threaded implementation can do something
    like
    >
    #define errno (*__thread_errn o())
    >
    where __thread_errno( ) returns a pointer to a per-thread variable.
    One real example of this is current versions of glibc where after a bit
    of digging I get
    # define errno (* __errno_locatio n ())
    --
    Flash Gordon

    Comment

    • Szabolcs Borsanyi

      #17
      Re: Handling 'initializer element not constant' error

      Assuming the OP can change the code of the library, he could
      initialise yyerfp to NULL and replace all the uses of it with
      >
      (yyerfp ? yyerfp : stdout)
      >
      It's a pity there isn't a standard way to get initialisation code run.
      >
      -- Richard
      I'd mention here a few more poins:
      - <OFF>The OP want's to initialise his shared library
      (although he could avoid it with a different design). [he can
      be she]
      This is usally possible on a decent operating system by
      defining
      a function with a magic name.
      </OFF>
      - Global objects are not quite unusual animals (especially if they are
      constant)
      and one can design them using e.g. a variant of the singleton
      pattern.
      In C that would be to define a pointer-to-function, and one can
      indeed initialise that
      to an other function. In this case:

      FILE *yyerrf_default () { return stdout; }
      static FILE *yyerrf_ptr;
      FILE *yyerrf_user() { return yyerrf_ptr; }
      FILE *(*yyerrf)()=yy errf_default;
      void yyerrf_set(FILE *F) { yyerrf_ptr=F; yyerrf=yyerrf_u ser; }
      #define yyerrf yyerrf() // protected // don't yell // is legal in
      c99
      #define yyerrf_ptr // protected

      (This is a slightly overcomplicated singleton, but of some
      unjustified reasons I avoid conditionals.)

      Szabolcs

      Comment

      Working...