在官方的文档中,http://www.cplusplus.com/refer…,例程的 strncpy 的用法是:
/* strncpy example */
#include <stdio.h>
#include <string.h>
int main ()
{
char str1[]= "To be or not to be";
char str2[40];
char str3[40];
/* copy to sized buffer (overflo…… 阅读全文
Author Archives: ZRJ
GCC “-fomit-frame-pointer”编译选项的含义
在 makefile 中看到这个编译选项,不太理解于是查了一下,相关的东西还不多,抄一下放在这里
首先这个文章,http://blog.csdn.net/byzs/arti…,讲的不错,挺清晰的:
优化你的软件时,发觉”-fomit-frame-pointer”这个选项还是蛮有用的。
GCC手册上面这么说:
Don’t keep the frame pointer in a…… 阅读全文
修改函数的返回地址
来看这个代码
#include <stdio.h>
void fun2() {
printf("haha\n");
}
void fun1(int a) {
int* ret_addr = (&a)-1;
*ret_addr = (int)fun2;
}
int main() {
int a = 3;
fun1(a);
return 0;
}
他跑起来之后的输出是这样的:
zrj@vm:~/tmp$ ./a.out…… 阅读全文
使用 gperftools 分析 cpp 程序性能
首先从这里,https://code.google.com/p/gper…,下载 gperftools-2.1.tar.gz
接下来的操作教程主要参看这篇文章,http://www.searchtb.com/2012/1…,Google CPU Profiler使用指南及小工具,已经写的很好了,其中需要注意的问题有几个:
原文的 Makefile 没有排版,需要自己去 tab 缩进,不然在 make 的时候…… 阅读全文
位运算的一个坑
来看这段代码
#include <stdio.h>
int main() {
int a = 1, b = 0;
if (a|b == 0) {
printf("1\n");
} else {
printf("2\n");
}
return 0;
}
这段代码想表达的意思是,如果两个 int 都为 0,那么走 if 分支,不然的话,走 else 分支,嗯,…… 阅读全文
STL map 的一个问题
先看看这个代码
#include <iostream>
#include <map>
#include <vector>
using namespace std;
int main() {
map<int, vector<int> > map_a, map_b;
map_a[0].push_back(0);
map_b[0] = map_a[0];
map_b[0].push_back(0);
cout<<map_a[0].size(…… 阅读全文
Python 的一个 Bug
今天要写一个统计脚本,于是打开 Aptana,建了一个文件叫 stat.py,取的 statistics 的缩写,但是一直报错如下
Traceback (most recent call last):
File "C:\Python27\lib\site.py", line 62, in <module>
import os
File "C:\Python27\lib\os.py", line 63, in <module>…… 阅读全文
WordCount – Hadoop 的 HelloWorld
按照官方教程,http://hadoop.apache.org/docs/…,开始着手写一个 Hadoop 的 HelloWorld 程序,叫做 WordCount
首先准备好代码如下:
package org.myorg;
import java.io.IOException;
import java.util.*;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.conf.*;
import org.apache.ha…… 阅读全文
Hadoop 1.1.2 安装(集群版)
首先还是安装必须的 jdk,看到这里,http://os.51cto.com/art/201003…
终端下进入你存放jdk-6u12-linux-i586.bin,例如我的位置是:/home/liangshihong
$ sudo -s ./jdk-6u12-linux-i586.bin
一路回车,直到询问是否安装,输入yes回车
ok,安装完毕,下面配置环境变量
配置classpath,修改所有用户的环境变量
$ sud…… 阅读全文
Hadoop 1.1.2 安装(单机版)
操作的环境是阿里云的服务器,http://www.aliyun.com/,还不错,按时间收费,一个小时 0.28 块钱,操作系统是 Ubuntu 10.10 64 位,单核 CPU,内存 512
首先上来先修改主机名
hostname cluster-1
然后开始按照这里,http://hadoop.apache.org/docs/…,的教程安装 Hadoop 1.1.2
首先看到 Prerequisites,需要 j…… 阅读全文