Mail Archives: djgpp-workers/2001/02/15/12:58:40
> If there's some way of knowing what happens in the Win32 API calls
> when the same file is open twice, we should consider emulating the
> same behavior, but only if we think it's The Right Thing. This won't
> be the first time DJGPP ``fixes'' DOS or Windows.
It shouldn't too hard to write a program to test this with one of win32
compilers. I'll look into it.
> > > > + typedef struct
> > > > + {
> > > > + unsigned char ref_count __attribute__((packed));
> > > > + char filename[0] __attribute__((packed));
> > > > + } o_temporary_file_rec;
BTW, I'll take out the 'packed'.
> > Because zero size arrays must be the last member of a struct according to
> > gcc's documentation on zero size arrays.
>
> Do we have to use zero-size arrays? Isn't a pointer enough? Or am I
> missing something (again)?
_o_temporary_rec is a struct with a filename member of variable length. Or as
the gcc docs explain it, o_temporary_file_rec is an object of variable size.
When allocating space for this struct, the space for the filename is also
allocated:
__o_temporary_files[fd] = (o_temporary_file_rec *)malloc(1 /* for ref_count
*/ + (len + 1) /* for filename */ );
If 'char filename[0]' were changed to 'char *filename'. Then the above line
would have to be changed to:
__o_temporary_files[fd] = (o_temporary_file_rec
*)malloc(sizeof(o_temporary_file_rec));
__o_temporary_files[fd]->filename = malloc(len + 1);
The gcc documentation has a very similar example:
-----------
Zero-length arrays are allowed in GNU C. They are very useful as
the last element of a structure which is really a header for a
variable-length object:
struct line {
int length;
char contents[0];
};
{
struct line *thisline = (struct line *)
malloc (sizeof (struct line) + this_length);
thisline->length = this_length;
}
In standard C, you would have to give `contents' a length of 1, which
means either you waste space or complicate the argument to `malloc'.
-----------
The documentation here is a bit old. This construct was added to the standard
in C99.
Mark
- Raw text -