-3
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,
since ret_arr=nullptr, do I need to allocate the memory and than copy it into the array inside get_back function?
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;
}
Gilad
  • 6,437
  • 14
  • 61
  • 119

2 Answers2

1

Seeing as you're freeing the array, you probably want to copy the contents over with memcpy (from string.h). You will also need to include stdlib.h for malloc.

#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.

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;
}
  • 1
    I get Unhandled exception at 0x582E31CA (msvcr120d.dll) in CppTest.exe: 0xC0000005: Access violation reading location 0x035DD000. something is wrong is the malloc function – Gilad May 03 '18 at 19:44
  • I accidentally specified the wrong file to include, try now. –  May 03 '18 at 19:47
  • 1
    sorry this code is not working, you are missing cast to (int*) from the malloc void* return type and also this is not running – Gilad May 03 '18 at 19:55
  • 1
    I forgot to add the changes for main(). Also, casting to int* from malloc isn't necessary. –  May 03 '18 at 19:56
  • 2
    [should I cast the return of malloc?](https://stackoverflow.com/questions/605845/do-i-cast-the-result-of-malloc) – Tormund Giantsbane May 03 '18 at 19:58
-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:

#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:

enter image description here

If you want to use C language then you should pass pointer on pointer(int** arr) to get right pointing after leaving scope. 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. 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:

enter image description here

Alexey Usachov
  • 1,364
  • 2
  • 8
  • 15