??xml version="1.0" encoding="utf-8" standalone="yes"?> 1、越? 在python中import this׃展示出The Zen of Python如下Q? 优美胜于丑陋QPython 以编写优的代码为目标) The Zen of Python, q段晦ӆ、复杂、凌q代码Q莫非是 Tim Peters 提供的反例?写给c/c++的新同行门,CTO门把你们的宝늻验拿出来吧,让这个帖对大伙有?/h5>
界是最难查的,注意memcpy strcpy,strncpyq些函数使用前一定要查边?
特别是你提供函数l别人用Ӟ你的函数中用Cq些东西Q一定要查别Zl你的指针的
边界
2、变量初始化
q种问题要养成好习惯Q否则出来偶然性问题,非常难查
3、多U程指针理
在多U程环境下用指针时Q最好采用引用计敎ͼ让最后一个放充引用计数时Q指针删除,避免一个线E在使用指针Q另外线E删除掉
4、多U程锁的理
多线E锁要粒度要适中Q尽量减 一个函数 q入多个锁,避免一个大函数一个大锁媄响性能Q可学习数据库的表Q行U锁
量不要在回调函CNQ易 引v死锁
做到U程安全函数单向调用Q上层往下层调用Q下屋向上层采用事g驱动反馈Q避免调用栈
q深Q易引v死锁
Q?
5、多U程对象生存期管?
量当多U程׃n对象 量不要直接删除Q徏议采用状态机形式来管理,其它U程讄状?
׃个线E统一按状态管理生存期
6、构造函?
函造函C不要放虚函数Q绝对不要在构造函C开U程Qƈ且线E调用自q成员
7、内?
虚函C要内?
8、多U程创徏办法
如果用Cq行库函敎ͼ要注意用Cq行库的Ҏ
9Q内存管?
采用内存池管?
10、线E生存期理
U程中尽量不要调用同步函敎ͼ不要杀U程Q要让线E不断@环,{待M信号自己退?/p>
int i=3;
p2=&i;
p1=&p2;
printf("Hello World!\n");
printf("%d\n",i); //3
printf("%d\n",**p1); //3
printf("%d\n",*p2); //3
printf("%d\n",&i); //1245044
printf("%d\n",*p1); //1245044
printf("%d\n",p2); //1245044
printf("%d\n",p1); //1245048
printf("%d\n",&p2); //1245048
printf("%d\n",&p1); //1245052
return 0;
]]>
The Zen of Python, by Tim Peters
Beautiful is better than ugly.
Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated.
Flat is better than nested.
Sparse is better than dense.
Readability counts.
Special cases aren't special enough to break the rules.
Although practicality beats purity.
Errors should never pass silently.
Unless explicitly silenced.
In the face of ambiguity, refuse the temptation to guess.
There should be one-- and preferably only one --obvious way to do it.
Although that way may not be obvious at first unless you're Dutch.
Now is better than never.
Although never is often better than *right* now.
If the implementation is hard to explain, it's a bad idea.
If the implementation is easy to explain, it may be a good idea.
Namespaces are one honking great idea -- let's do more of those!
从网上搜M一下,q被UCؓpython之禅Q以下是译
Python之禅
赖勇翻?/p>
明了胜于晦ӆQ优的代码应当是明了的Q命名规范,风格怼Q?
z胜于复杂(优美的代码应当是z的Q不要有复杂的内部实玎ͼ
复杂胜于凌ؕQ如果复杂不可避免,那代码间也不能有难懂的关p,要保持接口简z)
扁^胜于嵌套Q优的代码应当是扁q的Q不能有太多的嵌套)
间隔胜于紧凑Q优的代码有适当的间隔,不要奢望一行代码解决问题)
可读性很重要Q优的代码是可ȝQ?
即便假借特例的实用性之名,也不可违背这些规则(q些规则至高无上Q?
不要包容所有错误,除非你确定需要这样做Q精准地捕获异常Q不?except:pass 风格的代码)
当存在多U可能,不要试ȝ?
而是量找一U,最好是唯一一U明昄解决ҎQ如果不定Q就用穷举法Q?
虽然qƈ不容易,因ؓ你不?Python 之父Q这里的 Dutch 是指 Guido Q?
做也许好q不做,但不假思烦动手还不如不做Q动手之前要l思量Q?
如果你无法向人描qC的方案,那肯定不是一个好ҎQ反之亦ӞҎ评标准Q?
命名I间是一U绝妙的理念Q我们应当多加利用(倡导与号召)
蛇宗三字l?
作者:Tim Peters
译Q元?
Beautiful is better than ugly.
胜?
Explicit is better than implicit.
明胜?
Simple is better than complex.
胜复
Complex is better than complicated.
复胜?
Flat is better than nested.
胜?
Sparse is better than dense.
疏胜?
Readability counts.
辞达?
Special cases aren't special enough to break the rules.
不逄
Although practicality beats purity.
弃至?
Errors should never pass silently.
无阴?
Unless explicitly silenced.
有阳?
In the face of ambiguity, refuse the temptation to guess.
拒疑?
There should be one-- and preferably only one --obvious way to do it.
求完一
Although that way may not be obvious at first unless you're Dutch.
虽不臻I向往?
Now is better than never.
敏于?
Although never is often better than *right* now.
戒莽?
If the implementation is hard to explain, it's a bad idea.
差难a
If the implementation is easy to explain, it may be a good idea.
好易?
Namespaces are one honking great idea -- let's do more of those!
每师出,多有?/p>
比较恶搞的是Q其?this 模块的代码完全违背了q些原则Qؓ了方便你查看它的代码Q我把它贴出来:
s = """Gur Mra bs Clguba, ol Gvz Crgref
Ornhgvshy vf orggre guna htyl.
Rkcyvpvg vf orggre guna vzcyvpvg.
Fvzcyr vf orggre guna pbzcyrk.
Pbzcyrk vf orggre guna pbzcyvpngrq.
Syng vf orggre guna arfgrq.
Fcnefr vf orggre guna qrafr.
Ernqnovyvgl pbhagf.
Fcrpvny pnfrf nera'g fcrpvny rabhtu gb oernx gur ehyrf.
Nygubhtu cenpgvpnyvgl orngf chevgl.
Reebef fubhyq arire cnff fvyragyl.
Hayrff rkcyvpvgyl fvyraprq.
Va gur snpr bs nzovthvgl, ershfr gur grzcgngvba gb thrff.
Gurer fubhyq or bar-- naq cersrenoyl bayl bar --boivbhf jnl gb qb vg.
Nygubhtu gung jnl znl abg or boivbhf ng svefg hayrff lbh'er Qhgpu.
Abj vf orggre guna arire.
Nygubhtu arire vf bsgra orggre guna *evtug* abj.
Vs gur vzcyrzragngvba vf uneq gb rkcynva, vg'f n onq vqrn.
Vs gur vzcyrzragngvba vf rnfl gb rkcynva, vg znl or n tbbq vqrn.
Anzrfcnprf ner bar ubaxvat terng vqrn -- yrg'f qb zber bs gubfr!"""
d = {}
for c in (65, 97):
for i in range(26):
d[chr(i+c)] = chr((i+13) % 26 + c)
print "".join([d.get(c, c) for c in s])
]]>
]]>
Windows Server 2003
WINVER>=0x0502
Windows XP
WINVER>=0x0501
Windows 2000
WINVER>=0x0500
Windows NT 4.0
WINVER>=0x0400
Windows Me
WINVER>=0x0500
Windows 98
WINVER>=0x0410
Windows 95
WINVER>=0x0400
]]>
/*HDC hdc;
hdc=::GetDC(m_hWnd);
MoveToEx(hdc,m_ptOrigin.x,m_ptOrigin.y,NULL);
LineTo(hdc,point.x,point.y);
::ReleaseDC(m_hWnd,hdc);*/
/*CDC *pDC=GetDC();
pDC->MoveTo(m_ptOrigin);
pDC->LineTo(point);
ReleaseDC(pDC);*/
//CClientDC dc(this);
/*CClientDC dc(GetParent());
dc.MoveTo(m_ptOrigin);
dc.LineTo(point);*/
//CWindowDC dc(this);
//CWindowDC dc(GetParent());
/*CWindowDC dc(GetDesktopWindow());
dc.MoveTo(m_ptOrigin);
dc.LineTo(point);*/
/*CPen pen(PS_DOT,1,RGB(0,255,0));
CClientDC dc(this);
CPen *pOldPen=dc.SelectObject(&pen);
dc.MoveTo(m_ptOrigin);
dc.LineTo(point);
dc.SelectObject(pOldPen);*/
// CBrush brush(RGB(255,0,0));
/*CBitmap bitmap;
bitmap.LoadBitmap(IDB_BITMAP1);
CBrush brush(&bitmap);*/
/*CClientDC dc(this);
//dc.FillRect(CRect(m_ptOrigin,point),&brush);
CBrush *pBrush=CBrush::FromHandle((HBRUSH)GetStockObject(NULL_BRUSH));
CBrush *pOldBrush=dc.SelectObject(pBrush);
dc.Rectangle(CRect(m_ptOrigin,point));
dc.SelectObject(pOldBrush);*/
m_bDraw=FALSE;
CView::OnLButtonUp(nFlags, point);
int main ()
{
char *p=strToBinary(233);
//cout<<p;
//cout<<strToHex(233);
cout<<transToGKB("?);
return 0;
}
char* transToGKB(char *t){
int res=0;
int intlen;
intlen=strlen(t);
if(intlen>1){
char *result=new char[5];
int i=0;
if(0>t[0]){
res=256+t[0];
}
char *p1=strToHex(res);
if(0>t[1]){
res=256+t[1];
}
char *p2=strToHex(res);
result=p1;
result[2]=p2[0];
result[3]=p2[1];
result[4]='\0';
return result;
}
else{
//if(t[0]>64)
char * p=new char[3];
p=strToHex(t[0]);
p[2]='\0';
return p;
}
}
//数字转ؓ二进Ӟ255以内的正敎ͼ
char* strToBinary(int i){
char *result=new char[9];
int n=1;
int m;
int c=0;
int j=8;
for(c=0;c<8;c++){
m=i%2;
j=j-1;
i=n=i/2;
if(n>=0){
if (m>0){
result[j]='1';
}else
{
result[j]='0';
}
}
}
result[8]='\0';
// cout<<result;
return result;
}
//数字转ؓ十六q制Q?55以内的正敎ͼ
char* strToHex(int i){
char *result=new char[3];
int n=1;
int m;
int c=0;
int j=2;
for(c=0;c<2;c++){
m=i%16;
j=j-1;
i=n=i/16;
if(n>=0){
if (m>0){
if (m==1){
result[j]='1';
}
else if (m==2){
result[j]='2';
}
else if (m==3){
result[j]='3';
}
else if (m==4){
result[j]='4';
}
else if (m==5){
result[j]='5';
}
else if (m==6){
result[j]='6';
}
else if (m==7){
result[j]='7';
}
else if (m==8){
result[j]='8';
}
else if (m==9){
result[j]='9';
}
else if (m==10){
result[j]='A';
}
else if (m==11){
result[j]='B';
}
else if (m==12){
result[j]='C';
}
else if (m==13){
result[j]='D';
}
else if (m==14){
result[j]='E';
}
else if (m==15){
result[j]='F';
}
}else
{
result[j]='0';
}
}
}
result[2]='\0';
return result;
}
#include <string>
void yihuo(char *t,int n, int m);
void myToBinary();
void transToGKB(char *t);
void myToHex();
int main(){
//cout<<"GOOD";
int i=122;
int j=233;
int m=j^i;
char t[128]={0xC9,0xCC,0xC9,0xCC,0xC9,0xCC,'\0',0xC9,0xCC,0xC9,0xCC};
yihuo(t,0, 2);
// transToGKB(t);
//cout<<m;
//yihuo(2, 3);
//myToHex();
// myToBinary();
cout<<endl;
return 0;
}
//q制之间的{?br /> // 字符串传?6q制
void myToHex(){
char c[] = "CC";
unsigned long tt= strtoul(c, NULL, 16);
cout<<strtol(c, NULL, 16);
}
// 字符串传?6q制
void myToBinary(){
char c[] = "10000000";
// unsigned long tt= strtoul(c, NULL, 2);
cout<<strtol(c, NULL, 2);
}
//汉字的{换, 16q制转换为汉?br />void transToGKB(char *t){
// char *t="商户";
// CharN
//如果是负敎ͼ则{为正整数
// if(0>t[0]){
// res=256+t[0];
// }
int j=t[0];
cout<<t<<endl;
}
void yihuo(char *t,int n, int m) {
char *tt="商户说的?;
//转成数字q且保存到数l,然后求异?/p>
//求的长度
const int mylen=strlen(tt)+1;
char mysplit[1000];
//循环对这个整形数l进行赋?br />int i=0;
for(i<0;i<mylen-1;i++){
if (tt[i]<0){
mysplit[i]=256+tt[i];
}else
{
mysplit[i]=tt[i];
}
}
int result=mysplit[n-1];
int j;
for(j=n;j<n+m;j++){
result=result^mysplit[n];
cout<<"L"<<endl;
cout<<mysplit[n];
cout<<"M"<<endl;
}
//q行遍历求异?br />mysplit[mylen-1]='\0';
cout<<mysplit;
cout<<"ee";
if(result<0)
result=256+result;
cout<<result;
//int j=t[0];
// cout<<t<<endl;
}
Text1.Text = PropBag.ReadProperty("RecordSource", _
m_def_recordSource)
Text2.Text = PropBag.ReadProperty _
("ConnectionString", m_def_connectionString)
End Sub
return 0;
}
#
include
<stdio.h>
int g1=0, g2=0, g3=0;
intmax(int i)
{
int m1=0,m2,m3=0,*p_max;
static n1_max=0,n2_max,n3_max=0;
p_max =(int*)malloc(10);
printf("打印maxE序地址\n");
printf("in max: 0x%08x\n\n",max);
printf("打印max传入参数地址\n");
printf("in max: 0x%08x\n\n",&i);
printf("打印max函数中静态变量地址\n");
printf("0x%08x\n",&n1_max);//打印各本地变量的内存地址
printf("0x%08x\n",&n2_max);
printf("0x%08x\n\n",&n3_max);
printf("打印max函数中局部变量地址\n");
printf("0x%08x\n",&m1);//打印各本地变量的内存地址
printf("0x%08x\n",&m2);
printf("0x%08x\n\n",&m3);
printf("打印max函数中malloc分配地址\n");
printf("0x%08x\n\n",p_max);//打印各本地变量的内存地址
if(i)return 1;
elsereturn 0;
}
int main(int argc,char**argv)
{
staticint s1=0, s2, s3=0;
int v1=0, v2, v3=0;
int*p;
p =(int*)malloc(10);
printf("打印各全局变量(已初始化)的内存地址\n");
printf("0x%08x\n",&g1);//打印各全局变量的内存地址
printf("0x%08x\n",&g2);
printf("0x%08x\n\n",&g3);
printf("======================\n");
printf("打印E序初始E序main地址\n");
printf("main: 0x%08x\n\n", main);
printf("打印d地址\n");
printf("argv: 0x%08x\n\n",argv);
printf("打印各静态变量的内存地址\n");
printf("0x%08x\n",&s1);//打印各静态变量的内存地址
printf("0x%08x\n",&s2);
printf("0x%08x\n\n",&s3);
printf("打印各局部变量的内存地址\n");
printf("0x%08x\n",&v1);//打印各本地变量的内存地址
printf("0x%08x\n",&v2);
printf("0x%08x\n\n",&v3);
printf("打印malloc分配的堆地址\n");
printf("malloc: 0x%08x\n\n",p);
printf("======================\n");
max(v1);
printf("======================\n");
printf("打印子函数v始地址\n");
printf("max: 0x%08x\n\n",max);
return 0;
}
q个E序可以大致查看整个E序在内存中的分配情?
可以看出,传入的参?局部变?都是在栈分?随着子函数的增多而向下增?
函数的调用地址(函数q行代码),全局变量,静态变量都是在分配内存的低部存?而malloc分配的堆则存在于q些内存之上,q向上生?/font>