by masklinn · 2y ago · 0 comments
Yes?
if cond {
drop(p)
}
// p may or may not be dropped here
or let p;
if cond {
p = something();
}
// p may or may not be set here
These trigger dynamic drop semantics, in which case the stackframe has a hidden set of drop flags going alongside any variable with dynamic drop semantics, to know if they do or don’t need to be dropped. The flags are automatically updated when the corresponding variables are set or moved-from. int * owner p = malloc(sizeof(int));
if (condition)
free(p);
free(p); // error p may be initialized/moved.
to fix int * owner p = malloc(sizeof(int));
if (condition)
{
free(p);
p = 0;
}
free(p);