[CWB] Segmentation fault
Stefan Evert
stefanML at collocations.de
Sat May 9 16:55:20 CEST 2020
> On 5 May 2020, at 11:34, Hardie, Andrew <a.hardie at lancaster.ac.uk> wrote:
>
> I had my fingers crossed the problem would only be with the specific variable in the previous error you reported, but actually I was afraid of this.
>
> The underlying issue is that there are (at least) two ways to define global variables in C:
>
> • In a source file, with “extern” declarations in other source/header files
> • In a header file, which creates a copy of the definition in each source file the header is included in; the multiple copies of identical variables in global scope are later merged by the linker.
>
> The former is the newer and recommended way, and the latter is older and AFAIK Unix-only.
Yes, I'm afraid this seems to be correct – but it was surprisingly hard to find a clear & unambiguous explanation.
Modern C textbooks just tell you that you must declare variables in header files as "extern" because there can be only one definition. The fragments of the C99 standard I was able to find, often reprinted or slightly paraphrases in textbooks, carefully avoids any precise statements on "common" variables: it explains internal ("static") and external ("extern") linkage as well as tentative definitions, but does not explicitly say what a linker is supposed to do with multiple definitions in different translation units.
It was only in Kerninghan & Ritchie (2nd edition, documenting ANSI C) that I found a straightforward explanation: tentative definitions become definitions with external linkage within each translation unit, and multiple copies of the same symbol with external linkage in different translations units of the same program are now allowed.
The traditional "Unix" (or probably K&R C) style was to scope tentative definitions across all translation units in the same program, so they would automatically refer to a single instance. Which is the behaviour that CWB has been relying on.
In my defense: I learned C in 1991 from K&R (1st edition) …
@Maarten: thanks for reporting and @Andrew: thanks for fixing this serious bug in the CWB source code!
Best,
Stefan
More information about the CWB
mailing list