This is the mail archive of the gcc-help@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: Sanity check - making a static initializer constant


On 9 October 2013 20:10, Ian Pilcher wrote:
>   int match_foo(const char *s)
>   {
>         static const struct regex *const regex = &regexes[0];
>         // static regmatch_t *const matches = regex->matches;
>         // static regmatch_t *const matches = regexes[0].matches;
>         static regmatch_t *const matches = foo_matches;
>
>         return regexec(&regex->regex, s, regex->nmatch, matches, 0);
>   }
>
> Note the two commented-out initializers for "matches".  I would much
> prefer to write the initializer in terms of the "regex" pointer or the
> array.  This would make the connection much clearer and make any future
> changes to the code much less error-prone.  Unfortunately, neither of
> the commented-out initializers works; gcc (rightly) complains that the
> initializer elements is not constant.
>
> I cannot make the array itself constant, because of the need to compile
> and free the "regex" members.
>
> As far as I know, my options are:
>
> 1.  Live with the current scheme.
>
> 2.  Change "matches" in the function to an automatic variable.
>
> 3.  Change the "regex" member of "struct regex" to a pointer and
>     create separate foo_regex, bar_regex, etc. objects.  I could then
>     make the "regexes" array constant.
>
> Am I missing anything?

4. Use C++ instead of C, which allows those initializers.

Probably not what you were looking for though.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]