On Mon, 21 Apr 2008 00:35:50 -0700 (PDT), vshenoy
<vi*************@gmail.comwrote:
>Hi Guys,
I was going through gdbm-1.8.3 source (http://ftp.gnu.org/gnu/gdbm/
gdbm-1.8.3.tar.gz) and found this strange thing : all the exposed
functions of gdbm work with GDBM_FILE pointer (which is returned by
gdbm_open), but in the implementation of gdbm functions (e.g.
gdbm_open in gdbmopen.c)work with a structure called gdbm_file_info.
Now GDBM_FILE is defined like this in gdbm.h an exposed header :
typedef struct { int dummy[10];} *GDBM_FILE;
but gdbm_file_info is a structure which is a lot bigger than this.
Nowhere in the discussion below is there any indication of what a
gdbm_file_info looks like. Where do you get the idea it is bigger
than 10 int?
>
For e.g. in gdbm.h gdbm_open is defined as :
extern int gdbm_store __P((GDBM_FILE, datum, datum, int)); /* __P(x)
is x if it is standard C or C++ else _P(x) is () */
Why show us gdbm_store when your question is about gdbm_open?
This is not a definition. It is a declaration (also known as a
prototype). It serves two purposes: 1) to let the compiler check on
(and possibly convert) the arguments you pass, and 2) to let the
compiler generate code to use the return value.
>
but in its definition it is like this :
int
gdbm_store (dbf, key, content, flags)
gdbm_file_info *dbf;
datum key;
datum content;
int flags;
Technically, this definition is inconsistent with the prototype. If
gdbm.h is not included in this source module, the compiler will not
know about the inconsistency. The linker never cares about arguments.
At the code level, there is no problem if gdbm_file_info is also a
structure because all pointers to structure have the same size and
representation.
This is a very old style function definition (pre-C89). While it is
still legal, it is a strong hint that you are looking at code that
predates the standard.
>
I have these questions : (I searched previous archives of c.l.c, but
only found mention of typedef struct {int dummy[10];} *GDBM_FILE; in a
post called "What makes C _not_ a subset of C++, where it said that
this particular declaration is illegal in C++)
It's off-topic but I wonder why unless one of the tokens is a reserved
word.
>
1. How does this sort of code compiles in standard C ? ( What function
prototype matching rule of C applies here ?) When I checked in the
generated Makefile there wasn't any special flags that were passed to
gcc.
Is gdbm.h included in the source module that contains gdbm_store? If
not, there is no prototype to match.
>
2. Why was this done ? My first guess is because the author did not
want to expose the gdbm_file_info structure in gdbm.h so that he can
change it between different releases preserving compatibility (?) Also
to keep the external interface clean.
Not uncommon but remember how old this is.
>
3. Is this safe ? Will it work in all the compilers and machines i.e.
is it portable ?
You would not normally be compiling gdbm_store. It would be in a
library that would be used by the linker to resolve your references to
it. As explained above, while their is an inconsistency it is not the
type to manifest as an error in your generated code.
I would expect it to work only on those systems for which gdbm-1.8.3
is designed. Does gnu work on Windows also or is it limited to Unix?
Does gdbm_open eventually call fopen or does it use system specific
tricks to open the file? If the latter, I would be surprised if it
worked at all on my IBM mainframe.
Remove del for email