Live data from Hacker News

Stupid Smart Pointers in C

blog.kevinalbs.com

1–10 of 174 posts

Re: Stupid Smart Pointers in C

#3
Note that this will probably cause branch prediction misses, just like thread switching does - modern CPUs have a return address predictor which is just a simple stack. I don’t think you can avoid this without compiler support.

Re: Stupid Smart Pointers in C

#5
Really, don't do this, it's a portability and safety nightmare (aside from C not being memory safe already).

C programmers are better off with either of these two techniques:

* Use __attribute__((cleanup)). It's available in GCC and Clang, and we hope will be added to the C spec one day. This is widely used by open source software, eg. in systemd.

* Use a pool allocator like Samba's talloc (https://talloc.samba.org/talloc/doc/html/libtalloc__tutorial...) or Apache's APR.

(I didn't include using reference counting, since although that is also widely used, I've seen it cause so many bugs, plus it interacts badly with how modern CPUs work.)

Re: Stupid Smart Pointers in C

#9
post #5

Really, don't do this, it's a portability and safety nightmare (aside from C not being memory safe already). C programmers are better off with either of these two techniques: * Use __attribute__((cleanup)). It's available in GCC and Clang, and we hope will be added to the C spec one day. This is widely used by open source software, eg. in systemd. * Use a pool allocator like Samba's talloc ( https://talloc.samba.org/…

> __attribute__((cleanup))

Interesting. I'm not very proficient in C, this looks like some sort of finalizers for local variables?

Post reply on HN