linux生成临时文件方法

工具命令 mktemp 命令 mktemp 默认在/tmp/目录下生成tmp.xxxxxxxxx文件 trap 命令 trap命令用于指定在接收到信号后将要采取的动作 trap command/function signal 示例 #!/bin/sh # clear while this script exits. trap 'rm -f "$TMPFILE"' EXIT TMPFILE=$(mktemp -p `pwd`) || exit 1 echo "temp file is $TMPFILE" ……

阅读全文

左值和右值

本文整理自网文,如有冒犯,请告知删除。 左值(lvalue)和右值(rvalue) 左值(lvalue)和右值(rvalue)是c/c++中的基础概念。 简单理解为:有名字的是左值,没有名字的是右值。 C C语言中,左、右值均可为变量或表达式 C语言中,左值可出现在=号左边,也可出现在=号右边 C语言中,右值只能出现在=号右边 int x, y; x = 1; y = 2; x = y; y = x; 2 = x; //err x,y为左值,2为右值 CPP CPP语言中,一个表达式会产生一个左值,或者一个右值,前者称为左值表达式,后者称为右值表达式。 CPP语言中,对于基本类型,左值和右值概念与C语言相同。 CPP语言中,对于自定义类型,右值允许通过它的成员函数进行修改。 class A { public: A(int i) : _i(i) {} A& operator=(const A& other) { _i = other._i; return *this; } void set(int i) { _i = i; } int get() { return _i; } private: int _i; }; int main() { A a(5); auto f = [&] () ->A { return a; } f().……

阅读全文

lambda函数

格式 [捕捉列表] (参数) mutable -> 返回值类型 {函数体} lambda 表达式是一种匿名函数,即没有函数名的函数;该匿名函数是由数学中的λ演算而来的。 种类 []{} 最简单的lambda函数 [var] 表示以值传递方式捕捉变量var int main() { int x = 5; auto f = [x]{ //x is read-only int y; y = x + 1; std::cout << y << std::endl; }; f(); return 0; } $>> 6 [=] 表示值传递捕捉所有父作用域变量 int main() { int x = 5; int y = 6; auto f = [=]{ //x,y is read-only int z; z = x + y; std::cout << z << std::endl; }; f(); return 0; } $>> 11 [&var] 表示以引用传递方式捕捉变量var int main() { int x = 5; auto f = [&x]{ x++; }; f(); std::cout << x << std::endl; return 0; } $>> 6 [&] 表示引用传递捕捉所有父作用域变量 int main() { int x = 5; int y = 0; auto f = [&]{ x++; y++; }; f(); std::cout << x << std::endl; std::cout << y << std::endl; return 0; } $>> 6 $>> 1 [this] 表示值传递方式捕捉当前的this指针 class A { public: void foo() { auto f = [this] { bar(); }; f(); } void bar() { std::cout << "bar" << std::endl; } }; int main() { A a; a.……

阅读全文

c++11的weak_ptr

why? class A { public: A() { std::cout << "A ctor" << std::endl;} ~A() { std::cout << "A dtor" << std::endl; } std::shared_ptr<B> spB; }; class B { public: B() { std::cout << "B ctor" << std::endl;} ~B() { std::cout << "B dtor" << std::endl; } std::shared_ptr<A> spA; }; int main() { std::shared_ptr<A> spA = std::make_shared<A>(); std::shared_ptr<B> spB = std::make_shared<B>(); spA->spB_ = spB; spB->spA_ = spA; return 0; } output: A ctor B ctor 从以上结果可以看出,A、B均未释放,造成内存泄漏。 根本原因在于,A、B之间的循环引用。 how? weak_ptr用来与shared_ptr共同使用,避免内存泄漏。 class A { public: A() { std::cout << "A ctor" << std::endl;} ~A() { std::cout << "A dtor" << std::endl; } void foo() { std::cout << "I am foo" << std::endl; } std::weak_ptr<B> wpB_; }; class B { public: B() { std::cout << "B ctor" << std::endl;} ~B() { std::cout << "B dtor" << std::endl; } std::weak_ptr<A> wpA_; }; int main() { std::shared_ptr<A> spA = std::make_shared<A>(); std::shared_ptr<B> spB = std::make_shared<B>(); spA->wpB_ = spB; spB->wpA_ = spA; spB->wpA_.……

阅读全文

Golang 程序结构

关键字 go中含有25个关键字 break default func interface select case defer go map struct chan else goto package switch const fallthrough if range type continue for import return var 此外,还有大约30多个预定义的名字 内建常量: true false iota nil 内建类型: int int8 int16 int32 int64 uint uint8 uint16 uint32 uint64 uintptr float32 float64 complex128 complex64 bool byte rune string error 内建函数: make len cap new append copy close delete complex real imag panic recover 名字大小写 大写的名字表明是可导出的,即可被其它包访问,如fmt包中的Println 小写的名字只能在包内使用 名字长度 一般名字的长度是没有限制的。 Go提倡短小的名称,如表示索引,定义为i,而不是index等 当作用域范围较大或生命周期较长的变量,采用较长的名字可能会更好。 声明 主要有4种类型的声明语句: var(变量) var 变量名字 类型 = 表达式……

阅读全文