Skip to main content
Engineering LibreTexts

5.4: Unions and memory errors

  • Page ID
  • There are two common uses of C unions. One, which we saw in the previous section, is to access the binary representation of data. Another is to store heterogeneous data. For example, you could use a union to represent a number that might be an integer, float, complex, or rational number.

    However, unions are error-prone. It is up to you, as the programmer, to keep track of what type of data is in the union; if you write a floating-point value and then interpret it as an integer, the result is usually nonsense.

    Actually, the same thing can happen if you read a location in memory incorrectly. One way that can happen is if you read past the end of an array.

    To see what happens, I’ll start with a function that allocates an array on the stack and fills it with the numbers from 0 to 99.

    void f1() {
        int i;
        int array[100];
        for (i=0; i<100; i++) {
            array[i] = i;

    Next I’ll define a function that creates a smaller array and deliberately accesses elements before the beginning and after the end:

    void f2() {
        int x = 17;
        int array[10];
        int y = 123;
        printf("%d\n", array[-2]);
        printf("%d\n", array[-1]);
        printf("%d\n", array[10]);
        printf("%d\n", array[11]);

    If I call f1 and then f2, I get these results:


    The details here depend on the compiler, which arranges variables on the stack. From these results, we can infer that the compiler put x and y next to each other, “below” the array (at a lower address). And when we read past the array, it looks like we are getting values that were left on the stack by the previous function call.

    In this example, all of the variables are integers, so it is relatively easy to figure out what is going on. But in general when you read beyond the bounds of an array, the values you read might have any type. For example, if I change f1 to make an array of floats, the results are:


    The latter two values are what you get if you interpret a floating-point value as an integer. If you encountered this output while debugging, you would have a hard time figuring out what’s going on.

    • Was this article helpful?