Похожие чаты

Hi there Does deleting the 'instance' on line 24 result in

deleting all data in the 'func' function (including 'output' and also static 'x') ?

20 ответов

16 просмотров

output is the only thing you allocate, so it's the only thing you need to delete from inside func(). BUT this still leaks memory, because the original instance is overwritten when you reassign instance, so that is never deleted

A* instance = new A; delete instance; instance = instance->func(); delete instance;

L21 calls new L11 calls new (called by L22) So 2 calls to delete are required to clean up

To answer your question; no Deleting instance results in output being deleted. int number is basically automatically deleted when the function returns, static int x is never deleted, and never created, there's just one x for the entire lifetime of the program

Sahand-🏔️ Автор вопроса
Thomas
L21 calls new L11 calls new (called by L22) So 2 c...

Yes, technically I need to delete 'output' if I want to deallocate it. But also I need it to return from my function.. actually I will be happy if it gets deleted by deleting the 'instance' variable, so the memory leaks will be avoided ....

Why should deleting the object instance delete all data in func?

Sahand 🏔️
Yes, technically I need to delete 'output' if I wa...

Are you on Linux? I think you should try ltrace

Sahand-🏔️ Автор вопроса
Azadi
Why should deleting the object instance delete all...

I thought deleting instance result in deleting on-heap-allocated data too .. that's my guess

Sahand 🏔️
I thought deleting instance result in deleting on-...

It does, but variables in functions are not on-heap

Sahand-🏔️ Автор вопроса
Sahand-🏔️ Автор вопроса
Sahand-🏔️ Автор вопроса
Thomas
output is the only thing you allocate, so it's the...

What do you mean by "when you reassign instance" ? Like what I did on line 22 here ? https://t.me/thedevs_c/246528

Sahand 🏔️
I thought deleting instance result in deleting on-...

delete will free the memory allocated by new. So in this case yes. As for the static variable,it remains until your program terminates.

Sahand-🏔️ Автор вопроса
Thomas
output is the only thing you allocate, so it's the...

Here you said "that is never deleted" but here https://t.me/thedevs_c/246534 you also said deleting instance results in deleting output... So .. ? I'm a bit confused 😬 Will output be deleted by deleting instance?

// this is never deleted A *instance = new A; A *output = new A; instance = output; // output is deleted delete instance;

Thomas
// this is never deleted A *instance = new A; A *...

instance = output; overwrites the original address of instance, and then the original can never be deleted, because you lost the address to it

Sahand-🏔️ Автор вопроса
Thomas
instance = output; overwrites the original address...

Oh now I understand A* instance = new A; A* ptr = instance; instance = instance->func(); /* some code */ // GC delete instance; instance = ptr; delete instance; This one also works right ?

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

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

А кто-то пробовал, уезжая из Эстонии получить э-рез и продолжить вести предпринимательскую деятельность внутри Эстонии, используя свой OÜ?
Lalalashechki Lalala
62
Если у меня есть такой класс: Object = {} function Object:new(a_name, a_transform, a_color, a_mesh, a_material, a_shader, a_textures) local private = {} private.n...
Cuarno Vile
4
I'm new at Monero and I'd like to ask you all - why are you in Monero? I know about privacy but what do you expect to happen in the next years, decades that Monero will make i...
Konrad
18
Is Kadena onto something special? No one really knows. Good luck!
Bright Uncle Stephen EqualBet
40
я не магистр хаскеля, но разве не может лейзи тип конвертнуться в не-лейзи запросив вычисление содержимого прям при инициализации?
deadgnom32 λ madao
100
At which price point will BCH, an ASIC mined coin, be succeptible to reorg attacks because of low hashrate? $10? $1? $0.1? $0.01?
Kishniev
18
Гайз, кто-нибудь пробовал запустить probe-rs под камень, которого нет в probe-rs? Мб есть какой-нибудь пример у кого... Через target-gen попробовал сгенерировать chip-descript...
Максим Смирнов
2
зачем же переименовывать ? чтобы кол-во участников возросло или вдруг IBM от этого снова на свифте начнет кодить ? Я не понимаю что страшного в том что свифт гавно, если это т...
Oleh Nerzh
10
2 years ago I bought akt on Bittrex, at that time it gave a withdrawal option to the polygon network. I have read in this chat and they say that akt is not in polygon but if ...
TW
13
здравствуйте. совершаю вот такую вещь: strcpy(line, (char)current_number); где current number — неподписанный шорт, line — массив чаров. ругань следующая: main.c:29:30: error...
Roberto's Ширгозиев
13
Карта сайта