A bug Caused by Implicit Construction of std::string

Recently I met a bug when I was using google protobuf to serialize and deserialize messages.I have a code segment like this:

In the code, there is a weird usage of std::string::data() and MyMessage::ParseFromString() API. It’s a mistake due

Be careful with return-value optimization of C++ compilers

Side Effect of Return-value Optimization This “bug” is caused by return-value optimization and affects both GCC and Microsoft C/C++ Compiler. Assume that we have codes like following:

Compile the code with  g++ main.cpp -O0 -g -std=c++11. Run the program and the output result is

Python 3使用Basemap导入ESRI信息的问题

Basemap使用自带的shapefile.py处理ESRI的shapefile,但是Basemap自带的shapefile版本过低(1.1.x),其中存在一个bug导致使用Python 3.x版本时无法正确处理Windows-1252编码的二进制文件。典型错误:

解决方法一: 使用pip安装pyshp ( pip install pyshp ),或者手动下载最新版本的pyshp,并且将shapefile.py拷贝到Basemap的目录中( <python-install-prefix>\lib\site-packages\mpl_toolkits\basemap )。 解决方法二: 按照一的方式使用pip安装pyshp,并且修改Basemap的源代码,将__init__.py中shapefile模块的import语句略作修改,不再使用相对路径的导入方式并删除Basemap目录中的shapefile.py。修改之前为

修改之后为

使用方法二的一个好处是,使用pip更新pyshp后不需要再更新Basemap中的shapefile.py

Matlab求解变参数常微分方程组

以下代码在Matlab R2010a运行通过。 待求解函数示例:

k为可变参数。 求解主程序:

如上所示,在调用ODE solver时,格式为 solver(odefun, [t0 tfinal], y0, options, args...) 其中 args... 为可变参数,将被转发至待求解的方程中。 Matlab的自带帮助中并没有明确说明ODE solver支持这样的调用形式,不过这样的调用形式是符合Matlab函数定义的习惯的。上例中,ode45函数的最后一个参数(args…)为1,该参数被转发至待求解的方程中作为参数k。 上述求解过程使用了 deval() 来返回t=2时的结果,此函数的第一个实参是sol,是一个structure,要让ODE solver返回该类型,则ODE solver必须采用函数句柄的方式输入待求解的函数,若使用inline函数将无法返回该structure而仅返回积分区间。关于该structure的具体定义可查看Matlab帮助。

已知平衡数据利用Matlab作三元相图

需准备: 平衡线数据,连接线数据 1.作直角三角形相图

  2.作等边三角形相图