Corn
neat
You are in this chat too? That's sick!
Anonymous
I'm pretty much everywhere lol
Kenneth
Hi everyone!
Kenneth
New here
Anonymous
how to return an array by value from a function?
Anonymous
can you explain it with a code?
Jussi
oh, an array
Jussi
what kind of array?
Anonymous
yes
Anonymous
int
Jussi
you can always make a vector and return it
Anonymous
int arr[] = {1,2,3};
Jussi
it is the C++ way,
Anonymous
how to return it by value
Jussi
if you are in C, then you must malloc the memory for it
Anonymous
yes by cpp way
Anonymous
no no
Anonymous
cpp way
Anonymous
just cpp
Anonymous
yeag
Anonymous
yeah
Jussi
std::vector<int> arr = {1,2,3}; return arr;
Jussi
you must copy the value from the function, because after return the array/value would otherwise go out of scope and get trashed
Anonymous
not vector
Anonymous
an array
Jussi
why must it be an array and not vector??? You said you are using C++
Andrea
you cant, is allocated on the stack and will die when the function return
Jussi
the vector gets copied...
Anonymous
We can have int arr[] = {1,2,3}; as an example
Andrea
you have to use a vector, or allocate it using malloc or new
Anonymous
I know how to do it with vector but not with array
Jussi
We can have int arr[] = {1,2,3}; as an example
you get the excact same thing if you do std::vector<int> arr = {1,2,3}; int* pointer_to_int_array = &arr[0]; ...
Anonymous
return_type fun(){ int arr[] = {1,2,3}; }
Anonymous
return_type fun(){ int arr[] = {1,2,3}; return ? }
Jussi
return_type fun(){ int arr[] = {1,2,3}; }
well you can not. The arr will get deleted when it goes out of scope
Andrea
and technically if you put it on the heap you are not returning the array by value but you are return the address of the first element
Jussi
that is just invalid code right there
Anonymous
Anonymous
how does above not get destroyed
Jussi
and technically if you put it on the heap you are not returning the array by value but you are return the address of the first element
exactly. vector is also stored in heap, and when you return the vector, you actually return a reference/pointer to it
Anonymous
and with array it gets destroyed?
Jussi
so how does int fun(){ int x=6; return x; }
here int is one integer, which will be passed through stack, and the integer fits into one CPU register
Jussi
and with array it gets destroyed?
if you return array, you actually return POINTER to the array. The array itself gets destroyed
Jussi
the pointer is still pointing to the memory location where the array was locally stored, when it was in scope
Jussi
return std::array(arr);
he wanted to use raw C-type arrays
Jussi
He said the c++ way
yeah but he didn't accept the C++ way answer that I posted
Anonymous
wait wait
Jussi
OK then what about the case with return x;
in 64-bit machines, both pointers and integers are 64 bits long. So it makes sense?
Anonymous
in case of array we return the pointer to first element and since the array is in stack, it gets destroyed. What does "destroyed" mean?
Jussi
it gets overwritten by other values that program will write to the stack
Jussi
imagine this
Jussi
int* fun() { int a = 1; return &a; } will it work? no. why?
Anonymous
because a is in stack and gets destroyed
Jussi
return_type fun(){ int arr[] = {1,2,3}; return ? }
it's the same thing with this example you posted
Anonymous
so the address that is returned has some arbitrary value
Anonymous
is that so?
Mihail
It could have any value
Jussi
yes
Anonymous
ya ya arbitrary
Anonymous
Is this fine?
Anonymous
int* fun(int arr[]){ return arr; }
Anonymous
sometimes I've seen someone do return (int*) arr;
Anonymous
What the hell does that mean?
Jussi
yes it is, because it gets the pointer from the caller
Jussi
and the caller has most likely allocated the array
Jussi
but returning the pointer is pointless (lol), because the caller already has the address of the array it has passed to the function
Anonymous
ya that really is pointless
Jussi
of course, if the function realloc s or moves the array, then it has to tell where it moved it to the calling function
Anonymous
but suppose I've a function that has an array passed as parameter then I want to remove continuous duplicate values such as 1,1,3,3,3,3,5,6 to 1,3,5,6 and want to do that inplace and return the array So how should I also return the size of the array in which the duplicated values are not present?
Anonymous
I mean If i do return arr;
Anonymous
how would the caller know the size of the array without duplicate elements?
Jussi
if you use C++, you can do this again with a std::vector and remove the elements. Then the caller may call std::vector::size to get its new size
Jussi
if you want to do it with C-style array, then you must for example reserve space for the size of the array (for example, the first array value could be the size of the array)