Thread: How do I fix up this 4d calloc array code please? gcc 6.4.0

  1. #1
    Registered User
    Join Date
    Dec 2017
    Posts
    2

    How do I fix up this 4d calloc array code please? gcc 6.4.0

    Hi guys,

    I am running gcc 6.4.0 in cygwin in Windows 10. I was looking for a 4D calloc array.

    I tried this neat one by brewbuck from March 2007:
    dynamically allocating 4D arrays

    Code:
    int ****array_4d;
    int dim1 = 5;
    int dim2 = 5;
    int dim3 = 5;
    int dim4 = 5;
    int i, j, k;
    
    array_4d = calloc(dim1, sizeof(array_4d[0]));
    for(i = 0; i < dim1; i++)
    {
        array_4d[i] = calloc(dim2, sizeof(array_4d[0][0]));
        for(j = 0; j < dim2; j++)
        {
            array_4d[i][j] = calloc(dim3, sizeof(array_4d[0][0][0]));
            for(k = 0; k < dim3; k++)
            {
                array_4d[i][j][k] = calloc(dim4, sizeof(array_4d[0][0][0][0]));
            }
        }
    }
    The code seems so straight forward and makes sense at a glance. However I got this error when I tried to compile it:

    $ gcc -o 4dcalloc 4dcalloc.c
    4dcalloc.c:18:1: warning: data definition has no type or storage class
    array_4d = calloc(dim1, sizeof(array_4d[0]));
    ^~~~~~~~
    4dcalloc.c:18:1: warning: type defaults to ‘int’ in declaration of ‘array_4d’ [-Wimplicit-int]
    4dcalloc.c:18:1: error: conflicting types for ‘array_4d’
    4dcalloc.c:11:9: note: previous declaration of ‘array_4d’ was here
    int ****array_4d;
    ^~~~~~~~
    4dcalloc.c:18:40: error: subscripted value is neither array nor pointer nor vector
    array_4d = calloc(dim1, sizeof(array_4d[0]));
    ^
    4dcalloc.c:19:1: error: expected identifier or ‘(’ before ‘for’
    for(i = 0; i < dim1; i++)
    ^~~
    4dcalloc.c:19:14: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘<’ token
    for(i = 0; i < dim1; i++)
    ^
    4dcalloc.c:19:23: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘++’ token
    for(i = 0; i < dim1; i++)
    ^~

    I don't know how else to ask this question, but here it is: how do I make these warnings go away please?

    The warning says array_4d has no type, but I thought it does. It has been declared int **** type hasn't it?

    I then tried this piece of code by Stack Overflow from April 2004:

    4-dimensional array contiguous allocation

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    /*
    Our example.
    
    int arr[2][2][3][4] = {
        {
            { {0, 1, 2, 3}, {4, 5, 6, 7}, {8, 9, 10, 11} },
            { {12, 13, 14, 15}, {16, 17, 18, 19}, {20, 21, 22, 23} },
        },
        {
            { {24, 25, 26, 27}, {28, 29, 30, 31}, {32, 33, 34, 35} },
            { {36, 37, 38, 39}, {40, 41, 42, 43}, {44, 45, 46, 47} },
        }
    };
    */
    
    int main() {
        /* Local variables */
        int i=0, j=0, k=0, l=0, f, m=0;
        int ****arr = NULL;
    
        /* arr[2] */
        arr = malloc(2 * sizeof *arr);
        if (arr) {
            /* arr[2][2] */
            for (i = 0; i < 2; i++) {
                /* Allocate memory */
                arr[i] = malloc(2 * sizeof *arr);
    
                /* If allocation failed */
                if (arr[i] == NULL) {
                        /* Free any memory that was previously used */
                        for (f = i; f >= 0; f--) {
                            if (arr[f] != NULL) {
                                free(arr[f]);
                                arr[f] = NULL;
                            }
                        }
                        /* Free any memory that was previously used from beginning */
                        if (arr != NULL) {
                            free(arr);
                            arr = NULL;
                        }
    
                        /* Terminate program */
                        return 0;
                }
    
                /* arr[2][2][3] */
                for (j = 0; j < 2; j++) {
                    /* Allocate memory */
                    arr[i][j] = malloc(3 * sizeof *arr);
    
                    /* If allocation failed */
                    if (arr[i][j] == NULL) {
                        /* Free any memory that was previously used */
                        for (f = j; f >= 0; f--) {
                            if (arr[i][f] != NULL) {
                                free(arr[i][f]);
                                arr[i][f] = NULL;
                            }
                        }
                        /* Free any memory that was previously used in previous loop */
                        for (f = i; f >= 0; f--) {
                            if (arr[f] != NULL) {
                                free(arr[f]);
                                arr[f] = NULL;
                            }
                        }
                        /* Free any memory that was previously used from beginning */
                        if (arr != NULL) {
                            free(arr);
                            arr = NULL;
                        }
    
                        /* Terminate program */
                        return 0;
                    }
    
                    /* arr[2][2][3][4] */
                    for (k = 0; k < 3; k++) {
                        /* Allocate memory */
                        arr[i][j][k] = malloc(4 * sizeof *arr);
    
                        /* If allocation failed */
                        if (arr[i][j][k] == NULL) {
                            /* Free any memory that was previously used */
                            for (f = k; f >= 0; f--) {
                                if (arr[i][j][f] != NULL) {
                                    free(arr[i][j][f]);
                                    arr[i][j][f] = NULL;
                                }
                            }
                            /* Free any memory that was previously used */
                            for (f = j; f >= 0; f--) {
                                if (arr[i][f] != NULL) {
                                    free(arr[i][f]);
                                    arr[i][f] = NULL;
                                }
                            }
                            /* Free any memory that was previously used in previous loop */
                            for (f = i; f >= 0; f--) {
                                if (arr[f] != NULL) {
                                    free(arr[f]);
                                    arr[f] = NULL;
                                }
                            }
                            /* Free any memory that was previously used from beginning */
                            if (arr != NULL) {
                                free(arr);
                                arr = NULL;
                            }
    
                            /* Terminate program */
                            return 0;
                        }
                    }
                }
            }
        }else {
            return 0;
        }
    
        /* Make sure we can write without failure */
        for (i = 0; i < 2; i++) {
            for (j = 0; j < 2; j++) {
                for (k = 0; k < 3; k++) {
                    for (l = 0; l < 4; l++) {
                        arr[i][j][k][l] = m++;
                    }
                }
            }
        }
    
        /* Print array style for example sake */
        printf("In example to: int arr[2][2][3][4];\n\n");
        printf("int ****arr = {\n");
        printf("\t{\n");
        printf("\t\t{ {%d, %d, %d, %d}, {%d, %d, %d, %d}, {%d, %d, %d, %d} },\n", arr[0][0][0][0], 
            arr[0][0][0][1], arr[0][0][0][2], arr[0][0][0][3], arr[0][0][1][0], arr[0][0][1][1], 
            arr[0][0][1][2], arr[0][0][1][3], arr[0][0][2][0], arr[0][0][2][1], arr[0][0][2][2], 
            arr[0][0][2][3]);
        printf("\t\t{ {%d, %d, %d, %d}, {%d, %d, %d, %d}, {%d, %d, %d, %d} },\n", arr[0][1][0][0], 
            arr[0][1][0][1], arr[0][1][0][2], arr[0][1][0][3], arr[0][1][1][0], arr[0][1][1][1], 
            arr[0][1][1][2], arr[0][1][1][3], arr[0][1][2][0], arr[0][1][2][1], arr[0][1][2][2], 
            arr[0][1][2][3]);
        printf("\t},\n");
        printf("\t\t{ {%d, %d, %d, %d}, {%d, %d, %d, %d}, {%d, %d, %d, %d} },\n", arr[1][0][0][0], 
            arr[1][0][0][1], arr[1][0][0][2], arr[1][0][0][3], arr[1][0][1][0], arr[1][0][1][1], 
            arr[1][0][1][2], arr[1][0][1][3], arr[1][0][2][0], arr[1][0][2][1], arr[1][0][2][2], 
            arr[1][0][2][3]);
        printf("\t\t{ {%d, %d, %d, %d}, {%d, %d, %d, %d}, {%d, %d, %d, %d} },\n", arr[1][1][0][0], 
            arr[1][1][0][1], arr[1][1][0][2], arr[1][1][0][3], arr[1][1][1][0], arr[1][1][1][1], 
            arr[1][1][1][2], arr[0][0][1][3], arr[1][1][2][0], arr[1][1][2][1], arr[1][1][2][2], 
            arr[1][1][2][3]);
        printf("\t}\n};");
    
        /* Free Memory */
        for (i = 0; i < 2; i++) {
            for (j = 0; j < 2; j++) {
                for (k = 0; k < 3; k++) {
                    free(arr[i][j][k]);
                    arr[i][j][k] = NULL;
                }
                free(arr[i][j]);
                arr[i][j] = NULL;
            }
            free(arr[i]);
            arr[i] = NULL;
        }
        free(arr);
        arr = NULL;
    
        return 0;
    }

    It compiled and ran like a charm.

    However I like the first piece of code better. It's a lot neater and easier for me to work with. I don't want to work with two pages of code for a 4d calloc array because my actual code will be much more complicated. This 4d core will be called upon again and again by my program.

    How may I amend the first piece of code so it will run without error please?

    Thank you.

  2. #2
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,670
    As it is, your code produces no errors or warnings. You've probably made a mistake someplace else.

    Have you included stdlib.h?
    Last edited by GReaper; 12-18-2017 at 04:12 AM.
    Devoted my life to programming...

  3. #3
    Registered User
    Join Date
    Dec 2017
    Posts
    2
    Hi GReaper, yes I did include those headers. This was the actual code I used to compile:

    Code:
    /*
    
    
    https://cboard.cprogramming.com/c-programming/88542-dynamically-allocating-4d-arrays.html
    
    
    */
    
    
    #include <stdio.h>      /* printf, scanf, NULL */
    #include <stdlib.h>     /* calloc, exit, free */
    
    
    
    
    int ****array_4d;
    int dim1 = 5;
    int dim2 = 5;
    int dim3 = 5;
    int dim4 = 5;
    int i, j, k;
     
    array_4d = calloc(dim1, sizeof(array_4d[0]));
    for(i = 0; i < dim1; i++)
    {
        array_4d[i] = calloc(dim2, sizeof(array_4d[0][0]));
        for(j = 0; j < dim2; j++)
        {
            array_4d[i][j] = calloc(dim3, sizeof(array_4d[0][0][0]));
            for(k = 0; k < dim3; k++)
            {
                array_4d[i][j][k] = calloc(dim4, sizeof(array_4d[0][0][0][0]));
            }
        }
    }
    I feel there is something simple I need to do to that code to make it compile error free.

  4. #4
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,670
    I may be misunderstanding you here, but if that is literally the entire code you want to compile, it's wrong. You can't compile without a proper function that contains the code. I took the code you posted in the first post and put it inside main, and it worked fine. You can't compile code without functions to encapsulate that code.
    Devoted my life to programming...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Calloc for an array of array with negative index in C
    By george_engineer in forum C Programming
    Replies: 6
    Last Post: 10-15-2014, 03:26 AM
  2. Calloc() problem on ARM code/compiler
    By ashok449 in forum C Programming
    Replies: 3
    Last Post: 02-15-2013, 09:42 AM
  3. Need help with allocating an array with calloc
    By MSF1981 in forum C Programming
    Replies: 2
    Last Post: 05-06-2009, 07:45 AM
  4. malloc ,calloc code problem..
    By transgalactic2 in forum C Programming
    Replies: 37
    Last Post: 10-26-2008, 11:43 AM
  5. CALLOC / 2-D Array Creation
    By wbeasl in forum C Programming
    Replies: 4
    Last Post: 12-02-2003, 01:59 PM

Tags for this Thread