18

Some versions of documentation declare certain functions like parameters declared in the signature and, e.g., Arch Linux:

ssize_t read(size_t count;
             int fd, void buf[count], size_t count);

GCC just ignores the forward declaration, but it stumbles over an obviously illegal array of void.

Is that some universally accepted mnemonic (another version I saw was buf[.count]) and GCC ignores it or is it an extension?

9
  • 7
    @dbush: This is not a duplicate of that question because this is not a question about forward declarations. It is a question about why the documentation shows a parameter declared as void buf[count] when that is not a proper declaration because it violates the C standard’s constraint that an array element type must be a complete type. (Even though the declaration would be adjusted to void *, the constraint applies first.) Commented Aug 7 at 22:51
  • 1
    I was just wondering the same thing, just today, about mmap manual page on Ubuntu. Likewise, and "array of void" as first argument (void addr[.length]). That seems quite generalized: memcpy, memcmp (no forward this times. But still "array of void"), qsort, ... Maybe some new habit in the man-project? Commented Aug 7 at 23:42
  • 1
    The suggested duplicate question was What is a parameter forward declaration? One of the answers to that question links to the GCC documentation: Arrays of Variable Length. That explains the size_t count; part of the function syntax — it is a GNU (GCC) extension. Commented Aug 8 at 5:50
  • 1
    Well, somehow it is relieving to know that you saw it before. Because of the coincidence of you asking that question (that I saw randomly in my feed, not at all because I was looking for anything) just a few hours after I noticed it for the first time. Because of that coincidence, a part of me was wondering "did they change everybody's manpage overnight yesterday?" (and I consult man pages the old way, by typing man mmap in a shell, not looking a website. So, it's a local file...) Commented Aug 8 at 9:16
  • 4
    Relevant commit: man-pages c64cd13e: "Use VLA syntax also for void *, even if it's a bit more weird.". Commented Aug 8 at 18:05

2 Answers 2

20

The forward declaration of a parameter is a GCC extension, so GCC accepts it.

The declaration of an array of void is an error or deliberate misuse of C in the documentation. A declaration of a parameter as an array is automatically adjusted to be a pointer, so the documentation author likely thought void buf[count] would express a declaration of buf to have type void *, along with a suggestion that it points to the first of at least count elements (of some type known to the caller). However, the C standard has a constraint (in C 2024 6.7.7.3) that the element type of an array in a declaration must be complete (its size must be known to the compiler at that moment), and void is not a compete type. So a compiler will detect and report this constraint violation before the adjustment would be made.

A correct declaration for Unix systems, from The Open Group Base Specifications, would be:

ssize_t read(int fildes, void *buf, size_t nbyte);
Sign up to request clarification or add additional context in comments.

Comments

10

The forward declaration syntax is a GCC extension indeed. It is described e.g. here.

Neither the dot syntax nor the void array seem to be GCC extensions, but they are used in Linux man pages a lot, so I'd guess it's a "common mnemonic".

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.