Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C Programming (http://www.velocityreviews.com/forums/f42-c-programming.html)
-   -   Suggestions for declarations. (http://www.velocityreviews.com/forums/t444201-suggestions-for-declarations.html)

artifact.one@googlemail.com 09-01-2006 08:41 AM

Suggestions for declarations.
 
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


Spiros Bousbouras 09-01-2006 09:11 AM

Re: Suggestions for declarations.
 
artifact.one@googlemail.com 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).


artifact.one@googlemail.com 09-01-2006 09:49 AM

Re: Suggestions for declarations.
 
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


jacob navia 09-01-2006 10:49 AM

Re: Suggestions for declarations.
 
artifact.one@googlemail.com 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

artifact.one@googlemail.com 09-01-2006 10:57 AM

Re: Suggestions for declarations.
 
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


jacob navia 09-01-2006 11:02 AM

Re: Suggestions for declarations.
 
artifact.one@googlemail.com 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.


artifact.one@googlemail.com 09-01-2006 12:29 PM

Re: Suggestions for declarations.
 
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



All times are GMT. The time now is 09:45 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.