简体   繁体   English

无法通过指针从函数返回3d数组

[英]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. 我有一个函数,该函数返回整数的3-d数组作为指向其中第0个2-D数组的第0个1-D数组的指针。 It is of dimensions 2*3*4 . 它的尺寸为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: 在第一种方法中,我将指针指向一维数组,并看到有6个这样的数组,逐个打印出每个数组:

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: 在下一个方法中,我只是使用返回值来获取3-D数组中第一个元素的地址,然后打印所有内容:

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. 为了避免这种情况,请使用malloc在堆上声明数组,或者创建一个静态常量等。


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. 但是由于C规范不能保证线程安全,因此在关闭程序时可能会出现潜在的错误。

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? 编辑2:我设法找到一个描述此问题的SO问题: C ++函数中静态变量的生存期是多少?

In C/C++ Functions may not declare an array as a return type. 在C / C ++中,函数不能将数组声明为返回类型。

For example in the C Standard there is written 例如在C标准中

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. 6.7.6.3函数声明符(包括原型)约束1函数声明符不得指定作为函数类型或数组类型的返回类型。

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. 退出函数后,数组将被销毁。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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