浅谈CPP智能指针

智能指针其实并不是指针,而是一个特殊对象。
在智能指针对象生命期即将结束时,它会调用析构函数释放有它管理的堆内存。
访问智能指针管理对象的方法,使用操作符“->”(重载)。
访问智能指针本来的方法,使用操作符“.”。

我们常见的智能指针有以下几种:

C98
std::auto_ptr
第一代智能指针,有些操作比如“=”坑比较多,不推荐使用。

C11
std::unique_ptr
独占对象,并保证指针所指对象生命周期与其一致。

std::shared_ptr
可共享指针对象,可以赋值给shared_ptr或weak_ptr。
通过引用计数的方式控制生命周期,当指针所指对象的所有的shared_ptr生命周期结束时(引用计数为0时)会被销毁。

std::weak_ptr
可以指向shared_ptr,但并不影响引用计数。
不影像所指对象的生命周期,在引用所指对象时,先用需要lock()才能使用。

Boost
不共享对象,类似于std::unique_ptr
boost::scoped_ptr
boost::scoped_array

共享对象,类似于std::shared_ptr
boost::shared_ptr
boost::shared_array

共享对象,但不改变对象引用计数,类似于std::weak_ptr
boost::weak_ptr

侵入式引用计数,要求使用对象自己实现计数功能
boost::intrusive_ptr

下面给一个例子,说明一下std下的四种智能指针。
1、SmartPointerTest.cpp

#include <memory>
#include <iostream>
#include "MyTest.h"

using namespace std;

void test_auto_ptr()
{
	std::auto_ptr<MyTest> auto_ptr_01(new MyTest("tom", 20));

	if (auto_ptr_01.get())
	{
		auto_ptr_01->sayHello();
		auto_ptr_01.get()->_name = "jerry";
		auto_ptr_01->sayHello();
		(*auto_ptr_01)._age += 1;
		auto_ptr_01->sayHello();
	}
	
	//auto_ptr_02会抢占auto_ptr_01的对象
	//此后auto_ptr_01不指向MyTest对象
	std::auto_ptr<MyTest> auto_ptr_02 = auto_ptr_01;
	if (auto_ptr_01.get())
	{
		cout << "auto_ptr_01 is released" << endl;
	}
	auto_ptr_02->sayHello();

	//只是释放所有权,并不释放内存
	//MyTest* test = auto_ptr_02.release();

	//释放内存
	auto_ptr_02.reset();
	if (!auto_ptr_01.get())
	{
		cout <<"auto_ptr_02 is released"<< endl;
	}
	
}

void test_unique_ptr()
{
	//独占对象
	//保证指针所指对象生命周期与其一致
	unique_ptr<MyTest> unique_ptr_01(new MyTest("tom", 20));
	unique_ptr_01->sayHello();

	//不允许直接做右值
	//unique_ptr<int> unique_ptr_02 = unique_ptr_01;

	//需要通过move来处理
	unique_ptr<MyTest> unique_ptr_03 = move(unique_ptr_01);
	if (!unique_ptr_01)cout << "unique_ptr_01 is empty" << endl;
	unique_ptr_03->sayHello();

	//释放指针
	unique_ptr_03.reset();
	if (!unique_ptr_03)cout << "unique_ptr_03 is empty" << endl;
}

void test_shared_ptr()
{
	shared_ptr<MyTest> shared_ptr_01(make_shared<MyTest>("tom", 20));
	shared_ptr<MyTest> shared_ptr_02 = shared_ptr_01;
	shared_ptr_01->sayHello();
	shared_ptr_02->sayHello();

	shared_ptr_01.reset();
	if (!shared_ptr_01)cout << "shared_ptr_01 is empty" << endl;
	shared_ptr_02->sayHello();

	shared_ptr_02.reset();
	if (!shared_ptr_02)cout << "shared_ptr_02 is empty" << endl;
}

void test_weak_ptr()
{
	shared_ptr<MyTest> shared_ptr_01(make_shared<MyTest>("tom", 20));
	weak_ptr<MyTest> weak_ptr_01 = shared_ptr_01;
	shared_ptr_01->sayHello();
	weak_ptr_01.lock()->sayHello();

	weak_ptr_01.reset();
	if (!weak_ptr_01.lock())cout << "weak_ptr_01 is empty" << endl;
	shared_ptr_01->sayHello();
	
	weak_ptr<MyTest> weak_ptr_02 = shared_ptr_01;
	weak_ptr<MyTest> weak_ptr_03 = weak_ptr_02;
	if(weak_ptr_01.lock())weak_ptr_02.lock()->sayHello();
	shared_ptr_01.reset();
	if (!weak_ptr_01.lock())cout << "weak_ptr_02 is empty" << endl;
}

int _tmain(int argc, _TCHAR* argv[])
{
	test_auto_ptr();
	test_unique_ptr();
	test_shared_ptr();
	test_weak_ptr();

	return 0;
}

2、MyTest.h

#pragma once

#include <iostream>
#include <string>

using namespace std;

class MyTest
{
public:
	MyTest(string name, int age);
	~MyTest();
	void sayHello();

public:
	string _name;
	int _age;
};

3、MyTest.cpp

#include "stdafx.h"
#include "MyTest.h"

MyTest::MyTest(string name, int age)
{
	_name = name;
	_age = age;
}

MyTest::~MyTest()
{
}

void MyTest::sayHello()
{
	cout << "Hello " << _name<< "! You are "<< _age <<" years old." << endl;
}

PS:
聪明的你有没有发下,CPP的智能指针,与JVM内存中的四种引用方式,强引用、软引用、弱引用,虚引用,有很多相似的地方呢?

编译ss-qt5

1、安装依赖

sudo apt-get install qt5-default
sudo apt-get install cmake
sudo apt-get install libbotan-2-dev
sudo apt-get install libqrencode-dev
sudo apt-get install libzbar-dev

2、编译libQtShadowsocks

wget https://github.com/shadowsocks/libQtShadowsocks/archive/v2.1.0.tar.gz
tar -xvf libQtShadowsocks-2.1.0.tar.gz
cd libQtShadowsocks-2.1.0
make build
cd build
cmake .. -DCMAKE_INATALL_PREFIX=/usr -DUSE_BOTAN2=ON
make -j4
make
sudo make install

3、编译shadowsocks-qt5

git clone https://github.com/shadowsocks/shadowsocks-qt5.git
cd shadowsocks-qt5
make build
cd build
cmake .. -DCMAKE_INATALL_PREFIX=/usr
make -j4
make

4、运行

ss-qt5

SonarQube集成代码覆盖率

Java项目

#清理,并构建
mvn clean install javadoc:aggregate -Dadditionalparam=-Xdoclint:none surefire-report:report -Daggregate=true

#sonar收集信息
mvn sonar:sonar -Dsonar.host.url=http://127.0.0.1:9000 -Dsonar.scm.disabled=True -Dsonar.junit.reportPaths=target/surefire

.Net项目

#准备收集信息
MSBuild.SonarQube.Runner.exe begin /k:"MyProject" /n:"MyProject" /v:"1.0" /d:sonar.host.url=http://127.0.0.1:9000 /d:sonar.scm.disabled=True /d:sonar.cs.vstest.reportsPaths="%CD%\*.trx" /d:sonar.cs.vscoveragexml.reportsPaths="%CD%\MyProject.coverage.xml"

#清理并构建项目
msbuild MyProject /t:Clean /t:Rebuild

#进行单元测试并生成测试报告
"%VSINSTALLDIR%\Team Tools\Dynamic Code Coverage Tools\CodeCoverage.exe" collect /output:"%CD%\MyProject.coverage" /verbose "%VSINSTALLDIR%\Common7\IDE\CommonExtensions\Microsoft\TestWindow\vstest.console.exe" /Logger:trx "MyProject\bin\Debug\MyProject.dll"

#报告转换为xml格式
"%VSINSTALLDIR%\Team Tools\Dynamic Code Coverage Tools\CodeCoverage.exe" analyze /output:"%CD%\MyProject.coverage.xml" "%CD%\MyProject.coverage"

#结束分析
MSBuild.SonarQube.Runner.exe end

jekyll环境搭建

大家都知道github pages是用了jekyll,我们自己如何搭建一个网站呢?

#安装依赖环境
apt-get install ruby ruby-dev

#安装jekyll
gem install bundler jekyll

#新建网站
jekyll new mysite

#开启网站
cd mysite
bundle exec jekyll serve --host 0.0.0.0

#浏览网站
http://172.16.172.80:4000

Boss与Leader

Boss Leader
驱动员工 指导员工
制造恐惧 制造热情
面对错误,喜欢使用人事惩罚手段 面对错误,喜欢寻找解决问题的技术或管理方法
只知道怎么做 展示怎么做
用人 发展人
从团队收割成绩 给予团队成绩
喜欢命令和控制 喜欢沟通和写作
喜欢说,“给我上” 喜欢说,“跟我上”

转自http://coolshell.cn/。

Ubuntu18使用root登录

为了系统安全,ubuntu默认是禁用root登录的,更是禁用root登录gui。所以,以下的操作,仅用于开发、测试里很特殊的情况,用完后,建议关闭。

#1、为root创建密码,创建密码后就可以su,也可以在cui登录root账号
sudo passwd root

#2、设置gdm运行root登录
sudo vi /etc/gdm3/custom.conf
#开启AllowRoot一行
[security]
AllowRoot=true

#3、设置gdm允许root登录
sudo vi /etc/pam.d/gdm-password
# 将下面一行注释掉
# auth required pam_succeed_if.so user != root quiet_success

#4、编辑.profile文件
sudo vi /root/.profile
# executed by Bourne-compatible login shells.
if [ "$BASH" ]; then
if [ -f ~/.bashrc ]; then
. ~/.bashrc
fi
fi

# 最后一行替换成这个
if `tty -s`; then
mesg n
fi

#5、重启
sudo reboot

Ubuntu18设置静态IP

1、编辑配置文件

#打开配置文件
vi /etc/netplan/xxx.ymal

#其中enp0s3为固定ip
#其中enp0s8为dhcp
network:
    ethernets:
        enp0s3:
            addresses: [172.16.172.100/24]
            dhcp4: false
            gateway4: 172.16.172.2
            dhcp6: false
            optional: true
        enp0s8:
            addresses: []
            dhcp4: true
            optional: true
    version: 2

2、启用配置

netplan apply 

MacOS编译ipfs

1、安装go等必须的软件

brew install go

2、设置gopaht环境变量

export gopath=PAHT_TO_GOPATH

#然后在gopath下创建下面的目录
PAHT_TO_GOPATH/src/github.com/ipfs/

#设置必要的代理信息
export http_proxy=xxxx:xx
export https_proxy=xxxx:xx
export no_proxy="localhost, 127.0.0.1, 192.168.99.100, 根据你自己的实际情况进行处理"

3、下载源码

cd PAHT_TO_GOPATH/src/github.com/ipfs/
git clone https://github.com/ipfs/go-ipfs.git

4、编译go-ipfs

cd go-ipfs
make

会发现,gx无法获取所需的文件,原来是gx不会默认读取代理配置

5、下载gx代码

#然后在gopath下创建下面的目录
PAHT_TO_GOPATH/src/github.com/whyrusleeping/
cd PAHT_TO_GOPATH/src/github.com/whyrusleeping/
git clone https://github.com/whyrusleeping/gx.git
git clone https://github.com/whyrusleeping/gx-go.git

cd PAHT_TO_GOPATH/src/github.com/ipfs/
git clone https://github.com/ipfs/go-ipfs-api.git

6、修改go-ipfs-api代码,在文件shell.go中,增加一行代码

func NewShell(url string) *Shell {
        c := &gohttp.Client{
                Transport: &gohttp.Transport{
                        Proxy: gohttp.ProxyFromEnvironment, //请增加这一行
                        DisableKeepAlives: true,
                },
        }

        return NewShellWithClient(url, c)
}

7、编译gx及gx-go

cd PAHT_TO_GOPATH/src/github.com/whyrusleeping/gx
make
cd PAHT_TO_GOPATH/src/github.com/whyrusleeping/gx-go
make

8、用编译好的go及go-gx替换PAHT_TO_GOPATH/src/github.com/ipfs/go-ipfs/bin目录下对应的文件

9、编译ipfs

cd PAHT_TO_GOPATH/src/github.com/ipfs/go-ipfs
make

MacOS编译ethereum

1、安装go等必须的软件

brew install go

2、设置gopaht环境变量

export gopath=PAHT_TO_GOPATH

#然后在gopath下创建下面的目录
PAHT_TO_GOPATH/src/github.com/ethereum/

#设置必要的代理信息
export http_proxy=xxxx:xx
export https_proxy=xxxx:xx
export no_proxy="localhost, 127.0.0.1, 192.168.99.100, 根据你自己的实际情况进行处理"

3、下载源码

cd PAHT_TO_GOPATH/src/github.com/ethereum/
git clone https://github.com/ethereum/go-ethereum.git

4、编译

cd go-ethereum
make geth