$ find -name '*.c' -type f -exec grep printf '{}' +254 karma · joined April 10, 2009
$ find -name '*.c' -type f -exec grep printf '{}' +http://0pointer.de/blog/projects/instances.html http://lists.freedesktop.org/archives/systemd-devel/2011-Sep...
void * talloc_steal(const void * new_ctx, const void * ptr);
The talloc_steal() function changes the parent context of a talloc
pointer. It is typically used when the context that the pointer is
currently a child of is going to be freed and you wish to keep the
memory for a longer time.
NOTE: It is possible to produce loops in the parent/child relationship
if you are not careful with talloc_steal(). No guarantees are provided
as to your sanity or the safety of your data if you do this.
There's also talloc_reparent where you explicitly choose the parent to change.You are correct in that you can just write constructors and destructors, but then you still need to worry about error handling, double-frees and whatnot. With an allocator like talloc, you just state the relationships during the allocation, and afterwards only worry about the high-level structure.
One thing which can be somewhat cumbersome is the need to pass around those "contexts"; for example, if you allocate an array of some struct, and you need to allocate additional memory inside that struct, your context will presumably be the array, and so you need to have access to it. One solution I have found which is helpful in most, but not all, situations is to use the wonderful container_of[1] macro, which enables you to do upcasting using some pointer trickery. And of course, it does cost some performance as they say on their homepage.
In fact, the abstractions you mention are what might be confusing for the beginner: Classes, objects, inheritance, polymorphism, iterators, high-order functions, closures, monads, generators, etc. all require as much book- and code-staring as either void or function pointers do for the first time.
I do however agree with you that C is harder and much less useful to learn than a "batteries-included", modern language like Python or Ruby as a first language.
It seems to make things more reasonable. After all, Python has showed that saying "that just how it works" can be avoided, as in the case of / defaulting to floating-point division.