简体   繁体   English

c-在函数中初始化的数组的指针在返回后包含错误的值

[英]c - Pointer of array initialized in a function contains wrong values after returning

I'm facing an issue and I'm certainly doing something wrong. 我面临一个问题,我肯定做错了什么。 I need to call a function that returns a pointer to an array of int but when after it returns, the values inside the array are wrong and some values are missing. 我需要调用一个函数,该函数返回一个指向int数组的指针,但是当它返回后,该数组内的值错误并且缺少某些值。

int* patternForFirstDigit(int digit) {
    int *pattern;
    pattern = (int [6]){1,1,1,1,1,1};

    switch (digit) {
        case 0:
            pattern = (int [6]){1,1,1,1,1,1};
            break;

        case 1:
            pattern = (int [6]){1,1,2,1,2,2};
            break;

        default:
            pattern = (int [6]){0,0,0,0,0,0};
            break;
    }

    for (int i = 0; i < 6; i++) {
         printf("%i\n", pattern[i]);
    }

    return pattern;
}

In case of digit = 1, here's what's printed 如果数字= 1,这是打印的内容

1, 1, 2, 1, 2, 2

But after returning 但是回来之后

int *pattern = patternForFirstDigit(0);
for (int i = 0; i < 6; i++) {
     printf("%i\n", pattern[i]);
}

here's what's printed 这是印刷的

1, -1405451528, -1405449120, 366001

Do you have an idea of what's wrong ? 你有什么想法吗?

Thanks guys 多谢你们

PS : I'm using Xcode 4.6 and my project is using ARC but I'm pretty sure it's not the reason of my problem. PS:我使用的是Xcode 4.6,我的项目使用的是ARC,但我敢肯定这不是问题的原因。

You can not return a pointer to an array created in a function. 您不能返回指向在函数中创建的数组的指针。 This array is no longer existing after a function returns, so your pointer points to some random, invalid place in a memory. 函数返回后该数组不再存在,因此您的指针指向内存中某个随机的无效位置。

Allocate memory for a pointer (using malloc() for instance) and then return a pointer. 为指针分配内存(例如,使用malloc() ),然后返回指针。 This would also mean you'd need to free a pointer after you're done with it (with free() ). 这也意味着您需要在使用完指针之后释放指针(使用free() )。

Pseudocode for this would be something like: 伪代码将类似于:

int* patternForFirstDigit(int digit) {
  int *pattern = (int*) malloc(sizeof(int)*N);
  pattern[0] = 0;
  pattern[1] = 1;
  ...

  // Alternatively just create a local array and use a for-loop
  // to copy the contents to the pattern array.

  return pattern;
}

int *p = patternForFirstDigit(M);
// use p
free(p);

the

(int [6]){1,1,2,1,2,2};

is a local array defined in the function . 是在函数中定义的局部数组。 so the data of the array could be erased when the function finish the execution. 因此,当函数完成执行时,可以擦除数组的数据。 so that's why you get garbage values in your printf 这就是为什么在printf获得垃圾值的原因

1) Use malloc to allocate array instead 1)使用malloc代替分配数组

int* patternForFirstDigit(int digit) {
    int *pattern = malloc(6*sizeof(int));

    memcpy(pattern, (int [6]){1,1,1,1,1,1}, 6*sizeof(int));

    switch (digit) {
        case 0:
            memcpy(pattern, (int [6]){1,1,1,1,1,1}, 6*sizeof(int));
            break;

        case 1:
            memcpy(pattern, (int [6]){1,1,2,1,2,2};, 6*sizeof(int));
            break;

        default:
            memcpy(pattern, (int [6]){0,0,0,0,0,0}, 6*sizeof(int));
            break;
    }

    for (int i = 0; i < 6; i++) {
         printf("%i\n", pattern[i]);
    }

    return pattern;
}

and some where in your code when the pattern became useless then free it with free(pattern); 以及当pattern变得无用时在代码中的某些位置,然后使用free(pattern);将其free(pattern);

2) Or use static in the definition of the array: 2)或在数组的定义中使用static

int* patternForFirstDigit(int digit) {
    int *pattern; int i;
    static int A[6]={1,1,1,1,1,1};
    static int B[6]={1,1,2,1,2,2};
    static int C[6]={0,0,0,0,0,0};
    pattern = A;

    switch (digit) {
        case 0:
            pattern = A;
            break;

        case 1:
            pattern = B;
            break;

        default:
            pattern = C;
            break;
    }

    for (i = 0; i < 6; i++) {
         printf("%i\n", pattern[i]);
    }

    return pattern;
}

The problem is because you are returning a local variable. 问题是因为您正在返回局部变量。 A local variable is a temporary one that is no longer available after its scope is gone. 局部变量是一个临时变量,在其作用域消失后不再可用。 You may try the following: 您可以尝试以下方法:

int* patternForFirstDigit(int digit, int* pattern) {

    int* pattern1 = (int [6]){1,1,1,1,1,1};
int i;
    switch (digit) {
        case 0:
            pattern1 = (int [6]){1,1,1,1,1,1};
            break;

        case 1:
            pattern1 = (int [6]){1,1,2,1,2,2};
            break;

        default:
            pattern1 = (int [6]){0,0,0,0,0,0};
            break;
    }
memcpy(pattern,pattern1,6*sizeof(int));
    for ( i = 0; i < 6; i++) {
         printf("%i\n", pattern[i]);
    }
    return pattern;
}

Then you may use it like: 然后您可以像这样使用它:

pattern = malloc(6*sizeof(int));
pattern=patternForFirstDigit(1, pattern);
int i;
for ( i = 0; i < 6; i++) {
     printf("%i\n", pattern[i]);
}
free(pattern);

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

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