从 C 中的函数返回数组:分段错误 [英] Returning an array from a function in C: Segmentation Fault

查看:14
本文介绍了从 C 中的函数返回数组:分段错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用头文件实现一个简单的程序,其中头文件中的函数接受 int 数组并返回 int 数组.

header.h中:

int* point(int a[]);

header.c中:

#include<stdio.h>#include "header.h"int* 点(int a[]){printf("在点函数中
");整数数组[4],我;for(int i=0;i<4;i++){printf("%dth 迭代
",i);数组[i]=a[i];}返回数组;}

test.c中:

#include<stdio.h>#include "header.h"无效的主要(){整数*数组,我;int a[]={1,2,3,4};printf("调用点函数
");数组=点(一);printf("回到主函数
");for(i=0;i<4;i++){//这里的分段错误printf("%d
",array[i]);}}

我在 test.c 的打印循环中遇到分段错误.

解决方案

你不能从函数返回数组.当 point() 返回时,此函数内的本地数组超出范围.这个数组是在堆栈上创建的,一旦函数完成返回就会被销毁.与它关联的所有内存都被丢弃,返回的指针指向堆栈上不再存在的位置.您需要改为在堆上分配一个指针,然后返回它.这允许 array 在您的程序中共享.

代替:

int 数组[4];

您需要使用 malloc():

int *array = malloc(4 * sizeof(*array));/* 或 sizeof(int) */如果(数组 == NULL){/* 处理退出 */}

<块引用>

malloc() 在堆上分配请求的内存,并返回一个 void* 指向它的指针.

注意: malloc() 不成功时会返回NULL,所以需要经常检查.您还需要free() 之前分配的任何内存通过 malloc().您也不需要强制返回 malloc().

要指出的另一件事是在整个程序中使用幻数 4.这实际上应该使用 sizeof(a)/sizeof(a[0]) 来计算.

您可以在 main() 中将其声明为 size_t 变量:

size_t n = sizeof(a)/sizeof(a[0]);

或者你可以使用宏:

#define ARRAYSIZE(arr) (sizeof(arr)/sizeof(arr[0]))

每次您想要数组的大小时,只需调用 ARRAYSIZE(a).

I am trying to implement a simple program using a header file where a function in the header file accepts an int array and returns an int array too.

In header.h:

int* point(int a[]);

In header.c:

#include<stdio.h>
#include "header.h"

int* point(int a[])
{
 printf("In the point function
");
 int array[4],i;
 for(int i=0;i<4;i++)
 {
    printf("%dth Iteration
",i);
    array[i]=a[i];
 }

return array;
}

In test.c:

#include<stdio.h>
#include "header.h"
void main()
{
 int *array,i;
  int a[]={1,2,3,4};
   printf("calling point function
");
  array=point(a);
  printf("Back in the main function
");
  for(i=0;i<4;i++)
  {
    //SEGMENTATION FAULT HERE
    printf("%d
",array[i]);
  }

}

I am getting a segmentation fault at the print loop in test.c.

解决方案

You cannot return arrays from functions. When point() returns, the local array within this function goes out of scope. This array is created on the stack, and will get destroyed once the function finishes returning. All memory associated with it is discarded, and the returned pointer points to a position on the stack that doesn't exist anymore. You need to instead allocate a pointer on the heap, and return that instead. This allows array to be shared across your program.

Instead of:

int array[4];

you need to dynamically allocate a pointer using malloc():

int *array = malloc(4 * sizeof(*array)); /* or sizeof(int) */
if (array == NULL) {
    /* handle exit */
}

malloc() allocates requested memory on the heap, and returns a void* pointer to it.

Note: malloc() can return NULL when unsuccessful, so it needs to be checked always. You also need to free() any memory previously allocated by malloc(). You also don't need to cast return of malloc().

Another thing to point out is using the magic number 4 all over your program. This should really be calculated using sizeof(a)/sizeof(a[0]).

You can declare this as a size_t variable in your main():

size_t n = sizeof(a)/sizeof(a[0]);

Or you can use a macro:

#define ARRAYSIZE(arr) (sizeof(arr) / sizeof(arr[0]))

And simply call ARRAYSIZE(a) everytime you want the size of the array.

这篇关于从 C 中的函数返回数组:分段错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆