Answer the question
In order to leave comments, you need to log in
How to correctly pass an array to a method and get back an array of a different size?
It is necessary to pass an array to the method, where it is processed and return the result as an array of a different size.
I understand that it is necessary to create a pointer before the call, bind an array to it through new, pass it to the method, then after processing delete the bound data through delete [] and create new ones through new.
Confused about * and & ))
Simplified:
Метод(float *arr) {
... использование данных
delete [] arr;
float *newArr;
newArr = new float[newSize];
... заполнение новыми данными
arr = &newArr;
}
float *a;
a = new float[size];
Метод(a);
Answer the question
In order to leave comments, you need to log in
You don't need new
raw pointers either. You need a linear container with elements stored in a contiguous block of memory. It will be either std::vector
, or std::array
.
For starters, you can focus on the vector . Another important type would be std::span
[ ? ] or gsl::span
[ ? ][ S ] if you can't use C++20.
When implementing your function, you need to understand why you need to add memory management to your function. In general, if a developer begins to manage memory in a computational function, this indicates a poorly thought out algorithm for the function. In rare cases, memory management may be technically justified. Your case is not one of those. There is no need to manage memory in it, this will not do any good, but will only lead to overcomplicating the implementation.
Here is your function declaration: gsl::span<float> Метод( gsl::span<float> values )
.
span
is a non-memory type denoting a piece of contiguous memory with data of a certain type. span
is very lightweight and is a value type - i.e. It was created so that its passing by value does not lead to tangible loads. span
is constructed from std::vector
, std::array
, flat arrays, std::unique_ptr<[]>
and raw memory blocks.
In your function, you should work with memory values
, counting along the way how many elements you have processed. And to return the processed data section, it will be enough for you to call subspan .
std::span<float> Метод( std::span<float> values )
{
size_t processed_count = 0;
for( float& element : values )
{
// ... обработка значений
// ... изменение processed_count
// ... условия обрыва цикла
}
return values.subspan( 0, processed_count );
}
int main()
{
std::vector<float> values{ 7.83f, 14.1f, 20.3f };
std::span<float> processed_values = Метод( values );
for( const float& value : processed_values )
{
std::cout << value << ' ';
}
return 0;
}
First option
1) You need to pass a pointer by reference to change its value when memory is allocated with new[]
2) You have to change pointer a (from main) by reference arr
Code:
Метод(float& *arr) {}
arr = newArr;
void foo(float& *arr) {
// ... использование данных
delete [] arr;
float *newArr = new float[newSize];
//... заполнение новыми данными
arr = newArr;
}
float *a = new float[size];
foo(a);
float* foo(float *arr) {
// ... использование данных
delete [] arr;
float *newArr = new float[newSize];
//... заполнение новыми данными
return newArr;
}
float *a = new float[size];
a = foo(a);
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question