Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

> I usually allocate and free in the same function unless the function is meant to allocate for the caller. (And then that call is considered allocation in the caller.)

Yeah. I do a similar thing in C, but think of it as 'differing level of scopes in the abstraction'[1].

It's pretty easy to visually spot when a scope acquires a resource in $SCOPE::foo() and never releases that resource in the $SCOPE::cleanup().

[1] Like the way code has block scope, function scope, file scope and global scope, a problem domain or an abstraction over the solution (the model) also has varying levels of scope. I've never found this to be a taught thing, however. Modeling the problem domain and any proposed solution is a useful skill before jumping into coding a solution.



Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: