Big tables in .h file!

Z

Zhekka

Hello, gurus.

http://cvs.gnome.org/viewcvs/libunicode/msft/cp932.h?rev=1.2&view=markup"

I've stumbled upon an interesting thing. If you follow this link, you
will see huge translation tables (from ascii to unicode) located in H
file. Now. It is well known (at least to me), that such tables are
better to be stored in C file, right? So that in case of double
inclusion, bad things won't happen...

I really trust those gnome guys, I'm sure they're really smart, and
there was some thought behind this.

Please, give me a hint!!

Thanks
 
B

Ben Pfaff

Emmanuel Delahaye said:
Zhekka a icrit :

Absolute nonsense. The guy who has written the code generator desserves
honey and fire ants...

I imagine that this .h file is included in only one .c file.
That is, it's not really a header, it's just a generated table
for including in some source file. I usually use a .inc
extension for such code to avoid confusing people.
 
C

cane

Zhekka said:
Hello, gurus.

http://cvs.gnome.org/viewcvs/libunicode/msft/cp932.h?rev=1.2&view=markup"

I've stumbled upon an interesting thing. If you follow this link, you
will see huge translation tables (from ascii to unicode) located in H
file. Now. It is well known (at least to me), that such tables are
better to be stored in C file, right? So that in case of double
inclusion, bad things won't happen...

I really trust those gnome guys, I'm sure they're really smart, and
there was some thought behind this.

Please, give me a hint!!

see the "inclusion guards"?

#ifndef MSFT_CP932_H
#define MSFT_CP932_H
....
#endif /* MSFT_CP932_H */

these prevent double inclusion :)

bye.
 
T

Toni Uusitalo

cane said:
see the "inclusion guards"?

#ifndef MSFT_CP932_H
#define MSFT_CP932_H
...
#endif /* MSFT_CP932_H */

these prevent double inclusion :)

Actually, it doesn't seem to prevent it. I once came to this conclusion
doing similar thing; including static tables header from multiple C
files - Then I decided to include file only from one C file (as Ben
Pfaff already suggested).

If somebody could enlighten me - why include guard doesn't work in this
case?

test:
====

main.c:
#include <stdlib.h>
#include "cp932.h"
int main(int argc, char* argv[]) { return 0; }

dummy.c
#include <stdlib.h>
#include "cp932.h"
void dummy(void) {}


gcc -o test.exe main.c dummy.c
gives about 66 KB exe

gcc -o test.exe main.c
(or commenting out cp932.h include in dummy.c)
gives about 41 KB exe

with respect,
Toni Uusitalo
 
R

Richard Tobin

cane said:
see the "inclusion guards"?

#ifndef MSFT_CP932_H
#define MSFT_CP932_H
...
#endif /* MSFT_CP932_H */

these prevent double inclusion :)

They prevent double inclusion *in one .c file*, but if they are
included by multiple .c files that are linked together, it won't
help. Presumably you're not meant to do that.

-- Richard
 
J

John Devereux

They prevent double inclusion *in one .c file*, but if they are
included by multiple .c files that are linked together, it won't
help. Presumably you're not meant to do that.

Also of course because the declaration is static, each module that
#includes the file will likely get its own private copy of the table,
possibly unknown to the programmer.
 
B

Bart C

Also of course because the declaration is static, each module that
#includes the file will likely get its own private copy of the table,
possibly unknown to the programmer.

So what's the syntax to make a declaration 'global'? Then any link loader
will detect such multiple definitions.

Bart C.
 
S

slebetman

Bart said:
So what's the syntax to make a declaration 'global'? Then any link loader
will detect such multiple definitions.

Bart C.

The 'syntax' is to put it in a .c file and declare an extern to it in a
..h file.
 
F

Flash Gordon

Dik said:
Not making it static.

There is no guarantee that the linker will complain if this leads to
multiple definitions, since multiple definitions lead to undefined
behaviour. Indeed, I've had it not complain at link time but had strange
effects at run time when someone else got this wrong.
 
I

Ian Malone

Ben said:
I imagine that this .h file is included in only one .c file.
That is, it's not really a header, it's just a generated table
for including in some source file. I usually use a .inc
extension for such code to avoid confusing people.

What are other people's approaches to this kind of thing? My
approach would be to have a .c containing the translation table
and a .h with the extern declaration. It prevents you using
internal scope though. But my formal C training is somewhat
lacking...
 
K

Kenneth Brody

Ian said:
What are other people's approaches to this kind of thing? My
approach would be to have a .c containing the translation table
and a .h with the extern declaration. It prevents you using
internal scope though. But my formal C training is somewhat
lacking...

I have tables like that, in which the data is in an included ".inc" file,
and the code is in the usual ".c" file. This is especially true when the
data tables are needed by more than one executable, but the code which
uses the table may be different.

Or, perhaps there are several tables, each for the same purpose, but with
different data. For example, collating order and upper/lowercase
conversions for different lauguages/codepages. We have an "english.inc",
a "spanish.inc", a "portuguese.inc", and so on. A single module then
includes the appropriate language tables. Additional languages can be
added by creating new include files, and including them from the main
module. (We also can load from external data files. But that's another
story.)

--
+-------------------------+--------------------+-----------------------------+
| Kenneth J. Brody | www.hvcomputer.com | |
| kenbrody/at\spamcop.net | www.fptech.com | #include <std_disclaimer.h> |
+-------------------------+--------------------+-----------------------------+
Don't e-mail me at: <mailto:[email protected]>
 
A

Alan Balmer

What are other people's approaches to this kind of thing? My
approach would be to have a .c containing the translation table
and a .h with the extern declaration. It prevents you using
internal scope though. But my formal C training is somewhat
lacking...

I'd have to study the problem, but quite likely, I'd have a .c file
containing both the tables and the functions which operate on them.
There would be a corresponding .h file presenting the user interface
(the function prototypes.) Usually, there's no need to make the tables
themselves visible to other modules.
 
B

Ben Pfaff

Ian Malone said:
What are other people's approaches to this kind of thing? My
approach would be to have a .c containing the translation table
and a .h with the extern declaration. It prevents you using
internal scope though.

That's the usual approach. I would use a .inc in the case where
the data table is generated by a program (as it appears to be in
the above link) and I want to include some additional code in the
same translation unit (and the generator program is not amenable
to that).
But my formal C training is somewhat lacking...

Is there such a thing as "formal C training"? I don't have any
either.
 
E

Emmanuel Delahaye

Ben Pfaff a écrit :
I imagine that this .h file is included in only one .c file.
That is, it's not really a header, it's just a generated table
for including in some source file. I usually use a .inc
extension for such code to avoid confusing people.

Agreed.
 
E

Emmanuel Delahaye

cane a écrit :
see the "inclusion guards"?

#ifndef MSFT_CP932_H
#define MSFT_CP932_H
...
#endif /* MSFT_CP932_H */

these prevent double inclusion :)

So what ? Do you reall know the purpose of this guards ? It protects
against multiple inclusion *in the same translation unit*. No effect if
include the same .h in two or more different TU. Got it ?
 
E

Emmanuel Delahaye

John Devereux a écrit :
Also of course because the declaration is static, each module that
#includes the file will likely get its own private copy of the table,
possibly unknown to the programmer.

With a useless double or more use of the memory space, which desserves
double or more rations of honey and fire ants...
 
E

Emmanuel Delahaye

Bart C a écrit :
So what's the syntax to make a declaration 'global'? Then any link loader
will detect such multiple definitions.

/* .h */
#define SIZE ...
extern T a[SIZE];

/* .c */
T a[SIZE] = {...};
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,171
Messages
2,570,936
Members
47,472
Latest member
KarissaBor

Latest Threads

Top