Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!sdd.hp.com!usc!pollux.usc.edu!kjh From: kjh@pollux.usc.edu (Kenneth J. Hendrickson) Newsgroups: comp.os.minix Subject: Re: Bug in Minix C compiler Message-ID: <25643@usc.edu> Date: 2 Jul 90 09:36:58 GMT References: <19169@orstcs.CS.ORST.EDU> Sender: news@usc.edu Organization: EE-Systems, USC, Los Angeles Lines: 58 New material follows this reply. Keep on reading. In article <19169@orstcs.CS.ORST.EDU> regan@jacobs.CS.ORST.EDU (Dave Regan) writes: >I found a possible bug in the C compiler. >Consider the following test file: > char array1[1000] = "test"; > char array2[1000]; >Both of these arrays should be 2000 bytes long with "array1" having >the first 5 bytes initialized. Both should be 500 bytes long. Also, if they are declared static outside a function, they they should go in the .bss segment, and they should be initialized to 0's unless explicitly initialized to another value. >You get the following: > .globl _array1 > .data > _array1: | 3 words of storage ^^^^^^^^^^^^^^^^^^^^ You are right here. This is a SERIOUS BUG! There should be 500 words of storage allocated, instead of only 3. > .word 25972 > .word 29811 > .globl _array2 > .word 0 > .bss > _array2: .zerow 1000/2 | 500 words of storage > .text >I am currently avoiding the problem by initializing arrays at runtime. Smart. Very smart. >I am sorry if this has been brought up before; I hadn't noticed before. Thanks for bringing it up. We all needed to see this. BEWARE YE, BEWARE YE, BEWARE YE: If you declare static variables outside of a function, and initialize them, they go in the .bss segment in the order in which they are declared. If you declare static variables outside of a function, and do not initialize them, then they go in the .bss segment in the opposite order from which they are declared. (This is the way the Minix C compiler does it.) The wise guy that wrote crypt() for BSD wrote code that was system dependant, and required that variables would go into the bss segment in the order in which they were declared. While he probably thought he was very clever, his code jumped up and bit me in the *ss. Now maybe the wise guy was wiser than I, and K&R insist that variables go into their respective segments in the order in which they are declared. However, I am not aware of this. If I was defining the C language, I would not have done it this way. Of course, if K&R did do it this way, that means the Minix C compiler is broken. Ken Hendrickson N8DGN/6 kjh@usc.edu ...!uunet!usc!pollux!kjh