HN user

harpiee

10 karma
Posts0
Comments3
View on HN
No posts found.

Smart pointers for what?

I don't use any dynamically allocated memory in my firmware projects.

I do sometimes use statically allocated pools for things like packet buffers and allocate chunks out of them, but their lifetimes are not scope based, so automatic call of constructors/destructors would not be of any help.

I do it mainly as a form of namespacing and aid in readability.

  do_thing(foo); // foo is variable

  do_thing(FOO); // FOO is constant (i.e this call should always do the same thing)

  foo = FOO; // I wanna name a variable the same as a constant

With unsigned you can actually check for overflow yourself very easily

  z=x+y; if(z < x || z < y) // overflow
And bounds checks are just a single comparisons against an upper bound (handles both over and underflow)
  size = x + y;
  // or
  size = x - y

  if(size < bound) // good to go 
Prior to C23 (stdckdint.h) its very error prone to check for signed overflow since you have to rearrange equations to make sure no operation could ever possibly overflow.