简体   繁体   中英

Unable to return a 3d array from function through pointers

I have a function which returns a 3-d array of integers as a pointer to the 0th 1-D array of the 0th 2-D array in it. It is of dimensions 2*3*4 . Here is my function:

    int (*ultimate())[4] {
        int a[2][3][4] = {
                {
                        {1,2,3,4},
                        {5,6,7,8},
                        {9,10,11,12}
                },
                {
                        {13,14,15,16},
                        {17,18,19,20},
                        {21,22,23,24}
                }
        };
        return (int (*)[4]) a;
    }

I am using this function in two different ways:

In the first method, I am taking the pointer to the 1-D array, and seeing that there are 6 such arrays, print out each one by one:

int (*q)[4];
q = ultimate();
for (int i = 0; i < 2*3; i++) {
    printf("\n");
    for (int k = 0; k < 4; k++) {
        printf("%d ",*(*(q+i)+k));
    }
}

In the next method, I am simply using the return value to get the address of the first element in the 3-D array, and then printing all:

int *p;
    for (int i = 0; i < 2; i++) {
        p = (int *) (q+i*3);
        pnl();
        for (int j = 0; j < 3; j++) {
            for (int k = 0; k < 4; k++) {
                printf("%d",*p);
                p++;
            }
        }
    }

However, in both cases, I am getting junk values. In the first method, I am getting some rows of the array while the other values are junk, while in the second method all values are junk. Any idea where I am being wrong?

You are creating the array inside the function on the stack. When the function exits, that memory is reclaimed. So there is no guarantee that you will be able to access those values.

The reason you are getting randomly correct values is that just by chance, that area of memory has not been overwritten yet. To avoid this, either declare the array on the heap using malloc or create a static constant, etc.


EDIT: By static constant I meant a global one outside the function. Technically the lifetime of a static variable inside a function is equal to the lifetime of the program. But because the C spec does not guarantee thread-safety, there may be potential errors while the program is being shut down.

EDIT 2: I managed to find an SO question which describes this problem: What is the lifetime of a static variable in a C++ function?

In C/C++ Functions may not declare an array as a return type.

For example in the C Standard there is written

6.7.6.3 Function declarators (including prototypes) Constraints 1 A function declarator shall not specify a return type that is a function type or an array type.

As for your code then it has undefined behaviour because the function returns pointer to a local object that has type of an array. After exiting the function the array will be destroyed.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM