简体   繁体   English

通过ref从struct中的数组传回值

[英]passing back values from array in struct by ref

struct myStruct
{
    int* arr;
    int size;
};

void get_back(struct myStruct* my ,int* arr, int* size)
{
    arr = my->arr;
    *size = my->size;
}

int main()
{
    struct myStruct my;
    my.arr = (int*) malloc(3 * sizeof(int));
    my.arr[0] = 20;
    my.arr[1] = 200;
    my.arr[2] = 2000;
    my.size = 3;

    int* ret_arr = NULL;
    int size;
    get_back(&my, ret_arr, &size);
    free(my.arr);
    return 1;
}

The goal of my simple program is to get back the values from my.arr into ret_arr, 我的简单程序的目标是将my.arr中的值取回ret_arr,
since ret_arr=nullptr, do I need to allocate the memory and than copy it into the array inside get_back function? 由于ret_arr = nullptr,我是否需要分配内存并将其复制到get_back函数内部的数组中?
or I can just point to the existing array inside "my" struct? 还是只能指向“我的”结构中的现有数组?

This is my current solution, I copy the values. 这是我当前的解决方案,我复制值。

struct myStruct
{
    int* arr;
    int size;
};

int* get_back(struct myStruct* my , int* size)
{
    int *arr = (int*)malloc(3 * sizeof(int));
    for (int i = 0; i < my->size; i++)
    {
        arr[i] = my->arr[i];
    }
    *size = my->size;
    return arr;
}

int main()
{
    myStruct my;
    my.arr = (int*) malloc(3 * sizeof(int));
    my.arr[0] = 20;
    my.arr[1] = 200;
    my.arr[2] = 2000;
    my.size = 3;

    int* ret_arr = NULL;
    int size;
    ret_arr = get_back(&my, &size);
    free(my.arr);
    free(ret_arr);
    return 1;
}

Seeing as you're freeing the array, you probably want to copy the contents over with memcpy (from string.h ). 看到要释放数组时,您可能想要使用memcpy (来自string.h )复制内容。 You will also need to include stdlib.h for malloc . 您还需要为malloc包括stdlib.h

#include <string.h>
#include <stdlib.h>
// Struct definition goes here
void get_back(struct myStruct* my, int** arr, int* size)
{
    *arr=malloc(my->size*sizeof(int));          //Allocate space for integers
    memcpy(*arr, my->arr, my->size*sizeof(int));   //Copy integers to new array
    *size=my->size;
}

The function needs to take a pointer to the pointer in order to be able to modify it. 该函数需要使用指向该指针的指针才能对其进行修改。

Additionally, your main function will need to be modified too. 此外,您的main功能也将需要修改。

int main()
{
    struct myStruct my;                     // Structs are not types.
    my.arr = (int*) malloc(3 * sizeof(int));
    my.arr[0] = 20;
    my.arr[1] = 200;
    my.arr[2] = 2000;
    my.size = 3;

    int* ret_arr = NULL;
    int size;
    get_back(&my, &ret_arr, &size);        //Need to pass ret_arr by reference
    free(my.arr);
    return 1;
}

Use std::vector , it's very comfortable and has many useful algorithms, there is std::copy function to copy from one vector to another, take a look at your task with a help of vectors: 使用std::vector ,它非常舒适并且有许多有用的算法,有std::copy函数可以将一个向量复制到另一个向量,借助向量来查看您的任务:

#include <vector>
#include <iostream>

struct myStruct
{
    std::vector<int> arr;
};

int main()
{
    myStruct my;
    my.arr.push_back(20);
    my.arr.push_back(200);
    my.arr.push_back(2000);

    std::vector<int> ret_arr;

    std::copy(my.arr.begin(), my.arr.end(), std::back_inserter(ret_arr));

    return 1;
}

And the result is on screen: 结果显示在屏幕上:

在此处输入图片说明

If you want to use C language then you should pass pointer on pointer(int** arr) to get right pointing after leaving scope. 如果要使用C语言,则应在指针(int ** arr)上传递指针,以在离开范围后获得正确的指向。 I will show two methods, first just to point on already allocated memory: 我将展示两种方法,首先是指向已分配的内存:

void get_back_pointers(myStruct* my ,int** arr, int* size)
{
    *arr = my->arr;
    *size = my->size;
}

Other is for deep copy, to allocate new array and copy data to it: 另一种是用于深层复制,以分配新数组并将数据复制到该数组:

void get_back_copy(myStruct* my ,int** arr, int& size)
{
    *arr = (int*) malloc(3 * sizeof(int));
    memcpy( *arr, my->arr, my->size * sizeof(int) );
    size = my->size;
}

After get_back_copy passed arr will be needed to free its memory. get_back_copy传递后,需要arr释放其内存。 In debugger you can see results that my.arr and ret_arr have one address but ret_arr2 has another because it's allocated in new memory: 在调试器中,您可以看到结果my.arrret_arr有一个地址,而ret_arr2有另一个地址,因为它是在新内存中分配的:

在此处输入图片说明

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

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