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?
Jussi
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...
Jussi
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
Anonymous
return_type fun(){
int arr[] = {1,2,3};
}
Anonymous
return_type fun(){
int arr[] = {1,2,3};
return ?
}
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
Anonymous
and with array it gets destroyed?
Anonymous
Mihail
Jussi
the pointer is still pointing to the memory location where the array was locally stored, when it was in scope
Anonymous
Mihail
Jussi
Anonymous
wait wait
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
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)