function pointer print

#include 
#include 

void test()
{
}

int main()
{
  std::cout << test << std::endl;
  std::cout << &test << std::endl;
  std::cout << (void*)test << std::endl;
  printf("%p\n", test);
  printf("%llx\n", test);
  return 0;
}

output

1
1
0x4008b6
0x4008b6
4008b6

第三行對應 ostream & operator <<( ostream &, const void * ); 故輸出function address 但第一行與第二行出現1的原因是因為 cout 找不到直接對應的method 開始透過standard conversions找可以對應的method standard conversion可以參考c++03 standard 4. Standard conversions

找到了 boolean (function pointer被轉成boolean) 所以輸出1
參考:
https://stackoverflow.com/questions/2064692/how-to-print-function-pointers-with-cout

4.12 Boolean conversions [conv.bool]
An rvalue of arithmetic, enumeration, pointer, or pointer to member type can be converted to an rvalue of
type bool. A zero value, null pointer value, or null member pointer value is converted to false; any
other value is converted to true.

Posted in C++ Language | Leave a comment

git with specific ssh key

需要將github private repo分享時,可針對repo使用collaborator方式加入帳號,缺點是該帳號具有寫入權限,或是在global設定可存取的ssh key,但這樣的key具有整個帳號的存取權限

另一種方式是針對repo加入deploy key,可以設定是否有寫入權限,但缺點是一旦在某個repo設定了key,不能在其他repo加入。
因此透過新生成的key來指定哪些key可以存取repo,而非使用機器預設的ssh key(~/.ssh/id_rsa)便成了一個折衷方式。
這樣一來可以針對每個repo分別gen一組(or多組)唯讀權限的key,與可寫入的key,再分發給使用者操作。

一行產生ssh key在特定位置(此處設為當前目錄)

ssh-keygen -t rsa -b 4096 -f ./id_rsa -y -q -N ""

git指定 ssh key

參考:https://stackoverflow.com/questions/4565700/specify-private-ssh-key-to-use-when-executing-shell-command


ssh-agent bash -c 'ssh-add /somewhere/id_rsa; git clone git@github.com:user/project.git'
其他操作亦同
ssh-agent bash -c 'ssh-add /somewhere/id_rsa; git pull'

在github repo上設定deploy key id_rsa.pub (不是id_rsa) 即可存取

Posted in System Administration | Leave a comment

latex install

full installation of texlive

yum -y install texlive texlive-*.noarch

Posted in System Administration | Leave a comment

ssl key generation one liner

openssl req -subj ‘/CN=example.com/O=O/C=US’ -new -newkey rsa:2048 -days 365 -nodes -x509 -keyout server.key -out server.crt

Posted in System Administration | Leave a comment

add/sub without +, –

同事問到的一題,不用加減法來計算加減法,當然是使用bit operation來操作,這題的想法主要是處理進位問題。

這邊只用recursive來處理,將進位的結果再加回去。

但須注意recursive可能的深度,避免function call stack overflow。(但以32bit整數加法來說 最多呼叫32次)。 事實上此處的recursive是在最尾端(tail recursive),所以在有處理tail call elimination的語言或是compiler有處理tail call optimization則不需擔心深度問題

#include <iostream>

int add(int a, int b)
{
 int r = a ^ b;
 int c = (a & b) << 1;
 if(!c) return r;
 return add(r, c);
}

int sub(int a, int b)
{
 int r = a ^ b;
 int c = (r & b) << 1;
 if(!c) return r;
 return sub(r, c);
}

int main()
{
 std::cout << add(-2, 34) << "," << sub(-2, 34) << std::endl;
 return 0;
}
Posted in Brainstorming | Leave a comment

chromium note

https://github.com/henrypp/chromium/releases

Chromium各版本build,在自動化處理網頁連線時,不同版本的chrome是需要的,
因為越新的版本對一些html5的API限縮得越多(例如chrome 66後,AudioContext需要user gesture才能正常init),
一般配合selenium WebDriver 或 chrome dev protocol來進行操作
可參考 https://github.com/GoogleChrome/puppeteer

Posted in System Administration | Leave a comment

ffmpeg audio volume adjust

當多媒體檔案聲音大小需要調整時
可參考 https://trac.ffmpeg.org/wiki/AudioVolume

get stats

ffmpeg -i INPUT_FILE -filter:a volumedetect -f null NUL (NUL or /dev/null)

[Parsed_volumedetect_0 @ 000001be4a046040] n_samples: 26064896
[Parsed_volumedetect_0 @ 000001be4a046040] mean_volume: -32.1 dB
[Parsed_volumedetect_0 @ 000001be4a046040] max_volume: -5.6 dB

adjust volume

ffmpeg -i INPUT_FILE -af "volume=5.6dB" -c:v libx264 -crf 23 -c:a aac -q:a 100 OUTPUT_FILE

Posted in System Administration | Leave a comment

division & modulus

a / b, a % b

當b為0時, behavior undefined,這邊討論其中一個operand為負數的情況
C89
整數除法(有負數情況下 implementation defined)
-9/7 有可能是 -1 or -2 (rounded up or rounded down)
當然此時對應的 % 就會根據除法的結果而不同 (這邊討論整數的modulus,floating point可以參考fmod)
要符合 dividend = divisor * quotient + remainder
-9 = 7 * (-1) + (-2)
-9 = 7 * (-2) + 5

C99
6.5.5 Multiplicative operators
When integers are divided, the result of the / operator is the algebraic quotient with any
fractional part discarded

總是 truncated to zero
所以 -9/7 = -1, -9%7=-2

C++
在c++03標準中 5.6 Multiplicative operators提到
If both operands are nonnegative then the remainder is nonnegative;
if not, the sign of the remainder is implementation-defined
這邊講的remainder要是正還是負為implementation defined,也就是有可能是 -2 or 5 對應的quotient是 -1, 2

Posted in C Language, C++ Language | Leave a comment

版本號convention

Microsoft .NET Assembly

major.minor[.build[.revision]]

major, minor一般做法都差不多
使用API compatibility, feature來分別(major不同則可能破壞backward compatibility)

Build: A difference in build number represents a recompilation of the same source. Different build numbers might be used when the processor, platform, or compiler changes.

Revision: Assemblies with the same name, major, and minor version numbers but different revisions are intended to be fully interchangeable. A higher revision number might be used in a build that fixes a security hole in a previously released assembly.
參考: https://msdn.microsoft.com/en-us/library/system.version.aspx

Semantic Versioning

MAJOR.MINOR.PATCH

這個在open source版本號安排滿常見

細節參考: https://semver.org/

linux shared lib:

current.revision.age

The standard GNU version numbering scheme:

major.minor.revision

其他參考: https://en.wikipedia.org/wiki/Software_versioning

Posted in Tips | Leave a comment

windows git bash

with VC2015 support
先透過command line 執行

"C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\vcvarsall.bat" amd64

再進入git bash

"C:\Program Files\Git\bin\bash.exe"

BAT檔呼叫shell script

"c:\Program Files\Git\bin\bash.exe" --login -i -c "COMMAND"

需要注意的地方
一般command line需要傳 forward slash參數時 eg. /help
需要加上 forward slash來escape 例如 //help

參考: https://stackoverflow.com/questions/34647591/passing-windows-slash-based-parameters-to-a-program-from-bash-script

Posted in Tips | Leave a comment