[LCP]array of struct with 1 element
Erik Mouw
J.A.K.Mouw at ITS.TUDelft.NL
Mon Sep 3 08:50:40 UTC 2001
On Sun, Sep 02, 2001 at 02:15:13PM -0700, Manu Anand wrote:
> Been studying sources og glibc lately. Two questions:-
>
> 1. How do I implement closeall() which closes all open
> file descriptors in ANSI C?? The problem can be broken
> down to how do I know which all files are open?
Use select() with a large value for the highest numbered descriptor and
a short timeout to find out which file descriptors are ready, or
fstat() a large number of descriptors to find out, or just readdir()
/proc/self/fd/ (only works on linux).
> 2. Many time in kernel I find them using array of
> struct with 1 member.Example from fs.h
> struct files_struct files[1];
Hmm, apparently you're looking at some ancient kernel sources. There is
no such thing in linux-2.2.19 or linux-2.4.9-ac5.
> Is it different from
> struct files_struct files;
Yes, this is a scalar, while the other one is an array. The trick with
a 1 sized array is sometimes used to create an array with a variable
size (i.e.: an array that will be indexed out of bounds on purpose). It
has to be this way so the compiler will know that it is indeed an array
so it will reserve room for (at least) the first value. For an example,
have a look at the struct tag_cmdline in include/asm-arm/setup.h in
recent 2.4 kernels:
struct tag_cmdline {
char cmdline[1]; /* this is the minimum size */
};
This is a structure that holds the kernel command line on arm-linux
platforms. A command line is usually longer, but because the cmdline is
the last member of the structure we won't overwrite other information
(struct tag_cmdline is part of struct tag).
Erik
--
J.A.K. (Erik) Mouw, Information and Communication Theory Group, Department
of Electrical Engineering, Faculty of Information Technology and Systems,
Delft University of Technology, PO BOX 5031, 2600 GA Delft, The Netherlands
Phone: +31-15-2783635 Fax: +31-15-2781843 Email: J.A.K.Mouw at its.tudelft.nl
WWW: http://www-ict.its.tudelft.nl/~erik/
More information about the linuxCprogramming
mailing list