Похожие чаты

Hey guys i have a question im declaring a dynamic array

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

6 ответов

4 просмотра

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?

SULLIVAN- Автор вопроса
Ihor
That's why you shouldn't return arrays in this way...

no i just wanted to know if there is a solution for that case thank you

SULLIVAN
no i just wanted to know if there is a solution fo...

Caller must delete it or you can return a unique pointer instead of a raw one.

SULLIVAN- Автор вопроса
Ihor
Caller must delete it or you can return a unique p...

sorry what do you mean by caller? and what do you mean by unique pointer and raw pointer?

SULLIVAN
sorry what do you mean by caller? and what do you ...

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

SULLIVAN- Автор вопроса

Похожие вопросы

Обсуждают сегодня

Вопрос по диагностике ошибок (я знаю в чем, в данном конкретном примере, я знаю, как исправить, пример модельный, понятно, что в реальности бывает намного запутаннее). module...
ⰄⰎⰋⰐⰐⰑⰛⰤⰧⰧⰩⰄ ⰊⰑⰁⰓⰡⰛⰦⰕⰫ
10
Тут кста кто-нибудь NeoVim использует?
Simple Sorcerer
13
А чем вам питонисты не угодили?😂
.
79
Есть какой-нибудь для Delphi/FPC T*Compression(Decompression)Stream на базе LZ4/Zstd/любой другой быстрый(и хорошо сжимающий) алгоритм А ещё лучше в pure pascal А ещё лучше од...
notme
52
New Hedera drama. Is Hashinals on chain or not on chain?
Perfect Ability
15
Can someone explain me the difference of the stated quote on Quants homepage between R3 and Quant? „R3 brings its ability to deliver complex pioneering projects for regulate...
Carlson
2
Asus, норм фирма для ноутов?
Артем Записной
20
guys, why is it taking days for my node to sync? is this normal?
Big Chiano
10
А дальше что?.. Записать в файл, потом в Код?.. И потом разбирать как-то?..
Хаскель Моисеевич Гопник
14
Ребят немного глупый вопрос, но я правильно понимаю что неполнота по геделю означает наличие парадокса?
Smith
9
Карта сайта