Is it possible to avoid call to destructor for structs?
Haridas
haridas at systemc.net
Sun Sep 24 18:15:51 UTC 2017
Thanks Adam
Actually Bar's API and implementation is not in my control.
Consider the scenario where it is implemented in a library. Or a
scenario where I have millions of instances of Bar (not
necessarily as a component of Foo) and I do not want to add to
runtime memory footprint.
Ok, consider the following code. Now I am (seemingly) avoiding a
call to Bar's destructor by way of using pointers. But I have
doubts if delete on void pointer would reclaim the memory that
has been allocated while still not calling the destructor?
// >>>>
import std.stdio;
struct Foo {
Bar* bar;
this(size_t l) {
bar = cast(Bar*) new Bar[l];
}
~this() {
writeln("~Foo");
void *tmp = cast(void*) bar;
// would this reclaim memory
// allocated to Bar*
delete(tmp);
}
}
struct Bar {
~this() {
writeln("~Bar");
}
}
void main() {
Foo foo = 4;
}
More information about the Digitalmars-d-learn
mailing list