C and 0 sized arrays, any C standard gurus here?

Discussion in 'Mac Programming' started by foidulus, Dec 20, 2010.

  1. foidulus macrumors 6502a

    Joined:
    Jan 15, 2007
    #1
    so really odd question brought about accidentally by a freshman cs student.

    I'm TAing the intro to programming class and the student created an array of size [digit-1], where digit is guaranteed to be at least one due to how the program executes.
    However a 0 sized array doesn't make any sense to me. I created a test program and compiled it, and at least in GCC on the mac, it seems to work. I did a little searching on the interwebs and others are saying it's technically undefined behavior according to the C standard so I'm doubting this code would be portable. However I cannot find anything definitive on this.

    Any C standard gurus care to chime in on this? Am I just crazy, how is this working?

    Here is the quick and dirty sample I wrote:

    Code:
    #include <stdio.h>
    
    int main(void) {
    
    int r[0];
    
    r[0]=78;
    
    printf("%d\n",r[0]);
    
    return 0;
    
    }
    
    
     
  2. cubist macrumors 68020

    Joined:
    Jul 4, 2002
    Location:
    Muncie, Indiana
    #2
    Usually a zero-sized array is used for dereferencing. For example, you make a pointer to an array of size zero, then you can assign the address of any other array to it and use it to look at other elements of the array. Another use is to put a zero-sized array inside a structure so that you can take the address of its location.

    Your code example is very scary. An array of size zero does not have a 0th element, so when you assigned a value to r[ 0 ] you overwrote some other memory. This can cause program crashes.
     
  3. lee1210 macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #3
    I'm not at a computer, but I'd compile with -S and check out the assembly code. Might be worth tossing a few other variables in there and print them to see if they're getting corrupted. If the behavior is undefined, working inexplicably is a possible outcome. You're fine to tell them it's wrong to try to store something in position n of an array of length n, and point out they must have buggy logic if they are doing so. For kicks you may want to print the base of the array with %p, and print the address of the rest of your stack-local variables to see if the base of the array is the position where some other variable lives.

    -Lee
     
  4. Sydde macrumors 68020

    Sydde

    Joined:
    Aug 17, 2009
    #4
    Ran this code through my debugger and looked at the stack
    Code:
    void zeroArrayTest() {
    	int		age[0];
    	char		name[10] = "This Name";
    	age[0] = 0;
    	name[2] = 0x61;
    	age[0] = -1;
    }
    
    When age[0] is set to 0, it replaces "This" in the string. IOW, a zero size array simply puts data where ever it resides but takes up (reserves) no space itself. It might destroy important data on the stack if it is the last variable declared, since it will live at the bottom of the stack frame for the routine (most likely, you would lose a stack base register value, throwing your stack out of whack.
     
  5. gnasher729 macrumors P6

    gnasher729

    Joined:
    Nov 25, 2005
    #5
    1. Google for "N1256.pdf" which will give you the latest free draft of the C Standard. That's in the end where you should get your answer from.

    2. It sounds like your student used a variable length array, your example used a constant size array. Check in 1256.pdf if the rules are the same, and what the rules are.

    3. Why does a zero element array not make sense to you? There are plenty of cases where you could have n items, and the number of items could be varying in a wide range, including the number zero.

    4. If an array "a" has n elements, then it is legal to calculate the address &a for 0 <= i <= n; it is legal to read or write an array element a if 0 <= i < n. The consequence is that if an array has zero elements, it is legal to calculate &a if and only if i = 0, and reading or writing a is never legal. Still, it is completely legal to write "for (i = 0; i < n; ++i) a = 0;".
     
  6. Sander macrumors 6502

    Joined:
    Apr 24, 2008
    #6
    Specifically, check section 6.7.5.2 (Array declarators). There, it states that the number between the brackets must be greater than zero. So it seems a zero-length array is not valid C.

    Other than that, like lee1210 wrote and Gnasher explained in his point 4, it wouldn't have been allowed to read or write from a[0] is a were a zero-size array anyway.
     

Share This Page