从C中的函数返回具有多个可变长度数组的结构 [英] returning struct with multiple variable length arrays from function in C

查看:54
本文介绍了从C中的函数返回具有多个可变长度数组的结构的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在为此寻找解决方案.我想我知道发生了什么以及解决方案应该是什么,但是我不太确定如何实施它.

I have been searching for a solution for this for a while now. I think I know what is happening and what the solution should be, however I am not quite sure how to implement it.

我有一个包含两个可变长度数组的结构.这些将在函数中填充并返回给调用函数以进行处理.问题似乎是当被调用的函数超出范围时,可变长度数组的任何分配都变得无效.我猜想一个解决方案可能是在堆上分配内存,然后在调用函数中的结构完成后释放内存.下面给出一个代码示例

I have a struct that contains two variable length arrays. These are to be filled within a function and returned to the calling function to do work with. The problem seems to be that any assignment of the variable length array becomes invalid when the called function goes out of scope. I would guess that a solution may be to allocate the memory on the heap and then free the memory once I am done with the struct in the calling function. A code example is given below

struct fields {
  int n;
  double * A;
  double * B;
};

struct fields field() {
  int n = 4;
  double A[n] = { 1, 2, 3, 4 };
  double B[n] = { 1, 2, 3, 4 };

  struct fields field;
  field.n = n;
  field.A = A;
  field.B = B;
  /* field can be accessed with n, A, B set properly */
  return field;
}

double calling_function() {
  struct fields field1 = field();
  /* field1 contains n but A and B have not returned */
  .
  .
  .
}

推荐答案

首先,您的结构中实际上并没有两个可变长度数组.如果你这样做了,那将是一个错误.您实际上拥有的是两个指针,每个指针都可以指向任意大小数组的第一个元素.

First, you don't actually have two variable length arrays in your struct. It would be an error if you did. What you actually have is two pointers, each of which may point to the first element of an array of any size.

您遇到的问题是,您将在 field 函数中创建的数组作为局部变量,并将它们(或更准确地说是指向它们的第一个元素的指针)分配给正在创建的结构体回来.这意味着您正在从函数返回局部变量的地址.这些局部变量的生命周期在函数退出时结束,因此这些指针指向的内存是无效的(实际上指针值本身是不确定)并且尝试取消引用这些指针会触发 未定义的行为.

The problem you're having is that you're taking arrays created as local variables in the field function and assigning them (or more accurately pointers to their first elements) to the struct which is being returned. This means you're returning the address of local variables from the function. The lifetime of these locals end when the function exits, so the memory those pointers points to is invalid (and in fact the pointer values themselves are indeterminate) and attempting to dereference those pointers triggers undefined behavior.

另请注意,不能初始化变长数组.

Also note that a variable length array cannot be initialized.

您需要为 AB 字段动态分配空间并写入这些数组.但是,如果您不想复制单个成员,您可以使用现有的本地数组来执行 memcpy.

You need to dynamically allocate space for the A and B fields and write to those arrays. You can however use the existing local arrays to perform a memcpy if you don't want to copy individual members.

struct fields field() {
  double A[4] = { 1, 2, 3, 4 };
  double B[4] = { 1, 2, 3, 4 };

  struct fields field;
  field.n = 4;
  field.A = malloc(sizeof A);
  field.B = malloc(sizeof B);
  memcpy(field.A, A, sizeof A);
  memcpy(field.B, B, sizeof B);
  return field;
}

这篇关于从C中的函数返回具有多个可变长度数组的结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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