delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp-workers/2001/02/15/12:58:40

From: "Mark E." <snowball3 AT bigfoot DOT com>
To: "Eli Zaretskii" <eliz AT is DOT elta DOT co DOT il>, djgpp-workers AT delorie DOT com
Date: Thu, 15 Feb 2001 12:58:57 -0500
MIME-Version: 1.0
Subject: Re: O_TEMPORARY
Message-ID: <3A8BD291.29450.A975D0@localhost>
In-reply-to: <2950-Thu15Feb2001191733+0200-eliz@is.elta.co.il>
References: <3A8BB342 DOT 6283 DOT 2F2411 AT localhost> (snowball3 AT bigfoot DOT com)
X-mailer: Pegasus Mail for Win32 (v3.12c)
Reply-To: djgpp-workers AT delorie DOT com
Errors-To: nobody AT delorie DOT com
X-Mailing-List: djgpp-workers AT delorie DOT com
X-Unsubscribes-To: listserv AT delorie DOT com

> 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 -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019