2

I'm trying to allocate a 3D array in C. As you can tell, it's to store image data, in the form Array[height][width][channel]; This is the code I have.

BYTE*** allocateImageArray(INT_32 width, INT_32 height, INT_32 channels) {
    BYTE*** array;
    INT_32 i, j;

    array = malloc(height * sizeof(BYTE**));

    for(i = 0; i < height; i++) {
        array[i] = malloc(width * sizeof(BYTE*));

        for(j = 0; j < channels; j++) {
            array[i][j] = malloc(channels * sizeof(BYTE));
        }
    }

    printf("Pixel Array Size: %d\n", sizeof(array));
    return array;
}

It compiles fine. However, the array is always 8 at the end (Indicated by the sizeof). And when I try to access the array later in the code the program just crashes.

All answers are thanked in advanced.

EDIT: Forgot to include. I've worked out where the program crash occurs. It happens when I try to assign a value to one of the BYTES at the end of the array.

4 Answers4

2

The sizeof operator does not tell you the size of dynamically allocated arrays.

sizeof returns the size in bytes of the underlying datatype, which in your case is a pointer to a pointer to a pointer to a BYTE.

As array is still a pointer (regardless of what it points to) the returned size of 8 bytes makes sense.

Please refer to the sizeof operator explanation.

Aside:

I would also recommend having a look at Why does sizeof(x++) not increment x? for an interesting error that can prop up with the sizeof operator and assumptions that new programmers make about it.

Community
  • 1
  • 1
nonsensickle
  • 4,438
  • 2
  • 34
  • 61
2

The sizeof operator doesn't work like that. Think if it more as a special #define macro that figures out the size of something based on its type.

So sizeof(array) is the same as sizeof(BYTE***) which is the same as sizeof(void*) which is apparently 8 for your computer.

randomusername
  • 7,927
  • 23
  • 50
  • Thanks for both answers on this. Makes sense. Any ideas on why it's crashing? Is the code wrong to allocate it, or another possible issue? I've worked the crash out occur when I try assign a value to one of the array entries. – user3039913 Nov 27 '13 at 02:32
2

BYTE** is just a pointer, so in the end sizeof(BYTE **) always returns 8.

You may try the following code:

#define value_at_index(a,i,j,k,m,n) (a)[(i)*m*n + (j)*n + (k)]

static BYTE*** allocateImageArray(int width, int height, int channels) {
    BYTE (*array)[width][height];

    array = malloc(width * height * channels * sizeof(BYTE));
    array[1][2][3] = 1;

    printf("array[1][2][3] = %d", array[1][2][3]);

    return (BYTE***)array;
}

Doing so, you have a contiguous memory space for the 3-dimension array, then in the main() you can call like this:

BYTE ***array = allocateImageArray(10, 20, 30);
printf("array[1][2][3] = %d", value_at_index(array, 1, 2, 3, 10, 20));
Krypton
  • 3,337
  • 5
  • 32
  • 52
  • I have edited my answer. I tried my old code and I realized that returning a pointer won't work, as it doesn't know how to interpret the result of ``BYTE ***array = allocateImageArray(10, 20, 30);`` So I added the #define macro for the purpose. – Krypton Nov 27 '13 at 03:10
1

sizeof(array) == the number of bytes for a pointer, which is 8 bytes in your case.

Fiddling Bits
  • 8,712
  • 3
  • 28
  • 46