in a function and that function return that array
how should i delete that array ?
because that array is in heap and i dont want to make a memory leak in my program
int* func(){
int* arr = new int [10];
//make changes in array
return arr;
}
thank you
That's why you shouldn't return arrays in this way and delegate the responsibility of deleting to a caller. Why don't you use vector?
no i just wanted to know if there is a solution for that case thank you
Caller must delete it or you can return a unique pointer instead of a raw one.
sorry what do you mean by caller? and what do you mean by unique pointer and raw pointer?
Caller - code that calls this method and uses the pointer. unique_ptr - https://en.cppreference.com/w/cpp/memory/unique_ptr raw pointer - regular C style pointer
i got it thanks again
Обсуждают сегодня