Xinye Tao | Blog - Topic - Collection - Resume | RSS

Today I Learned

# Created: 2020-12-30; Modified: 2021-06-26

How to write script that can be safely edited on the run?

Method 1:

source actual-script.sh

Method 2:

{
  # actual script
  exit;
}

(StackOverflow)

Create highlight group in vim
# echo rule.vim
highlight Group ctermbg=green guibg=green
call matchadd("Group", "foo")
# vim text
:so rule.vim

(StackExchange)

Reclaim disk space (on a shared server)
  1. delete some files

  2. use sudo lsof +L1 to kill process referencing those files

  3. use tune2fs -m <root-reserved-percentage> <device> to grab more space from root

C supports Variable Length Array (VLA)
void initialize(size_t n, size_t m, double A[n][m]);
Forward Declaration

Pros: hide implementations (declare XXImpl), circular dependence, faster compilation

Cons: can’t use alias, potential inheritance is elided, delete an incomplete class is UB

Evaluation Interleaving

f(std::shared_ptr(new A()), g()) is risky if g() can be interleaved between object allocation and shared_ptr initialization.

This is explicitly prohibited after C++17 (StackOverflow)

Tiny details
  • _ variables are dropped immediately
  • Overflow check is disabled in release build
Avoid self-reference

I have a process table and I want to grep it for the phrase “banana”:

ps auxww | grep banana

root 87 Jun21 0:26.78 /System/Library/CoreServices/FruitProcessor --core=banana

mikec 456 450PM 0:00.00 grep banana

Argh! It also greps for the grep for banana! Annoying!

Well, I’m sure there’s pgrep or some clever thing, but my coworker showed me this and it took me a few minutes to realize how it works:

ps auxww | grep [b]anana

root 87 Jun21 0:26.78 /System/Library/CoreServices/FruitProcessor --core=banana

Doc Brown spoke to me: “You’re just not thinking fourth dimensionally!” Like Marty, I have a real problem with that. But don’t you see: [b]anana matches banana but it doesn’t match ‘grep [b]anana’ as a raw string. And so I get only the process I wanted!

(Hacker News)