
C&C++
CodeMasterShiller
一个老码农
展开
-
在结构体中定义长度为0的数组的用法
例如:struct varlendata{ int len; char data[0]; };sizeof(varlendata)的值是4,就是说data所占的空间为0因此,如果我们要存储len字节的数据,我们可以这样char* pbuf = new char[sizeof(varlendata)+len];varlendata* pdata = (varlendata*原创 2008-12-04 16:06:00 · 1170 阅读 · 0 评论 -
如何将彩色图片转换成灰白图片
<br /> <br />http://desktoppub.about.com/od/scanninggraphics/ss/color_to_bw_3.htm<br />Corel Photo-Paint: Image > Convert to... > Grayscale (8-bit) <br />Adobe Photoshop: Image > Mode > Grayscale <br />Adobe Photoshop Elements: Image > Mode > Grayscale (sa原创 2010-08-17 11:53:00 · 3447 阅读 · 0 评论 -
linux下安装opencv
installsudo apt-get install libcv-dev libcv-docfix bug under ubuntu 10.04: cannot find -lhighguisudo ln /usr/lib/libhighgui.so.4 /usr/lib/libhighgui.sobuildg++ -g -Wall -Wno-unused-function `pkg-config --cflags opencv` `pkg-config --libs opencv` opencv-t原创 2010-08-16 00:13:00 · 875 阅读 · 0 评论 -
Visual C++编程技巧
<br /><br />http://hi.baidu.com/9856226/blog/item/22988ad4aaa0c1cd50da4b20.html<br /> <br />调试dll、exe的时候,需要打log。写文件嘛太麻烦,printf只能在终端不是很灵活。这时候利用windows自带的OutputDebugString可以事半功倍,推荐大家试试。<br /> 跟其他函数一样,它也有两个版本:<br /> WINBASEAPI VOID WINAPI OutputDebugStr转载 2010-06-24 21:51:00 · 1531 阅读 · 0 评论 -
想成为嵌入式程序员应知道的0x10个基本问题
<br /><br />http://hi.baidu.com/9856226/blog/item/39a2a7d2544d90083af3cf8e.html<br /> <br />作者: hewok 出自: http://www.mcuresource.com<br />C语言测试是招聘嵌入式系统程序员过程中必须而且有效的方法。这些年,我既参加也组织了许多这种测试,在这过程中我意识到这些测试能为面试者和被面试者提供许多有用信息,此外,撇开面试的压力不谈,这种测试也是相当有趣的。<br /> 从被面试者转载 2010-06-24 21:28:00 · 513 阅读 · 0 评论 -
八皇后问题解法
<br />#include <stdio.h>#include <list>using std::list;int selcol(int* board, int dim, int row, int initcol){ for (int i=initcol; i<dim; ++i) { int j; for (j=0; j<row; ++j) { if (*(board+j*dim+i) != 0) break; if (i-(r原创 2010-07-30 13:54:00 · 781 阅读 · 1 评论 -
VS程序发布问题
<br />http://www.cnblogs.com/visoeclipse/archive/2010/02/27/1674866.html<br /> <br /> <br />http://hi.baidu.com/fairysky/blog/item/130dda13db7b050a5aaf53be.html<br />http://hi.baidu.com/fairysky/blog/item/e7a8366dbaa735f3431694c8.html<br />http://www.cppbl转载 2010-07-28 14:19:00 · 803 阅读 · 0 评论 -
VLD检测内存泄露原理及源码分析
<br /><br />包含vld.h的时候,使用pragma comment链接vldddlib(采用预编译指令,根据不同的工程链接不同的lib)<br /> <br />通过全局变量<br />VisualLeakDetector visualleakdetector;<br />的构造和析构来启动和终止内存跟踪<br /> <br />构造函数中使用_CrtSetAllocHook函数设置钩子函数捕获_HOOK_ALLOC、_HOOK_FREE、_HOOK_REALLOC等事件。<br />设置钩子函原创 2010-07-28 10:42:00 · 4810 阅读 · 0 评论 -
内存泄露检测
<br /><br />http://wyw.dcweb.cn/leakage.htm<br /> <br />A Cross-Platform Memory Leak Detector<br /> <br />Memory leakage has been a permanent annoyance for C/C++ programmers. Under MSVC, one useful feature of MFC is report memory leaks at the exit of an ap转载 2010-07-27 10:41:00 · 2646 阅读 · 0 评论 -
swf解码库libswfdec
<br /> <br />swf解码库<br />sudo apt-get install libswfdec-0.8-dev<br /> <br />example from http://swfdec.freedesktop.org/wiki/SwfdecExamples<br /> <br />//Created by : pepsidrinker@hotmail.com// Date : Sept. 15 2008#include <swfdec-gtk/swfdec-gtk.h>/原创 2010-08-18 11:28:00 · 3546 阅读 · 1 评论 -
指针的地址和数组的地址
<br />char a[100];<br />char *b = a;<br /> <br />printf("a:%p, &a:%p, b:%p, &b:%p/n", a, &a, b, &b);<br /> <br />打印的值,后两个肯定不同,前两个呢?<br />是一样的<br />原创 2011-05-25 23:21:00 · 951 阅读 · 0 评论 -
linux下fork和pthread混用的问题
<br />linux下编程处理并行任务时,可以采用多进程模块(fork)或者多线程模型(pthread)。有时候还会采用混合的模式。<br />当混合使用两者的时候,可能存在一些问题。<br /> <br />一种情况是:<br />进程创建了一些线程,然后执行了fork。代码编写者的意图可能是希望fork出很多个副本,执行同样的任务。可是fork之前创建的线程却并没有被复制,只有最初的进程拥有该线程。<br /> <br />另一种情况是:<br />进程创建了一些线程用于处理一些工作。然后希望转到后台原创 2010-11-10 22:37:00 · 5112 阅读 · 0 评论 -
linux下几个stat函数的区别
<br /><br />linux下几个stat函数的区别<br />lstat和stat的区别:<br />如果文件名指向的是一个软链,lstat会判断这个软链本身是否存在,而stat则会判断软链指向的文件是否存在<br />因此,使用ln -sf创建的软链,stat可能会失败,lstat则可以成功<br /> <br />fstat和stat的区别,fstat接受一个文件句柄为参数,而不是文件名原创 2010-11-10 14:08:00 · 1173 阅读 · 0 评论 -
tcmalloc介绍
<br />http://goog-perftools.sourceforge.net/doc/tcmalloc.html<br /> <br />google perftools中的一员<br /> <br />比glic的实现ptmalloc2快6倍,ptmalloc2一次malloc和free操作约要300ns,tcmalloc只需约50ns<br /> <br />比ptmalloc更高的空间利用率,ptmalloc每个对象约浪费8字节,tcmalloc只额外花费1%的空间<br /> <br />翻译 2010-11-06 01:32:00 · 1997 阅读 · 0 评论 -
C调用python
#include int main(){ Py_Initialize(); PyRun_SimpleString("print /"hello, python c api/""); Py_Finalize(); return 0;}gcc -o test test.c -lpython2.6./testref: http://docs.python.org/c-api/原创 2010-12-18 08:29:00 · 629 阅读 · 0 评论 -
gprof使用说明
<br />http://www.cs.utah.edu/dept/old/texinfo/as/gprof.htmlgcc使用 -pg 参数编译程序gcc -o test test.c -g -pg编译成功后运行程序,会在当前目录下生成gmon.out文件./test使用gprof和gmon.out运行程序gprof test gmon.out这时将输出程序的性能信息原创 2010-12-10 22:24:00 · 768 阅读 · 0 评论 -
tcmalloc使用说明
<br /><br />http://code.google.com/p/google-perftools/wiki/GooglePerformanceTools<br /> <br /> <br />TC Malloc:gcc [...]-ltcmalloc<br />Heap Checker:gcc [...]-o myprogram -ltcmalloc<br />HEAPCHECK=normal ./myprogram<br />Heap Profiler:gcc [...]-o myprogram转载 2010-12-10 22:19:00 · 1954 阅读 · 0 评论 -
c++ placement new
<br />from http://www.parashift.com/c++-faq-lite/dtors.html#faq-11.10<br /> <br />摘要<br />placement new的目的是把对象放在预先已分配好的指定内存里,并调用构造函数<br /> <br />使用者必须保证存放对象的地址是对齐的<br />使用者必须手动调用该对象的析构函数[11.10] What is "placement new" and why would I use it?<br />There are转载 2010-09-01 10:56:00 · 753 阅读 · 0 评论 -
C++ static 变量的初始化
C++全局static变量和class的static变量在main函数之前初始化,main函数之后销毁 函数内部的局部static变量在该函数第一次被调用时初始化,在main函数之后销毁 验证代码 //1.cpp#include using namespace std; struct Date{ Data(){cout ~Data(){c原创 2010-05-27 21:51:00 · 932 阅读 · 0 评论 -
一个c程序在执行main函数之前和main之后都做了那些事情啊
from http://club.topsage.com/archiver/tid-282994.html main函数之前--真正的函数执行入口或开始一种解释[quote]实际上,在可执行文件被加载之后,控制权立即交给由编译器插入的Start函数,它将对后面这些全局变量进行准备: _osver 操作系统的构件编号 _winmajor 操作系统的主版本号 _winm转载 2010-05-27 21:28:00 · 1172 阅读 · 0 评论 -
C++实现单例的几种方法
//1 class Singleton{ Singleton(){} static Singleton* m_spInstance; public: static Singleton* GetInstance() { if (!m_spInstance)原创 2009-11-15 12:21:00 · 1389 阅读 · 0 评论 -
几行C++代码
#include class Base{ long m_nRefCount; protected: virtual ~Base() { std::cout } Base():m_nRefCount(0) {原创 2009-11-05 22:45:00 · 1820 阅读 · 0 评论 -
这样提供程序的版本信息
struct VersionInfo{ int version_no; char* build_date;}; const g_MainVer = 1;const g_SubVer = 0;const VersionInfo g_VersionInfo ={ g_MainVer __DATE__ " " __TIME__原创 2009-10-23 22:10:00 · 552 阅读 · 0 评论 -
Cool Code list
from http://www.sjbaker.org/wiki/index.php?title=Cool_Code_listCool Code listFrom WikiidJump to: navigation, searchHereis my collection of cute C and C++ tricks - I have tried to转载 2009-10-13 21:14:00 · 536 阅读 · 0 评论 -
boost tribool example
/* * ===================================================================================== * * Filename: tribool.cpp * * Description: test boost tribool * * Version: 1原创 2009-10-01 19:55:00 · 1770 阅读 · 0 评论 -
使用宏模拟C++模版完成简单数据类型的交换
#define SWAP(x,y) do{typeof(x) tmp; tmp=x;x=y;y=tmp;}while(0)原创 2009-09-29 12:47:00 · 516 阅读 · 0 评论 -
字节对齐
C/C++中的字节对齐问题http://wzoot.blog.163.com/blog/static/416388002007111210305852/ C&C++ 2007-12-12 22:30 阅读288 评论1 字号: 大 中 小首先来看下在C/C++中定义如下的结构体,然后对他们分别进行sizeof()运算,看看结果会如何转载 2009-09-29 10:31:00 · 442 阅读 · 0 评论 -
如何按页分配内存
needSize为需要分配的内存大小,pageSize为内存分页大小allocSize为比needSize大的最小的pageSize的整数倍内存大小 allocSize = (needSize + pageSize - 1) & ~(pageSize-1);原创 2009-04-14 09:13:00 · 845 阅读 · 0 评论 -
如何重载类型操作符
如果希望将对象当成bool变量用于条件判断,可重载bool class BooleanObject{ int val; public: BooleanObject(int i):val(i){} operator bool() { return val != 0;原创 2009-11-15 12:14:00 · 497 阅读 · 0 评论 -
C++实现工厂模式的例子
#include using namespace std;class Base{ public: virtual void foo()=0; };class DerivedA : public Base{ public: void foo() { cout原创 2009-11-15 12:54:00 · 586 阅读 · 0 评论 -
要求或禁止在堆中产生对象
http://blog.chinaunix.net/u2/84425/showart_2037715.html1.1 要求或禁止在堆中产生对象有时你想这样管理某些对象,要让某种类型的对象能够自我销毁,也就是能够“delete this”。很明显这种管理方式需要此类型对象被分配在堆中。而其它一些时候你想获得一种保障:“不在堆中分配对象,从而保转载 2010-04-08 21:26:00 · 571 阅读 · 0 评论 -
GCC对可变参数的宏的支持
匿名可变参数:#define TRACE(fmt, ...) fprintf(stderr, fmt, ##__VA_ARGS__)带名称的可变参数:#define TRACE(fmt, args...) fprintf(stderr, fmt, ##args)“##”会将可变参数列表连接在固定参数的后面,如果可变参数列表为空,"##"会将前面多余的“,”去掉原创 2010-04-07 18:52:00 · 1018 阅读 · 0 评论 -
bcp: 给boost瘦身
boost是一个非常优秀的库。问及多数C++程序员为什么最终没有选择Boost的原因,均回答:Boost太大,过于Heavy。是的。这也是我多数在用与不用Boost之间徘徊的原因。现在给大家介绍的这个bcp也许可以部分消除这样的不良影响,让我们有机会得以使用Boost这样优秀的库。bcp的主页:http://www.boost.org/doc/libs/1_35_0/tools/bcp转载 2010-04-15 08:34:00 · 1067 阅读 · 0 评论 -
MiniCppUnit分析
MiniCppUnit是一个轻量级的CppUnit,可以用来做C++代码的单元测试。 MiniCppUnit一种两个核心文件:MiniCppUnit.hxx和MiniCppUnit.cxx 在其压缩包内,还有一个TestRunner.cxx文件,这里面有一个简单的main函数,如果用户要测试的代码片段没有主函数,则可以使用此文件。此文件就一行代码: return TestF原创 2010-04-14 20:01:00 · 785 阅读 · 0 评论 -
几个CPP单元测试库
boost unit testhttp://www.boost.org/doc/libs/1_42_0/libs/test/doc/html/index.html CppUnithttp://sourceforge.net/projects/cppunit/ MiniCppUnithttp://www.dtic.upf.edu/~parumi/MiniCppUnit/原创 2010-04-14 08:55:00 · 749 阅读 · 0 评论 -
隐藏在自动类型转换后的错误
#include int main(){ char key = 0x80; int a = 0x12345678; printf("%0x/n", a|key); return 0; } 上面这段代码的输出是什么?是123456f8?不是是fffffff8可能结果不是写这段代码的人预料的了key在与a执行 | 运算之前,被拓展为int不是拓展为0x000000原创 2010-04-08 20:19:00 · 613 阅读 · 0 评论 -
关于C++拷贝构造函数
from http://groups.google.com/group/comp.lang.c++.moderated/browse_thread/thread/400259ead753c6ca?hl=en Chris Morley View profile More options Nov 20, 5:转载 2009-11-21 10:42:00 · 423 阅读 · 0 评论 -
c++0x的一些新特性
先看一个简单的例子对比 //old std::list l; std::list::iterator it = l.begin(); //new std::list l; auto it = l.begin(); 很酷吧?auto原来是生命自动变量(局部变量)的关键词,在c++0x中有了新的含义 c++0x是新原创 2009-11-18 21:40:00 · 573 阅读 · 0 评论 -
一个php session 实现的问题
<br />php可以通过session_set_save_handler函数设置回调,通过应用层来实现session数据的存储和管理。<br /> <br />使用此函数时,有一点需要注意,设置的钩子函数并不是一次设置,一直有效。在请求结束或者调用session_destroy时,保存的回调函数指针也将被清除,下次再使用时须重新设置。<br /> <br />不知为何原因php不将回调钩子函数的生命周期设计为跨请求的。原创 2011-04-15 22:34:00 · 798 阅读 · 0 评论