Suggestions for declarations.

Discussion in 'C Programming' started by artifact.one@googlemail.com, Sep 1, 2006.

  1. Guest

    Hi.

    I'm working on a program that has one file of its source
    code generated by a script that reads a colon delimited data
    file and creates some C arrays as output. Now, in this data
    file, some pieces of data refer to other pieces of data and
    therefore, in these generated structures, there are pointers
    to other generated data structures. The script is quite
    brain dead. It doesn't try to order the data structures so
    that the ones at the bottom of the chain of dependencies are
    declared first (this might not even be possible, given the
    data).

    I thought a solution to this might be to (gasp!) put
    declarations such as these at the top of the file:

    extern const struct mkf_type *pretypes_execldep[];
    extern const struct mkf_type *pretypes_cpjlib[];

    And then the script will declare the actual structure later
    in the same file:

    const struct mkf_type *pretypes_cpjlib[] = {
    &type_dynamicldep,
    &type_dynamicldepx,
    };

    This way, I avoid any 'variable is undeclared at this point'
    errors.

    The trouble is, this gives the data structures external
    linkage, which I really don't want. Making the array
    declaration 'static' would, as far as I know, cause
    undefined behaviour because of the previous 'extern'
    declaration but I can't see that I can get rid of the
    'extern' declaration given the ordering problem (some
    structures refer to other structures that will not have been
    declared yet if the 'extern' declaration is removed).

    I realise this may sound like total gibberish, so be gentle
    please. I've not completely grasped the intricacies
    (horrors?) of the humble C compiler yet.

    Any help would be appreciated.
    MC
    , Sep 1, 2006
    #1
    1. Advertising

  2. wrote:

    > Hi.
    >
    > I'm working on a program that has one file of its source
    > code generated by a script that reads a colon delimited data
    > file and creates some C arrays as output. Now, in this data
    > file, some pieces of data refer to other pieces of data and
    > therefore, in these generated structures, there are pointers
    > to other generated data structures. The script is quite
    > brain dead. It doesn't try to order the data structures so
    > that the ones at the bottom of the chain of dependencies are
    > declared first (this might not even be possible, given the
    > data).
    >
    > I thought a solution to this might be to (gasp!) put
    > declarations such as these at the top of the file:
    >
    > extern const struct mkf_type *pretypes_execldep[];
    > extern const struct mkf_type *pretypes_cpjlib[];
    >
    > And then the script will declare the actual structure later
    > in the same file:
    >
    > const struct mkf_type *pretypes_cpjlib[] = {
    > &type_dynamicldep,
    > &type_dynamicldepx,
    > };
    >
    > This way, I avoid any 'variable is undeclared at this point'
    > errors.
    >
    > The trouble is, this gives the data structures external
    > linkage, which I really don't want. Making the array
    > declaration 'static' would, as far as I know, cause
    > undefined behaviour because of the previous 'extern'
    > declaration but I can't see that I can get rid of the
    > 'extern' declaration given the ordering problem (some
    > structures refer to other structures that will not have been
    > declared yet if the 'extern' declaration is removed).


    So you want the definition to be static but you're worried
    that if you declared them extern earlier it will cause problems ?
    Just declare them without using extern (or static).
    Spiros Bousbouras, Sep 1, 2006
    #2
    1. Advertising

  3. Guest

    Spiros Bousbouras wrote:
    >
    > So you want the definition to be static but you're worried
    > that if you declared them extern earlier it will cause problems ?
    > Just declare them without using extern (or static).


    Hello.

    I tried this, but if I remove the early extern declaration and
    make the second one static, the compiler rightfully complains:

    type.c:28: error: static declaration of 'pretypes_cpjlib' follows
    non-static declaration
    type.c:21: error: previous declaration of 'pretypes_cpjlib' was here

    If I remove the static keyword, using the nm tool, the symbols
    appear to have external linkage again:

    $ nm type.o
    00000050 D pretypes_cpjlib

    (uppercase 'D' is 'external linkage, in data segment')

    MC
    , Sep 1, 2006
    #3
  4. jacob navia Guest

    wrote:
    > Spiros Bousbouras wrote:
    >
    >>So you want the definition to be static but you're worried
    >>that if you declared them extern earlier it will cause problems ?
    >>Just declare them without using extern (or static).

    >
    >
    > Hello.
    >
    > I tried this, but if I remove the early extern declaration and
    > make the second one static, the compiler rightfully complains:
    >
    > type.c:28: error: static declaration of 'pretypes_cpjlib' follows
    > non-static declaration
    > type.c:21: error: previous declaration of 'pretypes_cpjlib' was here
    >
    > If I remove the static keyword, using the nm tool, the symbols
    > appear to have external linkage again:
    >
    > $ nm type.o
    > 00000050 D pretypes_cpjlib
    >
    > (uppercase 'D' is 'external linkage, in data segment')
    >
    > MC
    >

    You have to mark BOTH as static obviously
    jacob navia, Sep 1, 2006
    #4
  5. Guest

    jacob navia wrote:
    >
    > You have to mark BOTH as static obviously


    Hi.

    If I mark both as static, the compiler says:

    type.c:36: error: array size missing in 'pretypes_cpjlib'

    It no longer considers the first declaration as a sort of
    "prototype" declaration (for want of a better term).

    MC
    , Sep 1, 2006
    #5
  6. jacob navia Guest

    wrote:
    > jacob navia wrote:
    >
    >>You have to mark BOTH as static obviously

    >
    >
    > Hi.
    >
    > If I mark both as static, the compiler says:
    >
    > type.c:36: error: array size missing in 'pretypes_cpjlib'
    >
    > It no longer considers the first declaration as a sort of
    > "prototype" declaration (for want of a better term).
    >
    > MC
    >


    Yes, I forgot to realize that.
    Static arrays need a size specification and in your
    declaration you do not put any size.

    If you know the size you have to put it in there
    (2) from your example

    If you do NOT know the size you have to make two passes and
    save the size.
    jacob navia, Sep 1, 2006
    #6
  7. Guest

    jacob navia wrote:
    >
    > Yes, I forgot to realize that.
    > Static arrays need a size specification and in your
    > declaration you do not put any size.
    >
    > If you know the size you have to put it in there
    > (2) from your example
    >
    > If you do NOT know the size you have to make two passes and
    > save the size.


    Ah, simple as that eh?

    Well, it does seem to work! Thanks!

    MC
    , Sep 1, 2006
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. deep
    Replies:
    0
    Views:
    475
  2. M.D. van de Burgwal
    Replies:
    3
    Views:
    1,065
    Paul Uiterlinden
    Oct 7, 2004
  3. =?ISO-8859-1?Q?Andr=E9s?=

    Pin declarations in EC/ECP FPGAs

    =?ISO-8859-1?Q?Andr=E9s?=, Feb 22, 2005, in forum: VHDL
    Replies:
    11
    Views:
    2,490
    cristian
    Mar 11, 2005
  4. Srinivasan Venkataramanan

    Do you still use component declarations?

    Srinivasan Venkataramanan, Sep 18, 2005, in forum: VHDL
    Replies:
    3
    Views:
    656
    Colin Marquardt
    Sep 19, 2005
  5. Replies:
    0
    Views:
    301
Loading...

Share This Page