简体   繁体   English

从函数返回的错误数组值

[英]Wrong array values returning from a function

I am making this program in which my main function calls a function which returns an array after the calculation.我正在制作这个程序,其中我的主函数调用一个函数,该函数在计算后返回一个数组。 I checked already that calculation is right inside the local function.我已经检查过计算就在本地函数内部。 But when I return that array to 'main' function then I only can print the correct value one time and it prints wrong value all other times.但是当我将该数组返回到 'main' 函数时,我只能打印一次正确的值,而其他时间打印的值都是错误的。

#include <math.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <assert.h>
#include <limits.h>
#include <stdbool.h>
int* getJoinedPipes(int input1_size, int* input1,int* output_size){
  int i,j,temp;
  int op1[input1_size-1];
  *output_size = input1_size - 1;
  for(i=0; i<input1_size; i++){
    for(j=i+1; j<input1_size; j++){
      if(input1[i] > input1[j]){
        temp     = input1[i];
        input1[i] = input1[j];
        input1[j] = temp;
      }
    }
  }

  op1[0]=input1[0] + input1[1];
  for(i=1;i<input1_size-1;i++){
    op1[i] = op1[i-1]+input1[i+1];
  }
  //printf("%d\n",op1[2]);

  return op1; 
}

int main() {
  int output_size;
  int* output;

  int ip1_size = 0;
  int ip1_i;
  scanf("%d\n", &ip1_size);
  int ip1[ip1_size];
  for(ip1_i = 0; ip1_i < ip1_size; ip1_i++) {
    int ip1_item;
    scanf("%d", &ip1_item);

    ip1[ip1_i] = ip1_item;
  }
  output = getJoinedPipes(ip1_size,ip1,&output_size);
  printf("a==%d\n",output[0]);
  printf("a==%d\n",output[0]);
  printf("a==%d\n",output[0]);
  int output_i;
  for(output_i=0; output_i < output_size; output_i++) {
    printf("%d\n", output[output_i]);
  }

  return 0;
}

Output should be输出应该是

5
9
15

But in the console, it's showing the following (after dry run).但是在控制台中,它显示以下内容(在试运行后)。 看图

a==5    
a==1943372821    
a==1943372821    
1943372821    
17    
6356632

You can see first time its giving correct value (5) and later for same print its giving garbage values.您可以第一次看到它给出正确的值(5),然后在同样的打印中看到它给出的垃圾值。

op1 is an automatic array. op1是一个自动数组。 You cannot return it and use it outside its scope.您不能退回它并在其范围之外使用它。

op1 exists only within getJoinedPipes , if you return it, the result is Undefined Behaviour . op1只存在于getJoinedPipes ,如果你返回它,结果是Undefined Behavior

To fix it you can either:要修复它,您可以:

  • pass op1 as a parameter to getJoinedPipedop1作为参数传递给getJoinedPiped
  • allocate op1 on the heap dynamically.在堆上动态分配op1 You you do that, you can safely return op1 but you have to remember to free it when you don't need it.你这样做,你可以安全地返回op1但你必须记住在你不需要它时free它。

You are returning pointer to a local variable object whose lifetime has ended yet when you are trying to get its values -> horrible mistake and undefined behaviour.当您尝试获取其值时,您正在返回指向其生命周期已结束的局部变量对象的指针 -> 可怕的错误和未定义的行为。

If you want to return an array from function, allocate it dynamically via malloc , and dont forget to free it after you are done with that array.如果您想从函数返回一个数组,请通过malloc动态分配它,并且在完成该数组后不要忘记free它。 You should also check return value of malloc if you got memory you asked for.如果您获得所需的内存,您还应该检查malloc返回值。

Correct正确的

#include <math.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <assert.h>
#include <limits.h>
#include <stdbool.h>
int* getJoinedPipes(int input1_size, int* input1,int* output_size){
int i,j,temp;
int * op1 = (int *)malloc(sizeof(int) * (input1_size-1));
*output_size = input1_size - 1;
for(i=0; i<input1_size; i++){
    for(j=i+1; j<input1_size; j++){
        if(input1[i] > input1[j]){
            temp     = input1[i];
            input1[i] = input1[j];
            input1[j] = temp;
        }
    }
}

op1[0]=input1[0] + input1[1];
for(i=1;i<input1_size-1;i++){
    op1[i] = op1[i-1]+input1[i+1];
}
//printf("%d\n",op1[2]);

return op1;
}

int main() {
int output_size;
int* output;

int ip1_size = 0;
int ip1_i;
scanf("%d\n", &ip1_size);
int ip1[ip1_size];
for(ip1_i = 0; ip1_i < ip1_size; ip1_i++) {
    int ip1_item;
    scanf("%d", &ip1_item);

    ip1[ip1_i] = ip1_item;
}
output = getJoinedPipes(ip1_size,ip1,&output_size);
printf("a==%d\n",output[0]);
printf("a==%d\n",output[0]);
printf("a==%d\n",output[0]);
int output_i;
for(output_i=0; output_i < output_size; output_i++) {

    printf("%d\n", output[output_i]);

}

free(output);
return 0;
}

You should check your indentation... apart from that, I guess the problem could arise because the array you output in your function is created on the function stack.你应该检查你的缩进......除此之外,我想可能会出现问题,因为你在函数中输出的数组是在函数堆栈上创建的。 So what you get as an output array is a reference on the stack of your joinedPipes-function.因此,作为输出数组获得的是对joinedPipes 函数堆栈的引用。 Try to pass your array as an argument to the function and not create it as a return value.尝试将数组作为参数传递给函数,而不是将其创建为返回值。

Hope that does the trick...希望能解决问题...

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

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