??xml version="1.0" encoding="utf-8" standalone="yes"?>一区二区三区www,韩国精品一区二区三区,怡红院精品视频在线观看极品http://www.shnenglu.com/franksunny/archive/2010/04/22/113265.htmlfrank.sunnyfrank.sunnyThu, 22 Apr 2010 11:13:00 GMThttp://www.shnenglu.com/franksunny/archive/2010/04/22/113265.htmlhttp://www.shnenglu.com/franksunny/comments/113265.htmlhttp://www.shnenglu.com/franksunny/archive/2010/04/22/113265.html#Feedback0http://www.shnenglu.com/franksunny/comments/commentRss/113265.htmlhttp://www.shnenglu.com/franksunny/services/trackbacks/113265.html 

关于Symbian识别Ud、联通和其它q营商网l的Ҏ(gu)

 

自从Symbian OS?/span>EKA2提供?jin)强大?/span>CTelephonyQ这之后很多跟网l相关的参数都通过q个获取Q像识别目前手机是注册的是移动还是联通等信息|络上传的比较多的也是通过该方法,特别是啸天兄?/span>nokia论坛分n?jin)代码之后,|络上大多采用其代码Q当然也包括我这L(fng)懒h在内Q而且往往很多人都没有注意最关键的原理即“国际Ud用户识别码(IMSIQ?/span>International Mobile Subscriber Identification NumberQ是区别Ud用户的标志,储存?/span>SIM卡中Q可用于区别Ud用户的有效信息?/span>IMSI?/span>MCC?/span>MNC?/span>MSINl成Q其?/span>MCC为移动国家号码,?/span>3位数字组成,唯一地识别移动客h属的国家Q我国ؓ(f)460Q?/span>MNC为网l?/span>idQ由2位数字组成,用于识别Ud客户所归属的移动网l,中国Ud?/span>00Q中国联通ؓ(f)01Q?/span>MSIN为移动客戯别码Q采用等?/span>11位数字构?#8221;。具体详?/span>http://wiki.forum.nokia.com/index.php/%E5%8C%BA%E5%88%86%E5%BD%93%E5%89%8D%E7%94%A8%E6%88%B7SIM%E5%8D%A1%E6%98%AF%E7%A7%BB%E5%8A%A8%E8%BF%98%E6%98%AF%E8%81%94%E9%80%9A

最q在使用该代码时Q发现假如当手机处于ȝ状态下Q则不论有无?/span>SIM卡,使用啸天兄的Ҏ(gu)Q就识别不出来了(jin)Q这个应该跟CTelephony的实现有养I本h试着通过其源码去?jin)解了(jin),但是貌似跟踪到底层没有完全公开Q或者说个h看源码能力还太弱?jin)些。由于采用啸天兄Ҏ(gu)实现不了(jin)?jin),所以只能从上面的红头文Ӟ即红体字Q寻找解x(chng)法,虽然CTelephony::GetCurrentNetworkInfo在离U模式下失效Q但?/span>CTelephony::GetSubscriberId仍然可用Qؓ(f)此我们就可以通过直接分析IMSI来实现对q营商网l的识别Q至?/span>MNC的信息,大家可以查询http://en.wikipedia.org/wiki/Mobile_network_codeQ在国内的情况如下截图:(x)

既然知道?jin)如上信息,我们可以简单的?/span>IMSI可行分析了(jin)Q小可对啸天兄的代码q行单修改,当然该代码也是?/span>CTelephonyQ只能用在EKA2q_上,EKA1可以采用RMobilePhone::GetSubscriberId的方法来获取IMSIQ在q里也就不做展开?jin),具体代码如下Q?/span>

头文?/span>

/*

 * TelephonyAO.h

 *

 *  Created on: 2010-4-22

 *      Author: frank

 */

 

#ifndef TELEPHONYAO_H_

#define TELEPHONYAO_H_

 

#include <e32base.h>

#include <Etel3rdParty.h>

 

typedef enum

{

       ENetWorkUnKnow,

       ENetWorkCM,

       ENetWorkUN,

       ENetWorkTC,

       ENetWorkTT,

}TNetWorkType;

 

class CTelephonyAO : public CActive

{

public:

       static CTelephonyAO* NewL();

       TNetWorkType GetNetWorkId();

 

public:

       ~CTelephonyAO();

 

protected:

       void DoCancel();

       void RunL();

 

private:

       CTelephonyAO();

       void ConstructL();

 

       void GetNetWorkInfo();

 

private:

       CActiveSchedulerWait*                iActiveSchedulerWait;

       CTelephony*                        pTelephony_;

       CTelephony::TSubscriberIdV1             iSubscribId;

       CTelephony::TSubscriberIdV1Pckg     iSubscriberIdPckg;

};

 

#endif /* TELEPHONYAO_H_ */

 

实现文g

/*

 * TelephonyAO.cpp

 *

 *  Created on: 2010-4-22

 *      Author: frank

 */

 

#include "TelephonyAO.h"

 

CTelephonyAO::CTelephonyAO() : CActive(EPriorityStandard), iSubscriberIdPckg(iSubscribId)

{

      

}

 

CTelephonyAO::~CTelephonyAO()

{

       delete pTelephony_;

       pTelephony_ = NULL;

       delete iActiveSchedulerWait;

       iActiveSchedulerWait = NULL;

}

 

void CTelephonyAO::ConstructL()

{

       pTelephony_ = CTelephony::NewL();

       iActiveSchedulerWait = new (ELeave)CActiveSchedulerWait;

       CActiveScheduler::Add(this);

}

 

CTelephonyAO* CTelephonyAO::NewL()

{

       CTelephonyAO* pSelf = new(ELeave) CTelephonyAO;

       CleanupStack::PushL(pSelf);

       pSelf->ConstructL();

       CleanupStack::Pop();

       return pSelf;

}

 

void CTelephonyAO::RunL()

{

 

       if (iActiveSchedulerWait->IsStarted())

       {

              iActiveSchedulerWait->AsyncStop();

       }

}

 

void CTelephonyAO::DoCancel()

{

       pTelephony_->CancelAsync(CTelephony::EGetSubscriberIdCancel);

       if (iActiveSchedulerWait->IsStarted())

       {

              iActiveSchedulerWait->AsyncStop();

       }

}

 

void CTelephonyAO::GetNetWorkInfo()

{

       Cancel();

       pTelephony_->GetSubscriberId(iStatus, iSubscriberIdPckg);

       SetActive();

       iActiveSchedulerWait->Start();

}

 

TNetWorkType CTelephonyAO::GetNetWorkId()

{

       GetNetWorkInfo();

       TNetWorkType vNetWorkType = ENetWorkUnKnow;

       if(iSubscribId.iSubscriberId.Length() < 15)

       {

              vNetWorkType = ENetWorkUnKnow;

       }

       else

       {

              TBuf<6> vHeader;

              vHeader.Copy(iSubscribId.iSubscriberId.Left(5));

              TBuf<3> vPtrTemp;

              vPtrTemp.Copy(vHeader.Right(2));

              TInt vNetWorkId = 0;

              TLex vLex(vPtrTemp);

              vLex.Val(vNetWorkId);

              if((vNetWorkId == 1) ||(vNetWorkId == 6))

              {

                     vNetWorkType = ENetWorkUN;

              }

              else if((vNetWorkId == 0) ||(vNetWorkId == 2))

              {

                     vNetWorkType = ENetWorkCM;

              }

              else if((vNetWorkId == 3) ||(vNetWorkId == 5))

              {

                     vNetWorkType = ENetWorkTC;

              }

              else if(vNetWorkId == 20)

              {

                     vNetWorkType = ENetWorkTT;

              }

              else

              {

                     vNetWorkType = ENetWorkUnKnow;

              }

       }

       return vNetWorkType;

}

至于如何调用Q就可以通过如下单获取了(jin)Q不用再自己L较了(jin)?/span>

       CTelephonyAO* pTelephony = CTelephonyAO::NewL();

       CleanupStack::PushL(pTelephony);

       TNetWorkType vNetWorkType = pTelephony->GetNetWorkId();

       CleanupStack::PopAndDestroy(pTelephony);

好了(jin)Q暂时小l如下吧Q感谢啸天兄前h植树(wi)?/span>



frank.sunny 2010-04-22 19:13 发表评论
]]>
[转加整理]Symbian下用C++实现|页览的代?http://www.shnenglu.com/franksunny/archive/2009/08/12/93044.htmlfrank.sunnyfrank.sunnyWed, 12 Aug 2009 09:36:00 GMThttp://www.shnenglu.com/franksunny/archive/2009/08/12/93044.htmlhttp://www.shnenglu.com/franksunny/comments/93044.htmlhttp://www.shnenglu.com/franksunny/archive/2009/08/12/93044.html#Feedback0http://www.shnenglu.com/franksunny/comments/commentRss/93044.htmlhttp://www.shnenglu.com/franksunny/services/trackbacks/93044.html 先声明一下,下面的这D代码是调用pȝ的浏览器实现|页览的功能,很显然这是一U比较简单的Ҏ(gu)Q但是它的可控制性就不行?jin),例如左Y键的内容你是肯定该不?jin)的。如何写一个自q览器,而不调用pȝ的,{以后做出来再说吧?/span>

调用pȝ的浏览器来实现网|览可以根据系l浏览器的状态而决定调用的Ҏ(gu)Q例如当pȝ览器正在用的时候可以用TApaTask::SendMessage ()Ҏ(gu)Q当pȝ览器没有被使用的时候可以用RapaLsSession::StartDocument() Ҏ(gu)?/span>

下面是实C码:(x)

TBool CinternetAppUi::ConnectL(const TDesC& addr)

{

 const TInt KBrowserUid = 0x10008D39;

 TUid id( TUid::Uid( KBrowserUid ) );

 TApaTaskList taskList( CEikonEnv::Static()->WsSession() );

 TApaTask task = taskList.FindApp( id );

 // the system browser is in use

 if ( task.Exists() )

    {

    HBufC8* param8 = HBufC8::NewLC( addr.Length() );

    param8->Des().Append( addr );

    task.SendMessage( TUid::Uid( 0 ), *param8 ); // Uid is not used

    CleanupStack::PopAndDestroy();

    }

 // the system browser is not in use

else

 {

  RApaLsSession   appArcSession;

  User::LeaveIfError(appArcSession.Connect());    // connect to AppArc server

  TThreadId id;

  appArcSession.StartDocument( addr, TUid::Uid( KBrowserUid ), id );

  appArcSession.Close();

 }

 return ETrue;

}

//其中入口参数addr的格式是4”+” <Space>“+”<Url>”Q例?/span>“4  www.google.com?/span>

 

其中采用以上Ҏ(gu)不仅仅可以用于开启网,q可以用于启动安?/span>sis/sisxQ具体示例代码如下:(x)

RApaLsSession installSession;

TThreadId threadId;

TUid uid;

uid.iUid = 0x101F875A;

installSession.Connect();

installSession.StartDocument(aFileName, uid, threadId);

installSession.Close();

该代码自己没有亲过Q但是从理论上说应该可行Q而且有大牛说uid都不用传q去?/span>

另外播放音乐文gQ网上也说可以通过该方法来实现Q?/span>Uid分别如下Q?/span>

0x102072c3 (from S60 3rd Edition onwards)

0x6c5b9d2 (S60 2nd Edition)

RapaLsSession::StartDocument()功能q是很强大的Q在q里只做摘录Q以后有Z(x)再亲,不过用其打开|页的确可行?br>

 

 

 

 

 



frank.sunny 2009-08-12 17:36 发表评论
]]>
[整理]虚拟l承入门http://www.shnenglu.com/franksunny/archive/2008/10/16/64168.htmlfrank.sunnyfrank.sunnyThu, 16 Oct 2008 08:55:00 GMThttp://www.shnenglu.com/franksunny/archive/2008/10/16/64168.htmlhttp://www.shnenglu.com/franksunny/comments/64168.htmlhttp://www.shnenglu.com/franksunny/archive/2008/10/16/64168.html#Feedback6http://www.shnenglu.com/franksunny/comments/commentRss/64168.htmlhttp://www.shnenglu.com/franksunny/services/trackbacks/64168.htmlq次׃之后Q突然发现现在工作好像真的不是很好找Q没办法Q主动权不在自己手里Q静(rn)下心(j)来想惛_当通过W试来给自己查漏补缺吧,昨天W试遇到一个虚拟承的概念Q这不虽?/span>2分的题,但是q个玩意有大内容Q我学习(fn)?jin)下Q也先整个入门出来吧:(x)

 

Z么要引入虚拟l承Q?/span>

虚拟l承在一般的应用中很用刎ͼ所以也往往被忽视,q也主要是因为在C++中,多重l承是不推荐的,也ƈ不常用,而一旦离开?jin)多重承,虚拟l承完全失M(jin)存在的必要(因ؓ(f)q样只会(x)降低效率和占用更多的I间Q关于这一点,我自p没有太多深刻的理解,有兴的可以看网l上白杨的作?/span>?/span>RTTI、虚函数和虚?span lang=EN-US>cȝ开销分析?qing)用指?span lang=EN-US>?/span>Q说实话我目前还没看得很明白Q高?sh)可以指点下我?j)?/span>

以下面的一个例子ؓ(f)例:(x)

#include <iostream.h>

#include <memory.h>

class CA

{

    int k; //如果基类没有数据成员Q则在这里多重承编译不?x)出C义?/span>

public:

    void f() {cout << "CA::f" << endl;}

};

 

class CB : public CA

{

};

 

class CC : public CA

{

};

 

class CD : public CB, public CC

{

};

 

void main()

{

    CD d;

    d.f();

}

当编译上qC码时Q我们会(x)收到如下的错误提C:(x)

error C2385: 'CD::f' is ambiguous

即编译器无法定你在d.f()中要调用的函?/span>f到底是哪一个。这里可能会(x)让h觉得有些奇怪,命名只定义了(jin)一?/span>CA::fQ既然大安z?/span>CAQ那自然是调用?/span>CA::fQؓ(f)什么还无法定呢?

q是因ؓ(f)~译器在q行~译的时候,需要确定子cȝ函数定义Q如CA::f是确定的Q那么在~译CB?/span>CC时还需要在~译器的语法?wi)中生?/span>CB::fQ?/span>CC::f{标识,那么Q在~译CD的时候,׃CB?/span>CC都有一个函?/span>fQ此Ӟ~译器将试图生成q两?/span>CD::f标识Q显然这时就要报错了(jin)。(当我们不使用CD::f的时候,以上标识都不?x)生成,所以,如果Ld.f()一句,E序顺利通过~译Q?/span>

 

要解册个问题,有两个方法:(x)

1、重载函?/span>f()Q此时由于我们明定义了(jin)CD::fQ编译器(g)查到CD::f()调用时就无需再像上面一样去逐生成CD::f标识?jin)?/span>

此时CD的元素结构如下:(x)

|CB(CA)|

|CC(CA)|

故此时的sizeof(CD) = 8;Q?/span>CB?/span>CC各有一个元?/span>kQ?/span>

2、用虚拟承:(x)虚拟l承又称作共享承,q种׃n其实也是~译期间实现的,当用虚拟承时Q上面的E序变成下面的形式Q?/span>

#include <iostream.h>

#include <memory.h>

class CA

{

    int k;

public:

    void f() {cout << "CA::f" << endl;}

};

 

class CB : virtual public CA  //也有一U写法是class CB : public virtual CA

{                       //实际上这两种Ҏ(gu)都可?/span>

};

 

class CC : virtual public CA

{

};

 

class CD : public CB, public CC

{

};

 

void main()

{

    CD d;

    d.f();

}

此时Q当~译器确?/span>d.f()调用的具体含义时Q将生成如下?/span>CDl构Q?/span>

|CB|

|CC|

|CA|

同时Q在CB?/span>CC中都分别包含?jin)一个指?/span>CA的虚基类指针列表vbptrQ?/span>virtual base table pointerQ,其中记录的是?/span>CB?/span>CC的元素到CA的元素之间的偏移量。此Ӟ不会(x)生成各子cȝ函数f标识Q除非子c重载了(jin)该函敎ͼ从而达?#8220;׃n”的目的(q里的具体内存布局Q可以参看钻矛_l承内存布局Q在白杨的那文章中也有Q?/span>

也正因此Q此时的sizeof(CD) = 12Q两?/span>vbptr + sizoef(int)Q?/span>;

 

另注Q?/span>

如果CBQ?/span>CC中各定义一?/span>int型变量,?/span>sizeof(CD)变?/span>20(两个vbptr + 3?/span>sizoef(int)

如果CA中添加一?/span>virtual void f1(){}Q?/span>sizeof(CD) = 16Q两?/span>vbptr + sizoef(int)+vptrQ?/span>;

再添?/span>virtual void f2(){}Q?/span>sizeof(CD) = 16不变。原因如下所C:(x)带有虚函数的c,其内存布局上包含一个指向虚函数列表的指针(vptrQ,q跟有几个虚函数无关?/span>

以上内容涉及(qing)到类对象内存布局问题Q本难以做过多展开Q先贴这么多Q本文章只是考虑对于虚拟l承q行入门Q至于效率、应用等未作展开。本文在|上文章基础上修改了(jin)下而得此篇Q原文蝲?/span>http://blog.csdn.net/billdavid/archive/2004/06/23/24317.aspx

另外关于虚承和虚基cȝ讨论Q博客园有篇文章?/span>虚承与虚基cȝ本质》,ȝ得更l一炏V?/span>

 

 



frank.sunny 2008-10-16 16:55 发表评论
]]>
如何在C++中调用C的代?/title><link>http://www.shnenglu.com/franksunny/archive/2008/10/10/63675.html</link><dc:creator>frank.sunny</dc:creator><author>frank.sunny</author><pubDate>Fri, 10 Oct 2008 09:54:00 GMT</pubDate><guid>http://www.shnenglu.com/franksunny/archive/2008/10/10/63675.html</guid><wfw:comment>http://www.shnenglu.com/franksunny/comments/63675.html</wfw:comment><comments>http://www.shnenglu.com/franksunny/archive/2008/10/10/63675.html#Feedback</comments><slash:comments>1</slash:comments><wfw:commentRss>http://www.shnenglu.com/franksunny/comments/commentRss/63675.html</wfw:commentRss><trackback:ping>http://www.shnenglu.com/franksunny/services/trackbacks/63675.html</trackback:ping><description><![CDATA[<p class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><span lang=EN-US><o:p> </o:p></span></p> <p class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: center" align=center><span style="FONT-FAMILY: ?hu)? mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">如何?/span><span lang=EN-US>C++</span><span style="FONT-FAMILY: ?hu)? mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">中调?/span><span lang=EN-US>C</span><span style="FONT-FAMILY: ?hu)? mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">的代?/span></p> <p class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><span lang=EN-US><o:p> </o:p></span></p> <p class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 21pt; mso-char-indent-count: 2.0"><span style="FONT-FAMILY: ?hu)? mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">以前曄ȝq一?<a href="http://www.shnenglu.com/franksunny/archive/2007/11/29/37510.html">http://www.shnenglu.com/franksunny/archive/2007/11/29/37510.html</a>)Q关于在</span><span lang=EN-US>C</span><span style="FONT-FAMILY: ?hu)? mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">中如何调?/span><span lang=EN-US>C++</span><span style="FONT-FAMILY: ?hu)? mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">的代码,当时q未做完全的展开Q只是简单的做了(jin)下调试,最q看C个题目要求实?/span><span lang=EN-US>C</span><span style="FONT-FAMILY: ?hu)? mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">?/span><span lang=EN-US>C++</span><span style="FONT-FAMILY: ?hu)? mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">中代码的互相调用Q其l果虽然都是<span style="COLOR: red">通过</span></span><span lang=EN-US style="COLOR: red">extern “C”</span><span style="COLOR: red; FONT-FAMILY: ?hu)? mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">来实?/span><span style="FONT-FAMILY: ?hu)? mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">Q但是具体还是有些差别的?/span></p> <p class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 21pt; mso-char-indent-count: 2.0"><span style="FONT-FAMILY: ?hu)? mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">先对</span><span lang=EN-US>C</span><span style="FONT-FAMILY: ?hu)? mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">中调?/span><span lang=EN-US>C++</span><span style="FONT-FAMILY: ?hu)? mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">代码作个单回:(x)</span></p> <p class=MsoNormal style="MARGIN: 0cm 0cm 0pt 57.75pt; TEXT-INDENT: -36.75pt; mso-list: l0 level1 lfo1; tab-stops: list 57.75pt"><span lang=EN-US style="mso-fareast-font-family: 'Times New Roman'"><span style="mso-list: Ignore">1?span style="FONT: 7pt 'Times New Roman'">              </span></span></span><span style="FONT-FAMILY: ?hu)? mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">对于</span><span lang=EN-US>C++</span><span style="FONT-FAMILY: ?hu)? mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">中非cȝ成员函数Q可以简单的?span style="COLOR: red">函数声明前面?/span></span><span lang=EN-US style="COLOR: red">extern “C”</span><span style="FONT-FAMILY: ?hu)? mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">Q通常函数声明位于头文件中Q当然也可以<span style="COLOR: red">声明和函数定义一h?/span></span><span lang=EN-US style="COLOR: red">cpp</span><span style="COLOR: red; FONT-FAMILY: ?hu)? mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">?/span><span style="FONT-FAMILY: ?hu)? mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">Q在<span style="COLOR: red">没有声明的情况下Q直接在定义前添?/span></span><span lang=EN-US style="COLOR: red">extern “C”</span><span style="COLOR: red; FONT-FAMILY: ?hu)? mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">也可</span></p> <p class=MsoNormal style="MARGIN: 0cm 0cm 0pt 57.75pt; TEXT-INDENT: -36.75pt; mso-list: l0 level1 lfo1; tab-stops: list 57.75pt"><span lang=EN-US style="mso-fareast-font-family: 'Times New Roman'"><span style="mso-list: Ignore">2?span style="FONT: 7pt 'Times New Roman'">              </span></span></span><span style="FONT-FAMILY: ?hu)? mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">对于</span><span lang=EN-US>C++</span><span style="FONT-FAMILY: ?hu)? mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">cȝ成员函数Q则<span style="COLOR: red">需要另外做一?/span></span><span lang=EN-US style="COLOR: red">cpp</span><span style="COLOR: red; FONT-FAMILY: ?hu)? mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">文gQ将需要调用的函数q行包装</span><span style="FONT-FAMILY: ?hu)? mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">?/span></p> <p class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 21pt; mso-char-indent-count: 2.0"><span style="FONT-FAMILY: ?hu)? mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">以上两项的实例参看前?/span><span lang=EN-US>C</span><span style="FONT-FAMILY: ?hu)? mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">中如何调?/span><span lang=EN-US>C++</span><span style="FONT-FAMILY: ?hu)? mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">代码的文章?/span></p> <p class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 21pt; mso-char-indent-count: 2.0"><span style="FONT-FAMILY: ?hu)? mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">要实?/span><span lang=EN-US>C++</span><span style="FONT-FAMILY: ?hu)? mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">中调?/span><span lang=EN-US>C</span><span style="FONT-FAMILY: ?hu)? mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">的代码,具体操作Q?/span></p> <p class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 21pt; mso-char-indent-count: 2.0"><span style="FONT-FAMILY: ?hu)? mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">对于</span><span lang=EN-US>C</span><span style="FONT-FAMILY: ?hu)? mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">中的函数代码Q要?span style="COLOR: red">?/span></span><span lang=EN-US style="COLOR: red">C</span><span style="COLOR: red; FONT-FAMILY: ?hu)? mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">代码的头文gq行修改Q在其被含入</span><span lang=EN-US style="COLOR: red">C++</span><span style="COLOR: red; FONT-FAMILY: ?hu)? mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">代码时在声明中加?/span><span lang=EN-US style="COLOR: red">extern “C”</span><span style="FONT-FAMILY: ?hu)? mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">或?span style="COLOR: red">?/span></span><span lang=EN-US style="COLOR: red">C++</span><span style="COLOR: red; FONT-FAMILY: ?hu)? mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">代码中重新声明一?/span><span lang=EN-US style="COLOR: red">C</span><span style="COLOR: red; FONT-FAMILY: ?hu)? mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">函数Q重新声明时d?/span><span lang=EN-US style="COLOR: red">extern “C”</span><span style="COLOR: red; FONT-FAMILY: ?hu)? mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">?/span><span style="FONT-FAMILY: ?hu)? mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">?/span></p> <p class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 21pt; mso-char-indent-count: 2.0"><span style="FONT-FAMILY: ?hu)? mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">通过以上的说明,我明白一点,那就?span style="COLOR: red">?/span></span><span lang=EN-US style="COLOR: red">extern “C”</span><span style="COLOR: red; FONT-FAMILY: ?hu)? mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">头一定是加在</span><span lang=EN-US style="COLOR: red">C++</span><span style="COLOR: red; FONT-FAMILY: ?hu)? mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">的代码文件中才能起作用的</span><span style="FONT-FAMILY: ?hu)? mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">?/span></p> <p class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 21pt; mso-char-indent-count: 2.0"><span lang=EN-US><o:p> </o:p></span></p> <p class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 21pt; mso-char-indent-count: 2.0"><span style="FONT-FAMILY: ?hu)? mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">下面分析一下这个现象的实质原因Q?/span></p> <p class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 21pt; mso-char-indent-count: 2.0"><span lang=EN-US>C</span><span style="FONT-FAMILY: ?hu)? mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">~译器编译函数时不带函数的类型信息,只包含函数符号名字,?/span><span lang=EN-US>C</span><span style="FONT-FAMILY: ?hu)? mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">~译器把函数</span><span lang=EN-US>int a(float x)</span><span style="FONT-FAMILY: ?hu)? mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">~译成类?/span><span lang=EN-US>_a</span><span style="FONT-FAMILY: ?hu)? mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">q样的符P</span><span lang=EN-US>C</span><span style="FONT-FAMILY: ?hu)? mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">q接器只要找C(jin)调用函数的符P可以连接成功,它假讑֏数类型信息是正确的,q是</span><span lang=EN-US>C</span><span style="FONT-FAMILY: ?hu)? mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">~译q接器的~点。?/span><span lang=EN-US>C++</span><span style="FONT-FAMILY: ?hu)? mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">~译器ؓ(f)?jin)实现函数重载,~译时会(x)带上函数的类型信息,如他把上面的</span><span lang=EN-US>a</span><span style="FONT-FAMILY: ?hu)? mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">函数可能~译?/span><span lang=EN-US>_a_float</span><span style="FONT-FAMILY: ?hu)? mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">q样的符号ؓ(f)?jin)实现重载,注意它还是没有带q回值得信息Q这也是Z?/span><span lang=EN-US>C++</span><span style="FONT-FAMILY: ?hu)? mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">不支持采用函数返回值来区别函数重蝲的原因之一Q当?dng)函数的用者对函数q回值的处理方式Q如忽略Q也是重要原因?/span></p> <p class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 21pt; mso-char-indent-count: 2.0"><span style="FONT-FAMILY: ?hu)? mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">Z以上Q?/span><span lang=EN-US>C</span><span style="FONT-FAMILY: ?hu)? mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">调用</span><span lang=EN-US>C++</span><span style="FONT-FAMILY: ?hu)? mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">Q首先需要用装函数把对</span><span lang=EN-US>C++</span><span style="FONT-FAMILY: ?hu)? mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">的类{的调用装?/span><span lang=EN-US>C</span><span style="FONT-FAMILY: ?hu)? mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">函数以便</span><span lang=EN-US>C</span><span style="FONT-FAMILY: ?hu)? mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">调用Q于?/span><span lang=EN-US>extern "C"</span><span style="FONT-FAMILY: ?hu)? mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">的作用是Q让~译器知道这件事Q然?span style="COLOR: red">?/span></span><span lang=EN-US style="COLOR: red">C</span><span style="COLOR: red; FONT-FAMILY: ?hu)? mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">语言的方式编译和q接装函数</span><span style="FONT-FAMILY: ?hu)? mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">Q?span style="COLOR: red">通常是把装函数?/span></span><span lang=EN-US style="COLOR: red">C++</span><span style="COLOR: red; FONT-FAMILY: ?hu)? mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">~译器按</span><span lang=EN-US style="COLOR: red">C++</span><span style="COLOR: red; FONT-FAMILY: ?hu)? mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">方式~译Q用?/span><span lang=EN-US style="COLOR: red">extern "C" </span><span style="COLOR: red; FONT-FAMILY: ?hu)? mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">后,~译器便?/span><span lang=EN-US style="COLOR: red">C</span><span style="COLOR: red; FONT-FAMILY: ?hu)? mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">的方式编译封装接口,当然接口函数里面?/span><span lang=EN-US style="COLOR: red">C++</span><span style="COLOR: red; FONT-FAMILY: ?hu)? mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">语法q是?/span><span lang=EN-US style="COLOR: red">C++</span><span style="COLOR: red; FONT-FAMILY: ?hu)? mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">方式~译Q对?/span><span lang=EN-US style="COLOR: red">C</span><span style="COLOR: red; FONT-FAMILY: ?hu)? mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">语言部分</span><span lang=EN-US style="COLOR: red">--</span><span style="COLOR: red; FONT-FAMILY: ?hu)? mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">调用者,q是?/span><span lang=EN-US style="COLOR: red">C</span><span style="COLOR: red; FONT-FAMILY: ?hu)? mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">语言~译Q分别对</span><span lang=EN-US style="COLOR: red">C++</span><span style="COLOR: red; FONT-FAMILY: ?hu)? mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">接口部分?/span><span lang=EN-US style="COLOR: red">C</span><span style="COLOR: red; FONT-FAMILY: ?hu)? mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">部分~译后,再连接就可以实现</span><span lang=EN-US style="COLOR: red">C</span><span style="COLOR: red; FONT-FAMILY: ?hu)? mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">调用</span><span lang=EN-US style="COLOR: red">C++</span><span style="COLOR: red; FONT-FAMILY: ?hu)? mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">?/span><span style="FONT-FAMILY: ?hu)? mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">Q。相?/span><span lang=EN-US>,C++</span><span style="FONT-FAMILY: ?hu)? mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">调用</span><span lang=EN-US>C</span><span style="FONT-FAMILY: ?hu)? mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">函数Q?/span><span lang=EN-US>extern "C" </span><span style="FONT-FAMILY: ?hu)? mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">的作用是Q让</span><span lang=EN-US style="COLOR: red">C++</span><span style="COLOR: red; FONT-FAMILY: ?hu)? mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">q接器找调用函数的符h采用</span><span lang=EN-US style="COLOR: red">C</span><span style="COLOR: red; FONT-FAMILY: ?hu)? mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">的方?/span><span style="FONT-FAMILY: ?hu)? mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">Q即使用</span><span lang=EN-US>_a</span><span style="FONT-FAMILY: ?hu)? mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">而不?/span><span lang=EN-US>_a_float</span><span style="FONT-FAMILY: ?hu)? mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">来找调用函数?/span></p> <p class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 21pt; mso-char-indent-count: 2.0"><span lang=EN-US><o:p> </o:p></span></p> <p class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 21pt; mso-char-indent-count: 2.0"><span style="FONT-FAMILY: ?hu)? mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">具体CZ误</span><span lang=EN-US><a href="http://www.shnenglu.com/Files/franksunny/CCallCpp.rar"><u><font color=#0000ff>http://www.shnenglu.com/Files/franksunny/CCallCpp.rar</font></u></a></span></p> <p class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 21pt; mso-char-indent-count: 2.0"><span style="FONT-FAMILY: ?hu)? mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">注:(x)如果你用</span><span lang=EN-US>VC6.0</span><span style="FONT-FAMILY: ?hu)? mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">~译附g旉到类?/span><span lang=EN-US style="FONT-SIZE: 10pt; COLOR: black; FONT-FAMILY: Arial">“fatal error C1010: unexpected end of file while looking for precompiled header directive”</span><span style="FONT-SIZE: 10pt; COLOR: black; FONT-FAMILY: ?hu)? mso-ascii-font-family: Arial; mso-hansi-font-family: Arial; mso-bidi-font-family: Arial">报错的话Q请?/span><span lang=EN-US style="FONT-SIZE: 10pt; COLOR: black; FONT-FAMILY: Arial">bb.c</span><span style="FONT-SIZE: 10pt; COLOR: black; FONT-FAMILY: ?hu)? mso-ascii-font-family: Arial; mso-hansi-font-family: Arial; mso-bidi-font-family: Arial">文g做如下处理右键点击项目工E中的该文gQ选择</span><span lang=EN-US style="FONT-SIZE: 10pt; COLOR: black; FONT-FAMILY: Arial">setting</span><span style="FONT-SIZE: 10pt; COLOR: black; FONT-FAMILY: ?hu)? mso-ascii-font-family: Arial; mso-hansi-font-family: Arial; mso-bidi-font-family: Arial">Q在</span><span lang=EN-US style="FONT-SIZE: 10pt; COLOR: black; FONT-FAMILY: Arial">c/c++</span><span style="FONT-SIZE: 10pt; COLOR: black; FONT-FAMILY: ?hu)? mso-ascii-font-family: Arial; mso-hansi-font-family: Arial; mso-bidi-font-family: Arial">栏,选择</span><span lang=EN-US style="FONT-SIZE: 10pt; COLOR: black; FONT-FAMILY: Arial">PreCompiled headers</span><span style="FONT-SIZE: 10pt; COLOR: black; FONT-FAMILY: ?hu)? mso-ascii-font-family: Arial; mso-hansi-font-family: Arial; mso-bidi-font-family: Arial">Q然后设|第一选项Q选择不用预~译头?/span><span lang=EN-US style="FONT-SIZE: 10pt; COLOR: black; FONT-FAMILY: Arial"><o:p></o:p></span></p> <p class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 21pt; mso-char-indent-count: 2.0"><span lang=EN-US><o:p> </o:p></span></p> <img src ="http://www.shnenglu.com/franksunny/aggbug/63675.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.shnenglu.com/franksunny/" target="_blank">frank.sunny</a> 2008-10-10 17:54 <a href="http://www.shnenglu.com/franksunny/archive/2008/10/10/63675.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>C++多态的实现Q第一ơ接到面试电(sh)话,汗颜一下)(j)http://www.shnenglu.com/franksunny/archive/2008/05/19/50424.htmlfrank.sunnyfrank.sunnyMon, 19 May 2008 12:30:00 GMThttp://www.shnenglu.com/franksunny/archive/2008/05/19/50424.htmlhttp://www.shnenglu.com/franksunny/comments/50424.htmlhttp://www.shnenglu.com/franksunny/archive/2008/05/19/50424.html#Feedback8http://www.shnenglu.com/franksunny/comments/commentRss/50424.htmlhttp://www.shnenglu.com/franksunny/services/trackbacks/50424.html 

今天接到?sh)话面试Q被问到几个问题Q汗颜之余,结一?/span>

1?span style="FONT: 7pt 'Times New Roman'">      多态是如何实现l定?/span>

多态的l定可以分ؓ(f)q行是多态和~译时多?/span>

?/span> ~译时的多态?/span>

~译时的多态性是通过重蝲来实现的。对于非虚的成员来说Q系l在~译ӞҎ(gu)传递的参数、返回的cd{信息决定实CU操作?/span>

?/span> q行时的多态?/span>

q行时的多态性就是指直到pȝq行Ӟ才根据实际情况决定实CU操作?/span>C#中,q行时的多态性通过虚成员实现?/span>

~译时的多态性ؓ(f)我们提供?jin)运行速度快的特点Q而运行时的多态性则带来?jin)高度灵zd抽象的特炏V?/span>

今天才正式弄清楚原来虚函数是可以实现q行时多态的Q以前只知道虚函数可以得基cd象的的方法调用派生类的方法?/span>

2?span style="FONT: 7pt 'Times New Roman'">      析构函数是虚函数的优Ҏ(gu)什?/span>

?/span>C++开发的时候,用来做基cȝcȝ析构函数一般都是虚函数。可是,Z么要q样做呢Q下面用一个小例子来说明:(x)

有下面的两个c:(x)

class ClxBase

{

public:

    ClxBase() {};

    virtual ~ClxBase() {};

 

    virtual void DoSomething() { cout << "Do something in class ClxBase!" << endl; };

};

 

class ClxDerived : public ClxBase

{

public:

    ClxDerived() {};

    ~ClxDerived() { cout << "Output from the destructor of class ClxDerived!" << endl; };

 

    void DoSomething() { cout << "Do something in class ClxDerived!" << endl; };

};

 

代码

 

ClxBase *pTest = new ClxDerived;

pTest->DoSomething();

delete pTest;

 

输出l果是:(x)

 

Do something in class ClxDerived!

Output from the destructor of class ClxDerived!

 

q个很简单,非常好理解?/span>

但是Q如果把c?/span>ClxBase析构函数前的virtualLQ那输出l果是下面的样子了(jin)Q?/span>

Do something in class ClxDerived!

也就是说Q类ClxDerived的析构函数根本没有被调用Q一般情况下cȝ析构函数里面都是释放内存资源Q而析构函C被调用的话就?x)造成内存泄漏。我x(chng)有的C++E序员都知道q样的危险性。当?dng)如果在析构函C做了(jin)其他工作的话Q那你的所有努力也都是白费力气?/span>

所以,文章开头的那个问题的答案就是-Q这样做是ؓ(f)?jin)当用一个基cȝ指针删除一个派生类的对象时Q派生类的析构函C(x)被调用?/span>

当然Qƈ不是要把所有类的析构函数都写成虚函数。因为当c里面有虚函数的时候,~译器会(x)l类d一个虚函数表,里面来存放虚函数指针Q这样就?x)增加类的存储空间。所以,只有当一个类被用来作为基cȝ时候,才把析构函数写成虚函数?/span>

 

说实话,q个也是今天才深刻认识到的?/span>

 

当然q问到很多数据结构和法斚wQ空间复杂度和时间复杂度之类的东东,说真的也是基性的Q的问题Q至于那些东西,自己说实话抛开没用他们已经很长旉?jin),真可以说忘的差不多?jin)Q考这U真的很怕,也怪^时没怎么用到。不知道大家用的多不Q?/span>

好久没有正式参加q面试了(jin)Q今天突然来一ơ觉得自己基q是不够扎实?/span>



frank.sunny 2008-05-19 20:30 发表评论
]]>
C中如何调用C++函数http://www.shnenglu.com/franksunny/archive/2007/11/29/37510.htmlfrank.sunnyfrank.sunnyThu, 29 Nov 2007 12:38:00 GMThttp://www.shnenglu.com/franksunny/archive/2007/11/29/37510.htmlhttp://www.shnenglu.com/franksunny/comments/37510.htmlhttp://www.shnenglu.com/franksunny/archive/2007/11/29/37510.html#Feedback19http://www.shnenglu.com/franksunny/comments/commentRss/37510.htmlhttp://www.shnenglu.com/franksunny/services/trackbacks/37510.html 

C中如何调?/span>C++函数?

 

前阵子被问及(qing)一个在C中如何调?/span>C++函数的问?/span>Q当时简单回{是函?/span>?span lang=EN-US style="COLOR: black">extern "C"声明Q当被问?qing)如何将cd成员函数声明Ӟ一时语塞,后来|上查了(jin)下,|上有一译C++之父的文章可以作{,遂拿?span lang=EN-US>Mark一下?span lang=EN-US>

 

?/span> C++ 函数声明?/span>``extern "C"''Q在你的 C++ 代码里做q个声明Q,然后调用它(在你?/span> C 或?/span> C++ 代码里调用)(j)。例如:(x)

// C++ code:

extern "C" void f(int);

void f(int i)

{

     // ...

}

 

然后Q你可以q样使用 f()Q?/span>

/* C code: */

void f(int);

void cc(int i)

{

    f(i);

   /* ... */

    }

 

当然Q这招只适用于非成员函数。如果你惌?/span> C 里调用成员函敎ͼ包括虚函敎ͼ(j)Q则需要提供一个简单的包装Q?/span>wrapperQ。例如:(x)

// C++ code:

class C

{

       // ...

       virtual double f(int);

};

 

extern "C" double call_C_f(C* p, int i) // wrapper function

{

       return p->f(i);

}

 

然后Q你可以这栯?/span> C::f()Q?/span>

/* C code: */

double call_C_f(struct C* p, int i);

 

void ccc(struct C* p, int i)

{

       double d = call_C_f(p,i);

       /* ... */

}

 

如果你想?/span> C 里调用重载函敎ͼ则必L供不同名字的包装Q这h能被 C 代码调用。例?/span>Q?/span>

// C++ code:

void f(int);

void f(double);

 

extern "C" void f_i(int i) { f(i); }

extern "C" void f_d(double d) { f(d); }

 

然后Q你可以q样使用每个重蝲?/span> f()Q?/span>

/* C code: */

void f_i(int);

void f_d(double);

 

void cccc(int i,double d)

{

       f_i(i);

       f_d(d);

       /* ... */

}

注意Q这些技巧也适用于在 C 里调?/span> C++ cdQ即使你不能Q或者不惻I(j)修改 C++ 头文件?/span>

 该翻译的文Bjarne Stroustrup的原文链接地址?/span>

http://www.research.att.com/~bs/bs_faq2.html#callCpp

本来贴出来以后受到很多C/C++朋友的关注,非常荣幸Q在“梦在天(dng)”的提醒下Q本人后来又完成?jin)一个Demo工程Q发现和BJ说的有点出入Q希望有高手指点QDemo工程下蝲链接如下Q?/o:p>http://www.shnenglu.com/Files/franksunny/cCallCppDemo.rar

 

 



frank.sunny 2007-11-29 20:38 发表评论
]]>
无符号变量居然也能输?1http://www.shnenglu.com/franksunny/archive/2007/10/17/34495.htmlfrank.sunnyfrank.sunnyWed, 17 Oct 2007 14:46:00 GMThttp://www.shnenglu.com/franksunny/archive/2007/10/17/34495.htmlhttp://www.shnenglu.com/franksunny/comments/34495.htmlhttp://www.shnenglu.com/franksunny/archive/2007/10/17/34495.html#Feedback13http://www.shnenglu.com/franksunny/comments/commentRss/34495.htmlhttp://www.shnenglu.com/franksunny/services/trackbacks/34495.html今天一个很偶然的机?x),需要回{一个将无符h据存到有W号变量的问题。我~码如下Q结果很有意思,我是在VC6里调试的Q有高(sh)h看到可否帮忙指点下?br>int main()
{
    unsigned short temp1 = 65535;
    short temp2 = temp1;
    unsigned short temp3 = (unsigned short)temp2;
    unsigned short temp4 = temp2;
    int temp5 = temp2;
    unsigned int temp6 = temp2;
    unsigned long temp7 = temp2;
    int temp8 = (unsigned short)temp2;
    short temp9 = temp2;
    printf("temp1 = %d\n temp2 = %d\n temp3 = %d\n temp4 = %d\n temp5 = %d\n temp6 = %d\n temp7 = %d\n temp8 = %d\n temp9 = %d\n",
     temp1,temp2,temp3,temp4, temp5,temp6,temp7,temp8,temp9);
    return 0;
}
//改程序的输出l果
//temp1 = 65535
//temp2 = -1
//temp3 = 65535
//temp4 = 65535
//temp5 = -1
//temp6 = -1
//temp7 = -1
//temp8 = 65535
//temp9 = -1;


//Ҏ(gu)l果也就是说Q无W号W号数据是可以存储在有符号型变量内存?sh)?
//而且有例子在内存块长度一hQ不用强转,直接赋给无符号变量时也可?br>//上述事实可以解释为内存块不变Q采用不同的解码方式解出不同的数?br>//但是d来的时候要注意Q如果有W号转无W号一定要{
//之所以上例unsigned int输出-1Q我q不是很清楚



frank.sunny 2007-10-17 22:46 发表评论
]]>
C++的static关键?转蝲)http://www.shnenglu.com/franksunny/archive/2007/10/16/34373.htmlfrank.sunnyfrank.sunnyTue, 16 Oct 2007 13:03:00 GMThttp://www.shnenglu.com/franksunny/archive/2007/10/16/34373.htmlhttp://www.shnenglu.com/franksunny/comments/34373.htmlhttp://www.shnenglu.com/franksunny/archive/2007/10/16/34373.html#Feedback2http://www.shnenglu.com/franksunny/comments/commentRss/34373.htmlhttp://www.shnenglu.com/franksunny/services/trackbacks/34373.html阅读全文

frank.sunny 2007-10-16 21:03 发表评论
]]>
Visual Assist X AutoText修改http://www.shnenglu.com/franksunny/archive/2007/09/14/32223.htmlfrank.sunnyfrank.sunnyFri, 14 Sep 2007 09:27:00 GMThttp://www.shnenglu.com/franksunny/archive/2007/09/14/32223.htmlhttp://www.shnenglu.com/franksunny/comments/32223.htmlhttp://www.shnenglu.com/franksunny/archive/2007/09/14/32223.html#Feedback0http://www.shnenglu.com/franksunny/comments/commentRss/32223.htmlhttp://www.shnenglu.com/franksunny/services/trackbacks/32223.html自从开始用VC以后Q一直以来都在用Visual AssistQ感觉这个工L(fng)的非常好Q但是有些自动文功能就个h觉得q很适合Q不q可能由于自w的E文比较差吧,从来没想q自己如何去修改q个东西。来新单位已l两个礼拜了(jin)Q马上又要下班了(jin)Q就冲着q个查了(jin)下如何修改AutoTextQ终于发C(jin)官网上的详细说明Q现在粘帖在下面Q以备不时之需 http://www.wholetomato.com/products/features/autotext.asp

Z(jin)查找方便Q自׃常用的命o(h)_帖q来

Code

Type your expanded code into the Code field. Include reserved strings to expand the date, filename and more.


Reserved String Meaning
Date $DATE$ Year/month/day formatted as %04d/%02d/%02d
$DAY$ Day of month formatted as %d
$DAY_02$ Day of month formatted as %02d
$DAYNAME$ Three-character abbreviation of day
$DAYLONGNAME$ Full name of day
$MONTH$ Month formatted as %d
$MONTH_02$ Month formatted as %02d
$MONTHNAME$ Three-character abbreviation of month
$MONTHLONGNAME$ Full name of month
$YEAR$ Year formatted as %d
$YEAR_02$ Year formatted as %02d
File $FILE$ Full filename with path*
$FILE_UPPER$ Full filename with path in uppercase*
$FILE_BASE$ Filename without path or extension*
$FILE_BASE_UPPER$ Filename without path or extension in upper case*
$FILE_EXT$ Filename extension*
$FILE_EXT_UPPER$ Filename extension in upper case*
$FILE_PATH$ Path of file*
$FILE_PATH_UPPER$ Path of file in upper case*
General $clipboard$ Current clipboard
$end$ Position of caret after expansion
$selected$ Current selection**
$$ $
GUID $GUID_DEFINITION$ Generated GUID formatted for use in a definition
$GUID_STRING$ Generated GUID formatted for use in a string
$GUID_STRUCT$ Generated GUID formatted for use in a struct
(Note that all instances of GUID reserved words will use a singe generated GUID.)
Refactor $GeneratedPropertyName$ Property name generated during Encapsulate Field
$MethodArg$ One parameter of the method and its type
$MethodArgName$ One parameter of the method
$MethodArgType$ Type of one parameter of the method
$MethodBody$ Body of implementation
$MethodQualifier$ Optional qualifiers of method
$ParameterList$ Parameters separated by commas
$SymbolContext$ Context and name of method
$SymbolName$ Name of method
$SymbolPrivileges$ Access of method
$SymbolStatic$ Keyword static or blank
$SymbolType$ Return type of method
$SymbolVirtual$ Keyword virtual or blank
Time $HOUR$ Hour formatted as %d
$HOUR_02$ Hour formatted as %02d
$MINUTE$ Minute formatted as %02d
$SECOND$ Second formatted as %02d

*Reserved strings beginning with $FILE expand using the case of the current file.

**Lines with whitespace and $selected$ are omitted from expanded code if there is no selection. (This lets you define a single entry to be used with and without a selection.)

Autotext entries containing $GUID_* are available in IDL files.

Access the list of reserved strings using the context menu inside the Code field when editing Autotext.

autotextCodeInsertNew.png 

frank.sunny 2007-09-14 17:27 发表评论
]]>
[转]重入和不可重入函数概忉|?/title><link>http://www.shnenglu.com/franksunny/archive/2007/08/03/29269.html</link><dc:creator>frank.sunny</dc:creator><author>frank.sunny</author><pubDate>Fri, 03 Aug 2007 04:56:00 GMT</pubDate><guid>http://www.shnenglu.com/franksunny/archive/2007/08/03/29269.html</guid><wfw:comment>http://www.shnenglu.com/franksunny/comments/29269.html</wfw:comment><comments>http://www.shnenglu.com/franksunny/archive/2007/08/03/29269.html#Feedback</comments><slash:comments>2</slash:comments><wfw:commentRss>http://www.shnenglu.com/franksunny/comments/commentRss/29269.html</wfw:commentRss><trackback:ping>http://www.shnenglu.com/franksunny/services/trackbacks/29269.html</trackback:ping><description><![CDATA[<p class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><span lang=EN-US><o:p> </o:p></span></p> <p class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: center" align=center><span style="FONT-SIZE: 16pt; FONT-FAMILY: ?hu)? mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">可重入函C不可重入函数</span><span lang=EN-US style="FONT-SIZE: 16pt"><o:p></o:p></span></p> <p class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><span lang=EN-US><o:p> </o:p></span></p> <p class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 21pt; mso-char-indent-count: 2.0"><span style="COLOR: black; FONT-FAMILY: ?hu)? mso-bidi-font-size: 10.5pt">主要用于多Q务环境中Q一个可重入的函数简单来说就是可以被中断的函敎ͼ也就是说Q可以在q个函数执行的Q何时M断它Q{?span lang=EN-US>OS</span>调度下去执行另外一D代码,而返回控制时不会(x)出现什么错误;而不可重入的函数׃使用?jin)一些系l资源,比如全局变量区,中断向量表等Q所以它如果被中断的话,可能?x)出现问题,q类函数是不能运行在多Q务环境下的?span lang=EN-US><o:p></o:p></span></span></p> <p class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 21pt; mso-char-indent-count: 2.0"><span style="COLOR: black; FONT-FAMILY: ?hu)? mso-bidi-font-size: 10.5pt">也可以这L(fng)解,重入卌C重复进入,首先它意味着q个函数可以被中断,其次意味着它除?jin)用自己栈上的变量以外不依赖于M环境Q包?span lang=EN-US>static</span>Q,q样的函数就?span lang=EN-US>purecode</span>Q纯代码Q可重入Q可以允许有该函数的多个副本在运行,׃它们使用的是分离的栈Q所以不?x)互相干扰。如果确实需要访问全局变量Q包?span lang=EN-US>static</span>Q,一定要注意实施互斥手段。可重入函数在ƈ行运行环境中非常重要Q但是一般要问全局变量付出一些性能代h(hun)?span lang=EN-US><o:p></o:p></span></span></p> <p class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 21pt; mso-char-indent-count: 2.0"><span style="COLOR: black; FONT-FAMILY: ?hu)? mso-bidi-font-size: 10.5pt">~写可重入函数时Q若使用全局变量Q则应通过关中断、信号量Q即<span lang=EN-US>P</span>?span lang=EN-US>V</span>操作Q等手段对其加以保护?span lang=EN-US><o:p></o:p></span></span></p> <p class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 21pt; mso-char-indent-count: 2.0"><span lang=EN-US style="COLOR: black; FONT-FAMILY: ?hu)? mso-bidi-font-size: 10.5pt"><span style="mso-spacerun: yes"> </span></span><span style="COLOR: black; FONT-FAMILY: ?hu)? mso-bidi-font-size: 10.5pt">说明Q若Ҏ(gu)使用的全局变量不加以保护,则此函数׃h可重入性,卛_多个q程调用此函数时Q很有可能有关全局变量变(sh)ؓ(f)不可知状态?span lang=EN-US><o:p></o:p></span></span></p> <p class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 21pt; mso-char-indent-count: 2.0"><span lang=EN-US style="COLOR: black; FONT-FAMILY: ?hu)? mso-bidi-font-size: 10.5pt"><o:p> </o:p></span></p> <p class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 21pt; mso-char-indent-count: 2.0"><span style="COLOR: black; FONT-FAMILY: ?hu)? mso-bidi-font-size: 10.5pt">CZQ假?span lang=EN-US>Exam</span>?span lang=EN-US>int</span>型全局变量Q函?span lang=EN-US>Squre_Exam</span>q回<span lang=EN-US>Exam</span>qx(chng)倹{那么如下函Ch可重入性?span lang=EN-US><o:p></o:p></span></span></p> <p class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 21pt; mso-char-indent-count: 2.0"><span lang=EN-US style="COLOR: black; FONT-FAMILY: ?hu)? mso-bidi-font-size: 10.5pt">unsigned int example( int para ) <o:p></o:p></span></p> <p class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 21pt; mso-char-indent-count: 2.0"><span lang=EN-US style="COLOR: black; FONT-FAMILY: ?hu)? mso-bidi-font-size: 10.5pt">{<o:p></o:p></span></p> <p class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 21pt; mso-char-indent-count: 2.0"><span lang=EN-US style="COLOR: black; FONT-FAMILY: ?hu)? mso-bidi-font-size: 10.5pt">    unsigned int temp;<br>        Exam = para; // </span><span style="COLOR: black; FONT-FAMILY: ?hu)? mso-bidi-font-size: 10.5pt">Q?span lang=EN-US>**</span>Q?span lang=EN-US><br>        temp = Square_Exam( );<br>        return temp;<br>    }<br>    </span>此函数若被多个进E调用的话,其结果可能是未知的,因ؓ(f)当(<span lang=EN-US>**</span>Q语句刚执行完后Q另外一个用本函数的进E可能正好被Ȁz,那么当新Ȁzȝq程执行到此函数Ӟ<span lang=EN-US>Exam</span>赋与另一个不同的<span lang=EN-US>para</span>|所以当控制重新回到<span lang=EN-US>“temp = Square_Exam( )”</span>后,计算出的<span lang=EN-US>temp</span>很可能不是预想中的结果。此函数应如下改q?span lang=EN-US><br><br>    unsigned int example( int para ) {<br>        unsigned int temp;<br>        [</span>甌信号量操?span lang=EN-US>] //(1)<br>        Exam = para;<br>        temp = Square_Exam( );<br>        [</span>释放信号量操?span lang=EN-US>]<br>        return temp;<br>    }<br>    (1)</span>若申请不?span lang=EN-US>“</span>信号?span lang=EN-US>”</span>Q说明另外的q程正处于给<span lang=EN-US>Exam</span>赋值ƈ计算其^方过E中Q即正在使用此信P(j)<span lang=EN-US>,</span>本进E必ȝ待其释放信号后,才可l箋(hu)执行。若甌CP则可l箋(hu)执行Q但其它q程必须{待本进E释放信号量后,才能再用本信号?span lang=EN-US><br><br>    </span>保证函数的可重入性的Ҏ(gu)Q?span lang=EN-US><br>    </span>在写函数时候尽量用局部变量(例如寄存器、堆栈中的变量)(j)Q对于要使用的全局变量要加以保护(如采取关中断、信号量{方法)(j)Q这h成的函数׃定是一个可重入的函数?span lang=EN-US><br>    VxWorks</span>中采取的可重入的技术有Q?span lang=EN-US><br>    * </span>动态堆栈变量(各子函数有自q立的堆栈I间Q?span lang=EN-US><br>    * </span>受保护的全局变量和静(rn)态变?span lang=EN-US><br>    * </span>d变量<span lang=EN-US><br><br><br>--------------------------------------------------<br>    </span>在实时系l的设计中,l常?x)出现多个Q务调用同一个函数的情况。如果这个函Cq被设计成ؓ(f)不可重入的函数的话,那么不同d调用q个函数时可能修改其他Q务调用这个函数的数据Q从而导致不可预料的后果。那么什么是可重入函数呢Q所谓可重入函数是指一个可以被多个d调用的过E,d在调用时不必担心(j)数据是否?x)出错。不可重入函数在实时pȝ设计中被视ؓ(f)不安全函数。满下列条件的函数多数是不可重入的Q?span lang=EN-US><br>    1) </span>函数体内使用?jin)?rn)态的数据l构Q?span lang=EN-US><br>    2) </span>函数体内调用?span lang=EN-US>malloc()</span>或?span lang=EN-US>free()</span>函数Q?span lang=EN-US><br>    3) </span>函数体内调用?jin)标?span lang=EN-US>I/O</span>函数?span lang=EN-US><br><br>    </span>下面举例加以说明?span lang=EN-US><br>    A. </span>可重入函?span lang=EN-US><br>    void strcpy(char *lpszDest, char *lpszSrc)<o:p></o:p></span></span></p> <p class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 21pt; mso-char-indent-count: 2.0"><span lang=EN-US style="COLOR: black; FONT-FAMILY: ?hu)? mso-bidi-font-size: 10.5pt"><span style="mso-spacerun: yes"> </span>{<br>        while(*lpszDest++=*lpszSrc++);<br>        *dest=0;<br>    }<br><br>    B. </span><span style="COLOR: black; FONT-FAMILY: ?hu)? mso-bidi-font-size: 10.5pt">不可重入函数<span lang=EN-US>1<br>    charcTemp;//</span>全局变量<span lang=EN-US><br>    void SwapChar1(char *lpcX, char *lpcY)<o:p></o:p></span></span></p> <p class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 21pt; mso-char-indent-count: 2.0"><span lang=EN-US style="COLOR: black; FONT-FAMILY: ?hu)? mso-bidi-font-size: 10.5pt"><span style="mso-spacerun: yes"> </span>{<br>        cTemp=*lpcX;<br>        *lpcX=*lpcY;<br>        lpcY=cTemp;//</span><span style="COLOR: black; FONT-FAMILY: ?hu)? mso-bidi-font-size: 10.5pt">讉K?jin)全局变量<span lang=EN-US><br>    }<br><br>    C. </span>不可重入函数<span lang=EN-US>2<br>    void SwapChar2(char *lpcX,char *lpcY)<o:p></o:p></span></span></p> <p class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 21pt; mso-char-indent-count: 2.0"><span lang=EN-US style="COLOR: black; FONT-FAMILY: ?hu)? mso-bidi-font-size: 10.5pt"><span style="mso-spacerun: yes"> </span>{<br>        static char cTemp;//</span><span style="COLOR: black; FONT-FAMILY: ?hu)? mso-bidi-font-size: 10.5pt">?rn)态局部变?span lang=EN-US><br>        cTemp=*lpcX;<br>        *lpcX=*lpcY;<br>        lpcY=cTemp;//</span>使用?jin)?rn)态局部变?span lang=EN-US><br>    }<br><br>    </span>问题<span lang=EN-US>1</span>Q如何编写可重入的函敎ͼ<span lang=EN-US><br>    </span>{:(x)在函C内不讉K那些全局变量Q不使用?rn)态局部变量,坚持只用局部变量,写出的函数就是可重入的。如果必访问全局变量Q记住利用互斥信号量来保护全局变量?span lang=EN-US><br><br>    </span>问题<span lang=EN-US>2</span>Q如何将一个不可重入的函数改写成可重入的函敎ͼ<span lang=EN-US><br>    </span>{:(x)把一个不可重入函数变成可重入的唯一Ҏ(gu)是用可重入规则来重写它。其实很单,只要遵守?jin)几条很?gu)理解的规则,那么写出来的函数是可重入的?span lang=EN-US><br>    1) </span>不要使用全局变量。因为别的代码很可能覆盖q些变量倹{?span lang=EN-US><br>    2) </span>在和g发生交互的时候,切记执行cM<span lang=EN-US>disinterrupt()</span>之类的操作,是关闭g中断。完成交互记得打开中断Q在有些pd上,q叫?span lang=EN-US>“</span>q入<span lang=EN-US>/</span>退出核?span lang=EN-US>”</span>?span lang=EN-US><br>    3) </span>不能调用其它M不可重入的函数?span lang=EN-US><br>    4) </span>谨慎使用堆栈。最好先在用前?span lang=EN-US>OS_ENTER_KERNAL</span>?span lang=EN-US><br><br>    </span>堆栈操作涉及(qing)内存分配Q稍不留就?x)造成益出D覆盖其他d的数据,所以,误}慎用堆栈!最好别用!很多黑客E序利用了(jin)q一点以便系l执行非法代码从而轻松获得系l控制权。还有一些规则,MQ时刻记住一句话Q保证中断是安全的!<span lang=EN-US><br><br>    </span>实例问题Q曾l设计过如下一个函敎ͼ在代码检视的时候被提醒?span lang=EN-US>bug</span>Q因个函数是不可重入的,Z么?<span lang=EN-US><br>    unsigned int sum_int( unsigned int base ) <o:p></o:p></span></span></p> <p class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 21pt; mso-char-indent-count: 2.0"><span lang=EN-US style="COLOR: black; FONT-FAMILY: ?hu)? mso-bidi-font-size: 10.5pt">{<br>        unsigned int index;<br>        static unsigned int sum = 0; // </span><span style="COLOR: black; FONT-FAMILY: ?hu)? mso-bidi-font-size: 10.5pt">注意Q是<span lang=EN-US>static</span>cd<span lang=EN-US><br>        for (index = 1; index <= base; index++)<br>            sum += index;<br>        return sum;<br>    }<br><br>    </span>分析Q所谓的函数是可重入的(也可以说是可预测的)(j)Q即只要输入数据相同应产生相同的输出。这个函C所以是不可预测的,是因ؓ(f)函数中用了(jin)<span lang=EN-US>static</span>变量Q因?span lang=EN-US>static</span>变量的特征,q样的函数被UCؓ(f)Q带<span lang=EN-US>“</span>内部存储?span lang=EN-US>”</span>功能的的函数。因此如果需要一个可重入的函敎ͼ一定要避免函数中?span lang=EN-US>static</span>变量Q这U函C?span lang=EN-US>static</span>变量Q用原则是Q能不用量不用?span lang=EN-US><br>    </span>上面的函数修改为可重入的函敎ͼ只要声?span lang=EN-US>sum</span>变量中的<span lang=EN-US>static</span>关键字去掉,变量<span lang=EN-US>sum</span>卛_?sh)Z?span lang=EN-US>auto</span>cd的变量,函数卛_?sh)Z个可重入的函数?span lang=EN-US><br>    </span>当然Q有些时候,在函C是必要使用<span lang=EN-US>static</span>变量的,比如当某函数的返回gؓ(f)指针cdӞ则必L<span lang=EN-US>static</span>的局部变量的地址作ؓ(f)q回|若ؓ(f)<span lang=EN-US>auto</span>cdQ则q回为错指针?/span><span lang=EN-US style="FONT-FAMILY: ?hu)? mso-bidi-font-size: 10.5pt"><o:p></o:p></span></p> <img src ="http://www.shnenglu.com/franksunny/aggbug/29269.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.shnenglu.com/franksunny/" target="_blank">frank.sunny</a> 2007-08-03 12:56 <a href="http://www.shnenglu.com/franksunny/archive/2007/08/03/29269.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>也谈关于旉[转蝲time.h从头学]http://www.shnenglu.com/franksunny/archive/2007/04/05/21360.htmlfrank.sunnyfrank.sunnyThu, 05 Apr 2007 15:53:00 GMThttp://www.shnenglu.com/franksunny/archive/2007/04/05/21360.htmlhttp://www.shnenglu.com/franksunny/comments/21360.htmlhttp://www.shnenglu.com/franksunny/archive/2007/04/05/21360.html#Feedback0http://www.shnenglu.com/franksunny/comments/commentRss/21360.htmlhttp://www.shnenglu.com/franksunny/services/trackbacks/21360.html也谈关于旉

最q{L低层些的单片机程序编E,在一?span lang=EN-US>msp430上要增加一个国际标准时_(d)׃以前?span lang=EN-US>VC中都是拿来用的,没遇到问题,也就不会(x)LI。在单片Z想用标准C里面?span lang=EN-US>timeQ?span lang=EN-US>time_t*Q函数求得系l时_(d)最后结果出不来。后来才知道原来以前是取得的是操作系l的旉Q汗死,单片机没pȝ的啊Q希望能够尽早让我搞嵌入式啊Q呵c(din)?span lang=EN-US>

后来自己弄明白了(jin)Q设个时间|然后用单片机晶振累加计数Q还是可以用time.hL实现标准旉计时的,而且方便不用考虑自己d旉转换函数Q以下是具体?span lang=EN-US>time,h的讲解,我就不再展开?jin)?span lang=EN-US>

time.h从头?span lang=EN-US>

C/C++中对日期和时间操作所用到的数据结构和函数Qƈ对计时、时间的获取、时间的计算和显C格式等斚wq行?jin)阐q。本文还通过大量的实例向你展CZ(jin)time.h头文件中声明的各U函数和数据l构的详l用方法?span lang=EN-US>  

UTCQ世界标准时_(d)(j)Q?span lang=EN-US>Calendar TimeQ日历时_(d)(j)Q?span lang=EN-US>epochQ时间点Q,clock tickQ时钟计时单元)(j)  

    

C/C++中,对字W串的操作有很多值得注意的问题,同样Q?span lang=EN-US>C/C++Ҏ(gu)间的操作也有许多值得大家注意的地斏V最q,在技术群中有很多|友也多ơ问到过C++语言中对旉的操作、获取和昄{等的问题。下面,在这文章中Q笔者将主要介绍?span lang=EN-US>C/C++中时间和日期的用方?span lang=EN-US>.  

C/C++库,你可以有很多操作、用时间的Ҏ(gu)。但在这之前你需要了(jin)解一?span lang=EN-US>“旉?span lang=EN-US>“日期的概念,主要有以下几个:(x)  

Coordinated Universal TimeQ?span lang=EN-US>UTCQ:(x)协调世界Ӟ又称Z界标准时_(d)也就是大家所熟知的格林威L准时_(d)Greenwich Mean TimeQ?span lang=EN-US>GMTQ。比如,中国内地的时间与UTC的时差ؓ(f)+8Q也是UTC+8。美国是UTC-5?span lang=EN-US> 

Calendar TimeQ日历时_(d)是用从一个标准时间点到此时的旉l过的秒?span lang=EN-US>”来表C的旉。这个标准时间点对不同的~译器来说会(x)有所不同Q但对一个编译系l来_(d)q个标准旉Ҏ(gu)不变的,该编译系l中的时间对应的日历旉都通过该标准时间点来衡量,所以可以说日历旉?span lang=EN-US>“相对旉Q但是无Z在哪一个时区,在同一时刻对同一个标准时间点来说Q日历时间都是一L(fng)?span lang=EN-US>

epochQ时间点。时间点在标?span lang=EN-US>C/C++中是一个整敎ͼ它用此时的时间和标准旉点相差的U数Q即日历旉Q来表示?span lang=EN-US>

clock tickQ时钟计时单元(而不把它叫做旉滴答ơ数Q,一个时钟计时单元的旉长短是由CPU控制的。一?span lang=EN-US>clock tick不是CPU的一个时钟周期,而是C/C++的一个基本计时单位?span lang=EN-US>

ANSI标准库中?span lang=EN-US>time.h头文件。这个头文g中定义的旉和日期所使用的方法,无论是在l构定义Q还是命名,都具有明昄C语言风格。下面,我将说明?span lang=EN-US>C/C++中怎样使用日期的时间功能?span lang=EN-US>  

2Q?计时  

C/C++中的计时函数?span lang=EN-US>clock()Q而与其相关的数据cd?span lang=EN-US>clock_t。在MSDN中,查得?span lang=EN-US>clock函数定义如下Q?span lang=EN-US>  

开启这个程序进E?span lang=EN-US>”?span lang=EN-US>“E序中调?span lang=EN-US>clock()函数时之间的CPU旉计时单元Q?span lang=EN-US>clock tickQ数Q在MSDN中称之ؓ(f)挂钟旉Q?span lang=EN-US>wal-clockQ。其?span lang=EN-US>clock_t是用来保存时间的数据cdQ在time.h文g中,我们可以扑ֈ对它的定义:(x)  

typedef long clock_t;  
#define _CLOCK_T_DEFINED  
#endif  

clock_t是一个长整Ş数。在time.h文g中,q定义了(jin)一个常?span lang=EN-US>CLOCKS_PER_SECQ它用来表示一U钟?x)有多少个时钟计时单元,其定义如下?x)

1毫秒Q,调用clockQ)(j)函数q回的值就?span lang=EN-US>1。下面D个例子,你可以用公?span lang=EN-US>clock()/CLOCKS_PER_SEC来计一个进E自w的q行旉Q?span lang=EN-US>

{  
printf("Elapsed time:%u secs.\n",clock()/CLOCKS_PER_SEC);  
}  

clock函数来计你的机器运行一个@环或者处理其它事件到底花?jin)多时_(d)(x)  

#include “stdlib.h”  
#include “time.h”  

{  
long i = 10000000L;  
clock_t start, finish;  
double duration;  
/*
printf( "Time to do %ld empty loops is ", i );  
start = clock();  
while( i-- ) ;  
finish = clock();  
duration = (double)(finish - start) / CLOCKS_PER_SEC;  
printf( "%f seconds\n", duration );
system("pause");  
}  

1毫秒Q那么计时的_ֺ也ؓ(f)1毫秒Q那么我们可不可以通过改变CLOCKS_PER_SEC的定义,通过把它定义的大一些,从而计时_ֺ更高呢?通过试Q你?x)发现这h不行的。在标准C/C++中,最的计时单位是一毫秒?span lang=EN-US>

3Q与日期和时间相关的数据l构  

C/C++中,我们可通过tml构来获得日期和旉Q?span lang=EN-US>tml构?span lang=EN-US>time.h中的定义如下Q?span lang=EN-US>  

struct tm {  
int tm_sec; /*
取值区间ؓ(f)[0,59] */  
int tm_min; /* ?span lang=EN-US> - 取值区间ؓ(f)
[0,59] */  
int tm_hour; /* ?span lang=EN-US> - 取值区间ؓ(f)
[0,23] */  
int tm_mday; /* 一个月中的日期 - 取值区间ؓ(f)
[1,31] */  
int tm_mon; /* 月䆾Q从一月开始,0代表一月)(j) - 取值区间ؓ(f)
[0,11] */  
int tm_year; /* q䆾Q其值等于实际年份减?/font>1900 */  
int tm_wday; /* 星期取值区间ؓ(f)[0,6]Q其?span lang=EN-US>0代表星期天,1代表星期一Q以此类?/font> */  
int tm_yday; /* 从每q的1?span lang=EN-US>1?/st1:chsdate>开始的天数取值区间ؓ(f)[0,365]Q其?span lang=EN-US>0代表1?span lang=EN-US>1?/st1:chsdate>Q?span lang=EN-US>1代表1?span lang=EN-US>2?/st1:chsdate>Q以此类?/font> */  
int tm_isdst; /* 夏o(h)时标识符Q实行夏令时的时候,tm_isdst为正。不实行夏o(h)时的q候,tm_isdst?span lang=EN-US>0Q不?jin)解情况Ӟ?span lang=EN-US>tm_isdst()?/font>};  
#define _TM_DEFINED  
#endif  

ANSI C标准UC?span lang=EN-US>tml构的这U时间表CZؓ(f)分解旉(broken-down time)?span lang=EN-US>

Calendar TimeQ是通过time_t数据cd来表C的Q用time_t表示的时_(d)日历旉Q是从一个时间点Q例如:(x)1970q?span lang=EN-US>1?span lang=EN-US>1?span lang=EN-US>0?span lang=EN-US>0?span lang=EN-US>0U?/st1:chsdate>Q到此时的秒数。在time.h中,我们也可以看?span lang=EN-US>time_t是一个长整型敎ͼ(x)  

typedef long time_t; /* */  
#define _TIME_T_DEFINED /*
避免重复定义#endif  

time_t实际上是长整型,到未来的某一天,从一个时间点Q一般是1970q?span lang=EN-US>1?span lang=EN-US>1?span lang=EN-US>0?span lang=EN-US>0?span lang=EN-US>0U?/st1:chsdate>Q到那时的秒敎ͼx(chng)历时_(d)(j)出?jin)长整Ş所能表C的数的范围怎么办??span lang=EN-US>time_t数据cd的值来_(d)它所表示的时间不能晚?st1:chsdate w:st="on" Year="2038" Month="1" Day="18" IsLunarDate="False" IsROCDate="False">2038q?span lang=EN-US>1?span lang=EN-US>18?span lang=EN-US>19?span lang=EN-US>14?span lang=EN-US>07U?/st1:chsdate>。ؓ(f)?jin)能够表C更久远的时_(d)一些编译器厂商引入?span lang=EN-US>64位甚x(chng)长的整Ş数来保存日历旉。比如微软在Visual C++中采用了(jin)__time64_t数据cd来保存日历时_(d)q过_time64()函数来获得日历时_(d)而不是通过使用32位字?span lang=EN-US>time()函数Q,q样可以通过该数据类型保?st1:chsdate w:st="on" Year="3001" Month="1" Day="1" IsLunarDate="False" IsROCDate="False">3001q?span lang=EN-US>1?span lang=EN-US>1?span lang=EN-US>0?span lang=EN-US>0?span lang=EN-US>0U?/st1:chsdate>Q不包括该时间点Q之前的旉?span lang=EN-US>  

time.h头文件中Q我们还可以看到一些函敎ͼ它们都是?span lang=EN-US>time_t为参数类型或q回值类型的函数Q?span lang=EN-US>

time.hq提供了(jin)两种不同的函数将日历旉Q一个用time_t表示的整敎ͼ(j)转换为我们^时看到的把年月日时分U分开昄的时间格?span lang=EN-US>tmQ?span lang=EN-US>

MSDNQ我们可以知?span lang=EN-US>Microsoft C/C++ 7.0中时间点的|time_t对象的|(j)是从1899q?span lang=EN-US>12?span lang=EN-US>31?span lang=EN-US>0?span lang=EN-US>0?span lang=EN-US>0U?/st1:chsdate>到该旉Ҏ(gu)l过的秒敎ͼ而其它各U版本的Microsoft C/C++和所有不同版本的Visual C++都是计算的从1970q?span lang=EN-US>1?span lang=EN-US>1?span lang=EN-US>0?span lang=EN-US>0?span lang=EN-US>0U?/st1:chsdate>到该旉Ҏ(gu)l过的秒数?span lang=EN-US>  

4Q与日期和时间相关的函数?qing)应?span lang=EN-US>

time.h中声明的函数Ҏ(gu)间进行操作。这些操作包括取当前旉、计时间间隔、以不同的Ş式显C时间等内容?span lang=EN-US>  

4.1 获得日历旉

time()函数来获得日历时_(d)Calendar TimeQ,其原型ؓ(f)Q?span lang=EN-US>

timerQ你可以从参?span lang=EN-US>timerq回现在的日历时_(d)同时也可以通过q回D回现在的日历旉Q即从一个时间点Q例如:(x)1970q?span lang=EN-US>1?span lang=EN-US>1?span lang=EN-US>0?span lang=EN-US>0?span lang=EN-US>0U?/st1:chsdate>Q到现在此时的秒数。如果参Cؓ(f)I(NULQ,函数只通过q回D回现在的日历旉Q比如下面这个例子用来显C当前的日历旉Q?span lang=EN-US>

#include "stdio.h"  
int main(void)  
{  
struct tm *ptr;  
time_t lt;  
lt =time(NUL);  
printf("The Calendar Time now is %d\n",lt);  
return 0;  
}  

  

1122707619是我运行程序时的日历时间。即?st1:chsdate w:st="on" Year="1970" Month="1" Day="1" IsLunarDate="False" IsROCDate="False">1970q?span lang=EN-US>1?span lang=EN-US>1?span lang=EN-US>0?span lang=EN-US>0?span lang=EN-US>0U?/st1:chsdate>到此时的U数?span lang=EN-US>  

4.2 获得日期和时?span lang=EN-US>  

2节我们已l知道这些信息都保存在一个名?span lang=EN-US>tm的结构体中,那么如何一个日历时间保存(sh)ؓ(f)一?span lang=EN-US>tml构的对象呢Q?span lang=EN-US>  

gmtime()?span lang=EN-US>localtime()Q这两个函数的原型ؓ(f)Q?span lang=EN-US>  

struct tm * localtime(const time_t * timer);  

gmtime()函数是将日历旉转化Z界标准时_(d)x(chng)林尼L_(d)(j)Qƈq回一?span lang=EN-US>tml构体来保存q个旉Q?span lang=EN-US>localtime()函数是将日历旉转化为本地时间。比如现在用gmtime()函数获得的世界标准时间是2005q?span lang=EN-US>7?span lang=EN-US>30?span lang=EN-US>7?span lang=EN-US>18?span lang=EN-US>20U?/st1:chsdate>Q那么我?span lang=EN-US>localtime()函数在中国地得的本地旉?x)比世界标准旉?span lang=EN-US>8个小Ӟ?st1:chsdate w:st="on" Year="2005" Month="7" Day="30" IsLunarDate="False" IsROCDate="False">2005q?span lang=EN-US>7?span lang=EN-US>30?span lang=EN-US>15?span lang=EN-US>18?span lang=EN-US>20U?/st1:chsdate>。下面是个例子:(x)  

#include "stdio.h"  
int main(void)  
{  
struct tm *local;  
time_t t;  
t=time(NUL);  
local=localtime(&t);  
printf("Local hour is: %d\n",local->tm_hour);  
local=gmtime(&t);  
printf("UTC hour is: %d\n",local->tm_hour);  
return 0;  
}  

  

UTC hour is: 7  

4.3 固定的时间格?span lang=EN-US>  

asctime()函数?span lang=EN-US>ctime()函数时间以固定的格式显C出来,两者的q回值都?span lang=EN-US>char*型的字符丌Ӏ返回的旉格式为:(x)  

:?span lang=EN-US>:U?q?/font>\n\0  
例如Q?span lang=EN-US>Wed Jan 02 02:03:55 1980\n\0  

\n是一个换行符Q?span lang=EN-US>\0是一个空字符Q表C字W串l束。下面是两个函数的原型:(x)  

char * ctime(const time_t *timer);  

asctime()函数是通过tml构来生成具有固定格式的保存旉信息的字W串Q?span lang=EN-US>ctime()是通过日历旉来生成时间字W串。这L(fng)话,asctimeQ)(j)函数只是?span lang=EN-US>tml构对象中的各个域填到时间字W串的相应位|就行了(jin)Q?span lang=EN-US>ctimeQ)(j)函数需要先参照本地的时间设|,把日历时间{化ؓ(f)本地旉Q然后再生成格式化后的字W串。在下面Q如?span lang=EN-US>t是一个非I的time_t变量的话Q那么:(x)  

  

ptr=localtime(&t);  
printf(asctime(ptr));  

printf语句输出的结果就是不同的?jin)(除非你将本地时区设?f)世界标准旉所在的时区Q:(x)  

#include "stdio.h"  
int main(void)  
{  
struct tm *ptr;  
time_t lt;  
lt =time(NUL);  
ptr=gmtime(<);  
printf(asctime(ptr));  
printf(ctime(<));  
return 0;  
}  

  

Sat Jul 30 16:43:03 2005  

4.4 自定义时间格?span lang=EN-US>  

strftimeQ)(j)函数时间格式化为我们想要的格式。它的原型如下:(x)  

size_t strftime(  
char *strDest,  
size_t maxsize,  
const char *format,  
const struct tm *timeptr  
);
我们可以Ҏ(gu)format指向字符串中格式命o(h)?span lang=EN-US>timeptr中保存的旉信息攑֜strDest指向的字W串中,最多向strDest中存?span lang=EN-US>maxsize个字W。该函数q回?span lang=EN-US>strDest指向的字W串中放|的字符数?span lang=EN-US>  

strftime()的操作有些类gsprintf()Q识别以癑ֈ?span lang=EN-US>(%)开始的格式命o(h)集合Q格式化输出l果攑֜一个字W串中。格式化命o(h)说明?span lang=EN-US>strDest中各U日期和旉信息的确切表C方法。格式串中的其他字符原样放进串中。格式命令列在下面,它们是区分大写的?span lang=EN-US>  

  
%A
星期几的全称  
%b 月分的简?/font>  
%B 月䆾的全U?/font>  
%c 标准的日期的旉?/font>  
%C q䆾的后两位数字
  
%d 十进制表C的每月的第几天
  
%D ?span lang=EN-US>/?span lang=EN-US>/q?/font>  
%e 在两字符域中Q十q制表示的每月的W几?/font>  
%F q?span lang=EN-US>-?span lang=EN-US>-?/font>  
%g q䆾的后两位数字Q用基于周的年
  
%G q分Q用基于周的年
  
%h 写的月䆾?/font>  
%H 24时制的时
  
%I 12时制的时
  
%j 十进制表C的每年的第几天
  
%m 十进制表C的月䆾
  
%M 十时制表C的分钟?/font>  
%n 新行W?/font>  
%p 本地?span lang=EN-US>AM?span lang=EN-US>PM的等hC?/font>  
%r 12时的时?/font>  
%R 昄时和分钟:(x)
hh:mm  
%S 十进制的U数
  
%t 水^制表W?/font>  
%T 昄时分U:(x)
hh:mm:ss  
%u 每周的第几天Q星期一为第一?Qg0?span lang=EN-US>6Q星期一?span lang=EN-US>0Q?/font>  
%U W年的第几周Q把星期日做为第一天(g0?span lang=EN-US>53Q?/font>  
%V 每年的第几周Q用基于周的年
  
%w 十进制表C的星期几(g0?span lang=EN-US>6Q星期天?span lang=EN-US>0Q?/font>  
%W 每年的第几周Q把星期一做ؓ(f)W一天(g0?span lang=EN-US>53Q?/font>  
%x 标准的日期串
  
%X 标准的时间串
  
%y 不带世纪的十q制q䆾Qg0?span lang=EN-US>99Q?/font>  
%Y 带世U部分的十进制年?/font>  
%zQ?span lang=EN-US>%Z 时区名称Q如果不能得到时区名U则q回I字W?/font>  
%% 癑ֈ?span lang=EN-US>  

12时制显C,p下面q段E序Q?span lang=EN-US>  

#include “stdio.h”  
int main(void)  
{  
struct tm *ptr;  
time_t lt;  
char str[80];  
lt=time(NUL);  
ptr=localtime(<);  
strftime(str,100,"It is now %I %p",ptr);  
printf(str);  
return 0;  
}  

It is now 4PM  

  

#include <time.h>  

{  
struct tm *newtime;  
char tmpbuf[128];  
time_t lt1;  
time( <1 );  
newtime=localtime(<1);  
strftime( tmpbuf, 128, "Today is %A, day %d of %B in the year %Y.\n", newtime);  
printf(tmpbuf);  
}  

  

4.5 计算持箋(hu)旉的长?span lang=EN-US>  

1节计旉分中Q我已经?span lang=EN-US>clock函数举了(jin)一个例子?span lang=EN-US>Clock()函数可以_到毫U。同Ӟ我们也可以?span lang=EN-US>difftime()函数Q但它只能精到U。该函数的定义如下:(x)  

doublecd的,但这q不说明该时间具有同double一L(fng)_度,q是由它的参数觉得的Q?span lang=EN-US>time_t是以Uؓ(f)单位计算的)(j)。比如下面一D늨序:(x)  

#include "stdio.h"  
#include "stdlib.h"  
int main(void)  
{  
time_t start,end;  
start = time(NUL);  
system("pause");  
end = time(NUL);  
printf("The pause used %f seconds.\n",difftime(end,start));//<-  
system("pause");  
return 0;  
}  

  
hL键l?/font>. . .  
The pause used 2.000000 seconds.  
hL键l?span lang=EN-US>. . .  

2U钟。其实,你将上面E序的带?span lang=EN-US>“//<-”注释的一行用下面的一行代码替换:(x)  

  

4.6 分解旉转化为日历时?span lang=EN-US>  

C/C++中是tml构。我们可以?span lang=EN-US>mktimeQ)(j)函数用tml构表示的时间{化ؓ(f)日历旉。其函数原型如下Q?span lang=EN-US>  

1997q?span lang=EN-US>7?span lang=EN-US>1?/st1:chsdate>是星期几Q?span lang=EN-US>  

#include "stdio.h"  
#include "stdlib.h"  
int main(void)  
{  
struct tm t;  
time_t t_of_day;  
t.tm_year=1997-1900;  
t.tm_mon=6;  
t.tm_mday=1;  
t.tm_hour=0;  
t.tm_min=0;  
t.tm_sec=1;  
t.tm_isdst=0;  
t_of_day=mktime(&t);  
printf(ctime(&t_of_day));  
return 0;  
}  
  
Tue Jul 01 00:00:01 1997  
现在注意?jin),有?jin)mktime()函数Q是不是我们可以操作现在之前的Q何时间呢Q你可以通过q种办法出1945q?span lang=EN-US>8?span lang=EN-US>15h星期几吗Q答案是否定的。因个时间在1970q?span lang=EN-US>1?span lang=EN-US>1?/st1:chsdate>之前Q所以在大多数编译器中,q样的程序虽然可以编译通过Q但q行时会(x)异常l止?span lang=EN-US>



frank.sunny 2007-04-05 23:53 发表评论
]]>
由switch选择l构理解局部变?/title><link>http://www.shnenglu.com/franksunny/archive/2007/04/05/21358.html</link><dc:creator>frank.sunny</dc:creator><author>frank.sunny</author><pubDate>Thu, 05 Apr 2007 15:25:00 GMT</pubDate><guid>http://www.shnenglu.com/franksunny/archive/2007/04/05/21358.html</guid><wfw:comment>http://www.shnenglu.com/franksunny/comments/21358.html</wfw:comment><comments>http://www.shnenglu.com/franksunny/archive/2007/04/05/21358.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.shnenglu.com/franksunny/comments/commentRss/21358.html</wfw:commentRss><trackback:ping>http://www.shnenglu.com/franksunny/services/trackbacks/21358.html</trackback:ping><description><![CDATA[  <span> <p class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: center" align=center><span style="FONT-SIZE: 14pt; FONT-FAMILY: ?hu)? mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">?/span><span lang=EN-US style="FONT-SIZE: 14pt">switch</span><span style="FONT-SIZE: 14pt; FONT-FAMILY: ?hu)? mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">选择l构理解局部变?/span><span lang=EN-US style="FONT-SIZE: 14pt"><o:p></o:p></span></p> <p class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><span lang=EN-US><o:p> </o:p></span></p> <p class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 21pt; mso-char-indent-count: 2.0"><span style="FONT-FAMILY: ?hu)? mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">函数体内部自定义变量Q称为局部变量,存储于栈Q?/span><span lang=EN-US>stack</span><span style="FONT-FAMILY: ?hu)? mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">Q中Q由~译器自动分配和释放Q局部变量的生存期(或者说作用域)(j)是当前函数内部,使用时必d始化Q否则其值将不定。以前对局部变量的定义也就是这么多Q而且也就那么在用。近期碰到如下一个问题:(x)</span></p> <p class=MsoNormal style="BACKGROUND: #cccccc; MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 24.1pt; mso-char-indent-count: 2.0"><strong style="mso-bidi-font-weight: normal"><span lang=EN-US style="FONT-SIZE: 12pt; COLOR: #333333">void func( void ) <o:p></o:p></span></strong></p> <p class=MsoNormal style="BACKGROUND: #cccccc; MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 24.1pt; mso-char-indent-count: 2.0"><strong style="mso-bidi-font-weight: normal"><span lang=EN-US style="FONT-SIZE: 12pt; COLOR: #333333">{ <o:p></o:p></span></strong></p> <p class=MsoNormal style="BACKGROUND: #cccccc; MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 24.1pt; mso-char-indent-count: 2.0"><strong style="mso-bidi-font-weight: normal"><span lang=EN-US style="FONT-SIZE: 12pt; COLOR: #333333"><span style="mso-spacerun: yes">   </span>int x = 2; <o:p></o:p></span></strong></p> <p class=MsoNormal style="BACKGROUND: #cccccc; MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 24.1pt; mso-char-indent-count: 2.0"><strong style="mso-bidi-font-weight: normal"><span lang=EN-US style="FONT-SIZE: 12pt; COLOR: #333333"><span style="mso-spacerun: yes">   </span>switch ( x ) <o:p></o:p></span></strong></p> <p class=MsoNormal style="BACKGROUND: #cccccc; MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 24.1pt; mso-char-indent-count: 2.0"><strong style="mso-bidi-font-weight: normal"><span lang=EN-US style="FONT-SIZE: 12pt; COLOR: #333333"><span style="mso-spacerun: yes">   </span>{<o:p></o:p></span></strong></p> <p class=MsoNormal style="BACKGROUND: #cccccc; MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 24.1pt; mso-char-indent-count: 2.0"><strong style="mso-bidi-font-weight: normal"><span lang=EN-US style="FONT-SIZE: 12pt; COLOR: #333333"><span style="mso-tab-count: 1">      </span><span style="mso-spacerun: yes">   </span>int m =0;<span style="mso-tab-count: 1">       </span>//initialization skipped by case0,case1,case2,default <o:p></o:p></span></strong></p> <p class=MsoNormal style="BACKGROUND: #cccccc; MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 24.1pt; mso-char-indent-count: 2.0"><strong style="mso-bidi-font-weight: normal"><span lang=EN-US style="FONT-SIZE: 12pt; COLOR: #333333"><span style="mso-spacerun: yes">   </span>case 0 :<o:p></o:p></span></strong></p> <p class=MsoNormal style="BACKGROUND: #cccccc; MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 24.1pt; mso-char-indent-count: 2.0"><strong style="mso-bidi-font-weight: normal"><span lang=EN-US style="FONT-SIZE: 12pt; COLOR: #333333"><span style="mso-tab-count: 1">      </span><span style="mso-spacerun: yes">   </span>int i = 0;<span style="mso-tab-count: 2">        </span>//initialization skipped by case1,case2,default <o:p></o:p></span></strong></p> <p class=MsoNormal style="BACKGROUND: #cccccc; MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 24.1pt; mso-char-indent-count: 2.0"><strong style="mso-bidi-font-weight: normal"><span lang=EN-US style="FONT-SIZE: 12pt; COLOR: #333333"><span style="mso-tab-count: 1">      </span><span style="mso-spacerun: yes">   </span>{ int j = 1; }<span style="mso-spacerun: yes">   </span>// OK, initialized in enclosing block<o:p></o:p></span></strong></p> <p class=MsoNormal style="BACKGROUND: #cccccc; MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 24.1pt; mso-char-indent-count: 2.0"><strong style="mso-bidi-font-weight: normal"><span lang=EN-US style="FONT-SIZE: 12pt; COLOR: #333333"><span style="mso-tab-count: 1">      </span><span style="mso-spacerun: yes">   </span>break;<o:p></o:p></span></strong></p> <p class=MsoNormal style="BACKGROUND: #cccccc; MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 24.1pt; mso-char-indent-count: 2.0"><strong style="mso-bidi-font-weight: normal"><span lang=EN-US style="FONT-SIZE: 12pt; COLOR: #333333"><span style="mso-spacerun: yes">   </span>case 1 : <o:p></o:p></span></strong></p> <p class=MsoNormal style="BACKGROUND: #cccccc; MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 24.1pt; mso-char-indent-count: 2.0"><strong style="mso-bidi-font-weight: normal"><span lang=EN-US style="FONT-SIZE: 12pt; COLOR: #333333"><span style="mso-tab-count: 1">      </span><span style="mso-spacerun: yes">   </span>break;<o:p></o:p></span></strong></p> <p class=MsoNormal style="BACKGROUND: #cccccc; MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 24.1pt; mso-char-indent-count: 2.0"><strong style="mso-bidi-font-weight: normal"><span lang=EN-US style="FONT-SIZE: 12pt; COLOR: #333333"><span style="mso-spacerun: yes">   </span>case 2:<o:p></o:p></span></strong></p> <p class=MsoNormal style="BACKGROUND: #cccccc; MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 24.1pt; mso-char-indent-count: 2.0"><strong style="mso-bidi-font-weight: normal"><span lang=EN-US style="FONT-SIZE: 12pt; COLOR: #333333"><span style="mso-tab-count: 1">      </span><span style="mso-spacerun: yes">   </span>break;<o:p></o:p></span></strong></p> <p class=MsoNormal style="BACKGROUND: #cccccc; MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 24.1pt; mso-char-indent-count: 2.0"><strong style="mso-bidi-font-weight: normal"><span lang=EN-US style="FONT-SIZE: 12pt; COLOR: #333333"><span style="mso-spacerun: yes">   </span>default:<o:p></o:p></span></strong></p> <p class=MsoNormal style="BACKGROUND: #cccccc; MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 24.1pt; mso-char-indent-count: 2.0"><strong style="mso-bidi-font-weight: normal"><span lang=EN-US style="FONT-SIZE: 12pt; COLOR: #333333"><span style="mso-tab-count: 1">      </span><span style="mso-spacerun: yes">   </span>int k = 1;<span style="mso-tab-count: 2">              </span>// OK, initialization not skipped<o:p></o:p></span></strong></p> <p class=MsoNormal style="BACKGROUND: #cccccc; MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 24.1pt; mso-char-indent-count: 2.0"><strong style="mso-bidi-font-weight: normal"><span lang=EN-US style="FONT-SIZE: 12pt; COLOR: #333333"><span style="mso-spacerun: yes">   </span>} <o:p></o:p></span></strong></p> <p class=MsoNormal style="BACKGROUND: #cccccc; MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 24.1pt; mso-char-indent-count: 2.0"><strong style="mso-bidi-font-weight: normal"><span lang=EN-US style="FONT-SIZE: 12pt; COLOR: #333333">}<o:p></o:p></span></strong></p> <p class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 21pt; mso-char-indent-count: 2.0"><span style="FONT-FAMILY: ?hu)? mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">遇到q个问题Q网上的解答很多Q很多h觉得</span><span lang=EN-US>switch</span><span style="FONT-FAMILY: ?hu)? mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">内不能定义局部变量,q个明显是不对的。因为我把代码改成以下Ş式后完全可以用?jin)?/span></p> <p class=MsoNormal style="BACKGROUND: #cccccc; MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 24.1pt; mso-char-indent-count: 2.0"><strong style="mso-bidi-font-weight: normal"><span lang=EN-US style="FONT-SIZE: 12pt">void func( void ) <o:p></o:p></span></strong></p> <p class=MsoNormal style="BACKGROUND: #cccccc; MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 24.1pt; mso-char-indent-count: 2.0"><strong style="mso-bidi-font-weight: normal"><span lang=EN-US style="FONT-SIZE: 12pt">{ <o:p></o:p></span></strong></p> <p class=MsoNormal style="BACKGROUND: #cccccc; MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 24.1pt; mso-char-indent-count: 2.0"><strong style="mso-bidi-font-weight: normal"><span lang=EN-US style="FONT-SIZE: 12pt"><span style="mso-spacerun: yes">   </span>int x = 2; <o:p></o:p></span></strong></p> <p class=MsoNormal style="BACKGROUND: #cccccc; MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 24.1pt; mso-char-indent-count: 2.0"><strong style="mso-bidi-font-weight: normal"><span lang=EN-US style="FONT-SIZE: 12pt"><span style="mso-spacerun: yes">   </span>switch ( x ) <o:p></o:p></span></strong></p> <p class=MsoNormal style="BACKGROUND: #cccccc; MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 24.1pt; mso-char-indent-count: 2.0"><strong style="mso-bidi-font-weight: normal"><span lang=EN-US style="FONT-SIZE: 12pt"><span style="mso-spacerun: yes">   </span>{<o:p></o:p></span></strong></p> <p class=MsoNormal style="BACKGROUND: #cccccc; MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 24.1pt; mso-char-indent-count: 2.0"><strong style="mso-bidi-font-weight: normal"><span lang=EN-US style="FONT-SIZE: 12pt"><span style="mso-tab-count: 1">      </span><span style="mso-spacerun: yes">   </span>int m;<o:p></o:p></span></strong></p> <p class=MsoNormal style="BACKGROUND: #cccccc; MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 24.1pt; mso-char-indent-count: 2.0"><strong style="mso-bidi-font-weight: normal"><span lang=EN-US style="FONT-SIZE: 12pt"><span style="mso-tab-count: 1">      </span><span style="mso-spacerun: yes">   </span>m = 0;<span style="mso-tab-count: 3">                   </span>//without execute; <o:p></o:p></span></strong></p> <p class=MsoNormal style="BACKGROUND: #cccccc; MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 24.1pt; mso-char-indent-count: 2.0"><strong style="mso-bidi-font-weight: normal"><span lang=EN-US style="FONT-SIZE: 12pt"><span style="mso-spacerun: yes">   </span>case 0:<o:p></o:p></span></strong></p> <p class=MsoNormal style="BACKGROUND: #cccccc; MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 24.1pt; mso-char-indent-count: 2.0"><strong style="mso-bidi-font-weight: normal"><span lang=EN-US style="FONT-SIZE: 12pt"><span style="mso-tab-count: 1">      </span><span style="mso-spacerun: yes">   </span>int i;<o:p></o:p></span></strong></p> <p class=MsoNormal style="BACKGROUND: #cccccc; MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 24.1pt; mso-char-indent-count: 2.0"><strong style="mso-bidi-font-weight: normal"><span lang=EN-US style="FONT-SIZE: 12pt"><span style="mso-tab-count: 1">      </span><span style="mso-spacerun: yes">   </span>i = 0; <o:p></o:p></span></strong></p> <p class=MsoNormal style="BACKGROUND: #cccccc; MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 24.1pt; mso-char-indent-count: 2.0"><strong style="mso-bidi-font-weight: normal"><span lang=EN-US style="FONT-SIZE: 12pt"><span style="mso-tab-count: 1">      </span><span style="mso-spacerun: yes">   </span>{ int j = 1; }<span style="mso-spacerun: yes">   </span>// OK, initialized in enclosing block <o:p></o:p></span></strong></p> <p class=MsoNormal style="BACKGROUND: #cccccc; MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 24.1pt; mso-char-indent-count: 2.0"><strong style="mso-bidi-font-weight: normal"><span lang=EN-US style="FONT-SIZE: 12pt"><span style="mso-tab-count: 1">      </span><span style="mso-spacerun: yes">   </span>printf("%d<span style="mso-tab-count: 1">    </span>%d\n", m, i);<o:p></o:p></span></strong></p> <p class=MsoNormal style="BACKGROUND: #cccccc; MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 24.1pt; mso-char-indent-count: 2.0"><strong style="mso-bidi-font-weight: normal"><span lang=EN-US style="FONT-SIZE: 12pt"><span style="mso-tab-count: 1">      </span><span style="mso-spacerun: yes">   </span>break;<o:p></o:p></span></strong></p> <p class=MsoNormal style="BACKGROUND: #cccccc; MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 24.1pt; mso-char-indent-count: 2.0"><strong style="mso-bidi-font-weight: normal"><span lang=EN-US style="FONT-SIZE: 12pt"><span style="mso-spacerun: yes">   </span>case 1: <o:p></o:p></span></strong></p> <p class=MsoNormal style="BACKGROUND: #cccccc; MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 24.1pt; mso-char-indent-count: 2.0"><strong style="mso-bidi-font-weight: normal"><span lang=EN-US style="FONT-SIZE: 12pt"><span style="mso-tab-count: 1">      </span><span style="mso-spacerun: yes">   </span>i = 1;<o:p></o:p></span></strong></p> <p class=MsoNormal style="BACKGROUND: #cccccc; MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 24.1pt; mso-char-indent-count: 2.0"><strong style="mso-bidi-font-weight: normal"><span lang=EN-US style="FONT-SIZE: 12pt"><span style="mso-tab-count: 1">      </span><span style="mso-spacerun: yes">   </span>printf("%d<span style="mso-tab-count: 1">    </span>%d\n", m, i);<o:p></o:p></span></strong></p> <p class=MsoNormal style="BACKGROUND: #cccccc; MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 24.1pt; mso-char-indent-count: 2.0"><strong style="mso-bidi-font-weight: normal"><span lang=EN-US style="FONT-SIZE: 12pt"><span style="mso-tab-count: 1">      </span><span style="mso-spacerun: yes">   </span>break;<o:p></o:p></span></strong></p> <p class=MsoNormal style="BACKGROUND: #cccccc; MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 24.1pt; mso-char-indent-count: 2.0"><strong style="mso-bidi-font-weight: normal"><span lang=EN-US style="FONT-SIZE: 12pt"><span style="mso-spacerun: yes">   </span>case 2:<o:p></o:p></span></strong></p> <p class=MsoNormal style="BACKGROUND: #cccccc; MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 24.1pt; mso-char-indent-count: 2.0"><strong style="mso-bidi-font-weight: normal"><span lang=EN-US style="FONT-SIZE: 12pt"><span style="mso-tab-count: 1">      </span><span style="mso-spacerun: yes">   </span>i = 2;<o:p></o:p></span></strong></p> <p class=MsoNormal style="BACKGROUND: #cccccc; MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 24.1pt; mso-char-indent-count: 2.0"><strong style="mso-bidi-font-weight: normal"><span lang=EN-US style="FONT-SIZE: 12pt"><span style="mso-tab-count: 1">      </span><span style="mso-spacerun: yes">   </span>printf("%d<span style="mso-tab-count: 1">    </span>%d\n", m, i);<o:p></o:p></span></strong></p> <p class=MsoNormal style="BACKGROUND: #cccccc; MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 24.1pt; mso-char-indent-count: 2.0"><strong style="mso-bidi-font-weight: normal"><span lang=EN-US style="FONT-SIZE: 12pt"><span style="mso-tab-count: 1">      </span><span style="mso-spacerun: yes">   </span>break;<o:p></o:p></span></strong></p> <p class=MsoNormal style="BACKGROUND: #cccccc; MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 24.1pt; mso-char-indent-count: 2.0"><strong style="mso-bidi-font-weight: normal"><span lang=EN-US style="FONT-SIZE: 12pt"><span style="mso-spacerun: yes">   </span>default:<o:p></o:p></span></strong></p> <p class=MsoNormal style="BACKGROUND: #cccccc; MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 24.1pt; mso-char-indent-count: 2.0"><strong style="mso-bidi-font-weight: normal"><span lang=EN-US style="FONT-SIZE: 12pt"><span style="mso-tab-count: 1">      </span><span style="mso-spacerun: yes">   </span>int k = 1;<span style="mso-tab-count: 2">              </span>// OK, initialization not skipped<o:p></o:p></span></strong></p> <p class=MsoNormal style="BACKGROUND: #cccccc; MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 24.1pt; mso-char-indent-count: 2.0"><strong style="mso-bidi-font-weight: normal"><span lang=EN-US style="FONT-SIZE: 12pt"><span style="mso-spacerun: yes">   </span>} <o:p></o:p></span></strong></p> <p class=MsoNormal style="BACKGROUND: #cccccc; MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 24.1pt; mso-char-indent-count: 2.0"><strong style="mso-bidi-font-weight: normal"><span lang=EN-US style="FONT-SIZE: 12pt">}<o:p></o:p></span></strong></p> <p class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 21pt; mso-char-indent-count: 2.0"><span style="FONT-FAMILY: ?hu)? mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">~译时有一?/span><span lang=EN-US>warning</span><span style="FONT-FAMILY: ?hu)? mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">Q即“</span><span lang=EN-US>local variable 'm' used without having been initialized</span><span style="FONT-FAMILY: ?hu)? mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">”Q执行结果ؓ(f)Q?/span><span lang=EN-US>-858993460<span style="mso-tab-count: 1">       </span>2</span></p> <p class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 21pt; mso-char-indent-count: 2.0"><span style="FONT-FAMILY: ?hu)? mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">因此</span><span lang=EN-US>switch</span><span style="FONT-FAMILY: ?hu)? mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">内不但可以定义变量,而且也不用像很多人所说的?/span><span lang=EN-US>case</span><span style="FONT-FAMILY: ?hu)? mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">内遇到要用变量时一定要?/span><span lang=EN-US>{}</span><span style="FONT-FAMILY: ?hu)? mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">括v来,不过严格的说不用</span><span lang=EN-US>{}</span><span style="FONT-FAMILY: ?hu)? mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">扩v来的变量是是属于整个</span><span lang=EN-US>switch</span><span style="FONT-FAMILY: ?hu)? mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">块结构的Qؓ(f)此编E一定要新增变量作用域限定?/span><span lang=EN-US>case</span><span style="FONT-FAMILY: ?hu)? mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">内就必须要用</span><span lang=EN-US>{}</span><span style="FONT-FAMILY: ?hu)? mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">?/span></p> <p class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 21pt; mso-char-indent-count: 2.0"><span style="FONT-FAMILY: ?hu)? mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">通过</span><span lang=EN-US>switch</span><span style="FONT-FAMILY: ?hu)? mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">···</span><span lang=EN-US>case</span><span style="FONT-FAMILY: ?hu)? mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">l构Q对局部变量的声明、定义以?qing)初始化{概念可以有一个比较清晰的认识。我的理解就是:(x)声明语句不管是放在哪里,其编译时都是其|顶到块的头部,?/span><span lang=EN-US>int k</span><span style="FONT-FAMILY: ?hu)? mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">虽然?/span><span lang=EN-US>default</span><span style="FONT-FAMILY: ?hu)? mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">中,但是q个变量的声明就?/span><span lang=EN-US>switch</span><span style="FONT-FAMILY: ?hu)? mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">?/span><span lang=EN-US>{}</span><span style="FONT-FAMILY: ?hu)? mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">内,其生存期与变?/span><span lang=EN-US>m</span><span style="FONT-FAMILY: ?hu)? mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">{同Q只是由于前面没有声明,所?/span><span lang=EN-US>default</span><span style="FONT-FAMILY: ?hu)? mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">之前不能用?/span></p> <p align=center></span></p> <img src ="http://www.shnenglu.com/franksunny/aggbug/21358.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.shnenglu.com/franksunny/" target="_blank">frank.sunny</a> 2007-04-05 23:25 <a href="http://www.shnenglu.com/franksunny/archive/2007/04/05/21358.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>自己实现Stringc(|讯W试归来Q?/title><link>http://www.shnenglu.com/franksunny/archive/2007/03/04/19199.html</link><dc:creator>frank.sunny</dc:creator><author>frank.sunny</author><pubDate>Sun, 04 Mar 2007 13:53:00 GMT</pubDate><guid>http://www.shnenglu.com/franksunny/archive/2007/03/04/19199.html</guid><wfw:comment>http://www.shnenglu.com/franksunny/comments/19199.html</wfw:comment><comments>http://www.shnenglu.com/franksunny/archive/2007/03/04/19199.html#Feedback</comments><slash:comments>10</slash:comments><wfw:commentRss>http://www.shnenglu.com/franksunny/comments/commentRss/19199.html</wfw:commentRss><trackback:ping>http://www.shnenglu.com/franksunny/services/trackbacks/19199.html</trackback:ping><description><![CDATA[<p class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: center" align=center><strong style="mso-bidi-font-weight: normal"><span style="FONT-SIZE: 14pt; FONT-FAMILY: ?hu)? mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">|讯W试归来</span> </strong><strong style="mso-bidi-font-weight: normal"><span lang=EN-US style="FONT-SIZE: 14pt"><o:p></o:p></span></strong></p> <p class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 21pt; mso-char-indent-count: 2.0"><span style="FONT-FAMILY: ?hu)? mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">昨天ȝ讯(杭州Q笔试了(jin)Q做?jin)下W试题,感觉题目都不难,但是自己做的的确不怎么P估计是没Z(x)M(jin)Q不q暂时还是先把几道自p记得的题目,写出来,ȝ下,以做复习(fn)?/span> </p> <p class=MsoNormal style="MARGIN: 0cm 0cm 0pt 18pt; TEXT-INDENT: -18pt; mso-list: l0 level1 lfo1; tab-stops: list 18.0pt"><span lang=EN-US style="mso-fareast-font-family: 'Times New Roman'"><span style="mso-list: Ignore">1?span style="FONT: 7pt 'Times New Roman'">  </span></span></span><span style="FONT-FAMILY: ?hu)? mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">要求自己实现</span> <span lang=EN-US>String</span> <span style="FONT-FAMILY: ?hu)? mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">c,l出?/span> <span lang=EN-US>String</span> <span style="FONT-FAMILY: ?hu)? mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">cȝ以下头文件类声明</span> </p> <p class=MsoNormal style="BACKGROUND: silver; MARGIN: 0cm 0cm 0pt"><span lang=EN-US>class String</span> </p> <p class=MsoNormal style="BACKGROUND: silver; MARGIN: 0cm 0cm 0pt"><span lang=EN-US>{</span> </p> <p class=MsoNormal style="BACKGROUND: silver; MARGIN: 0cm 0cm 0pt"><span lang=EN-US>public:</span> </p> <p class=MsoNormal style="BACKGROUND: silver; MARGIN: 0cm 0cm 0pt"><span lang=EN-US><span style="mso-tab-count: 1">       </span>String(const char *m_char = NULL);</span> </p> <p class=MsoNormal style="BACKGROUND: silver; MARGIN: 0cm 0cm 0pt"><span lang=EN-US><span style="mso-tab-count: 1">       </span>String(const String & Str);</span> </p> <p class=MsoNormal style="BACKGROUND: silver; MARGIN: 0cm 0cm 0pt"><span lang=EN-US><span style="mso-tab-count: 1">       </span>String& operator = (const String &Str);</span> </p> <p class=MsoNormal style="BACKGROUND: silver; MARGIN: 0cm 0cm 0pt"><span lang=EN-US><span style="mso-tab-count: 1">       </span>~String();</span> </p> <p class=MsoNormal style="BACKGROUND: silver; MARGIN: 0cm 0cm 0pt"><span lang=EN-US>private:</span> </p> <p class=MsoNormal style="BACKGROUND: silver; MARGIN: 0cm 0cm 0pt"><span lang=EN-US><span style="mso-tab-count: 1">       </span>char * m_Data;</span> </p> <p class=MsoNormal style="BACKGROUND: silver; MARGIN: 0cm 0cm 0pt"><span lang=EN-US>};</span> </p> <p class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><span lang=EN-US><o:p> </o:p> </span></p> <p class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 21pt; mso-char-indent-count: 2.0"><span style="FONT-FAMILY: ?hu)? mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">关于</span> <span lang=EN-US>String</span> <span style="FONT-FAMILY: ?hu)? mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">cȝW试题,以前看林锐的随笔时听说他在微软面试时曄到那么一道题目,我自׃没有真的下笔dq,q_都是拿来q的,q次自己到Q才知道?x)死得那么惨Q反正编得不堪入目(我就不拿出来献丑?jin)?j)Q下面是我回来后Q自己重新写的答案?/span> </p> <p class=MsoNormal style="BACKGROUND: silver; MARGIN: 0cm 0cm 0pt"><span lang=EN-US>String::String(const char* m_char)</span> </p> <p class=MsoNormal style="BACKGROUND: silver; MARGIN: 0cm 0cm 0pt"><span lang=EN-US>{</span> </p> <p class=MsoNormal style="BACKGROUND: silver; MARGIN: 0cm 0cm 0pt"><span lang=EN-US><span style="mso-tab-count: 1">       </span>int m_nLength = strlen(m_char) + 1;</span> </p> <p class=MsoNormal style="BACKGROUND: silver; MARGIN: 0cm 0cm 0pt"><span lang=EN-US><span style="mso-tab-count: 1">       </span>if (m_Data != NULL)</span> </p> <p class=MsoNormal style="BACKGROUND: silver; MARGIN: 0cm 0cm 0pt"><span lang=EN-US><span style="mso-tab-count: 1">       </span>{</span> </p> <p class=MsoNormal style="BACKGROUND: silver; MARGIN: 0cm 0cm 0pt"><span lang=EN-US><span style="mso-tab-count: 2">              </span>delete [] m_Data;</span> </p> <p class=MsoNormal style="BACKGROUND: silver; MARGIN: 0cm 0cm 0pt"><span lang=EN-US><span style="mso-tab-count: 2">              </span>m_Data = NULL;</span> </p> <p class=MsoNormal style="BACKGROUND: silver; MARGIN: 0cm 0cm 0pt"><span lang=EN-US><span style="mso-tab-count: 1">       </span>}//</span> <span style="FONT-FAMILY: ?hu)? mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">以上判断是否必要</span> <span lang=EN-US>??</span> </p> <p class=MsoNormal style="BACKGROUND: silver; MARGIN: 0cm 0cm 0pt"><span lang=EN-US><span style="mso-tab-count: 1">       </span>m_Data = new char[m_nLength];</span> </p> <p class=MsoNormal style="BACKGROUND: silver; MARGIN: 0cm 0cm 0pt"><span lang=EN-US><span style="mso-tab-count: 1">       </span>memcpy(m_Data, m_char, m_nLength);</span> </p> <p class=MsoNormal style="BACKGROUND: silver; MARGIN: 0cm 0cm 0pt"><span lang=EN-US>}</span> </p> <p class=MsoNormal style="BACKGROUND: silver; MARGIN: 0cm 0cm 0pt"><span lang=EN-US><o:p> </o:p> </span></p> <p class=MsoNormal style="BACKGROUND: silver; MARGIN: 0cm 0cm 0pt"><span lang=EN-US>String::String(const String &Str)</span> </p> <p class=MsoNormal style="BACKGROUND: silver; MARGIN: 0cm 0cm 0pt"><span lang=EN-US>{</span> </p> <p class=MsoNormal style="BACKGROUND: silver; MARGIN: 0cm 0cm 0pt"><span lang=EN-US><span style="mso-tab-count: 1">       </span>int m_nLength = strlen(Str.m_Data) + 1;//</span> <span style="FONT-FAMILY: ?hu)? mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">以前真的不知道,原来对象的私有变?/span> </p> <p class=MsoNormal style="BACKGROUND: silver; MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 194.25pt; mso-char-indent-count: 18.5"><span lang=EN-US>//</span> <span style="FONT-FAMILY: ?hu)? mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">在类的实C码中也是可以讉K?/span> </p> <p class=MsoNormal style="BACKGROUND: silver; MARGIN: 0cm 0cm 0pt"><span lang=EN-US><span style="mso-tab-count: 1">       </span>if (m_Data != NULL)</span> </p> <p class=MsoNormal style="BACKGROUND: silver; MARGIN: 0cm 0cm 0pt"><span lang=EN-US><span style="mso-tab-count: 1">       </span>{</span> </p> <p class=MsoNormal style="BACKGROUND: silver; MARGIN: 0cm 0cm 0pt"><span lang=EN-US><span style="mso-tab-count: 2">              </span>delete [] m_Data;</span> </p> <p class=MsoNormal style="BACKGROUND: silver; MARGIN: 0cm 0cm 0pt"><span lang=EN-US><span style="mso-tab-count: 2">              </span>m_Data = NULL;</span> </p> <p class=MsoNormal style="BACKGROUND: silver; MARGIN: 0cm 0cm 0pt"><span lang=EN-US><span style="mso-tab-count: 1">       </span>}//</span> <span style="FONT-FAMILY: ?hu)? mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">以上判断是否必要</span> <span lang=EN-US>??</span> </p> <p class=MsoNormal style="BACKGROUND: silver; MARGIN: 0cm 0cm 0pt"><span lang=EN-US><span style="mso-tab-count: 1">       </span>m_Data = new char[m_nLength];</span> </p> <p class=MsoNormal style="BACKGROUND: silver; MARGIN: 0cm 0cm 0pt"><span lang=EN-US><span style="mso-tab-count: 1">       </span>memcpy(m_Data, Str.m_Data, m_nLength);</span> </p> <p class=MsoNormal style="BACKGROUND: silver; MARGIN: 0cm 0cm 0pt"><span lang=EN-US>}</span> </p> <p class=MsoNormal style="BACKGROUND: silver; MARGIN: 0cm 0cm 0pt"><span lang=EN-US><o:p> </o:p> </span></p> <p class=MsoNormal style="BACKGROUND: silver; MARGIN: 0cm 0cm 0pt"><span lang=EN-US>String& String::operator = (const String& Str)<span style="mso-spacerun: yes">  </span></span></p> <p class=MsoNormal style="BACKGROUND: silver; MARGIN: 0cm 0cm 0pt"><span lang=EN-US>{</span> </p> <p class=MsoNormal style="BACKGROUND: silver; MARGIN: 0cm 0cm 0pt"><span lang=EN-US><span style="mso-tab-count: 1">       </span>if(this == &Str)</span> </p> <p class=MsoNormal style="BACKGROUND: silver; MARGIN: 0cm 0cm 0pt"><span lang=EN-US><span style="mso-tab-count: 2">              </span>return *this;</span> </p> <p class=MsoNormal style="BACKGROUND: silver; MARGIN: 0cm 0cm 0pt"><span lang=EN-US><o:p> </o:p> </span></p> <p class=MsoNormal style="BACKGROUND: silver; MARGIN: 0cm 0cm 0pt"><span lang=EN-US><span style="mso-tab-count: 1">       </span>int m_nLength = strlen(Str.m_Data) + 1;</span> </p> <p class=MsoNormal style="BACKGROUND: silver; MARGIN: 0cm 0cm 0pt"><span lang=EN-US><span style="mso-tab-count: 1">       </span>if (m_Data != NULL)</span> </p> <p class=MsoNormal style="BACKGROUND: silver; MARGIN: 0cm 0cm 0pt"><span lang=EN-US><span style="mso-tab-count: 1">       </span>{</span> </p> <p class=MsoNormal style="BACKGROUND: silver; MARGIN: 0cm 0cm 0pt"><span lang=EN-US><span style="mso-tab-count: 2">              </span>delete [] m_Data;</span> </p> <p class=MsoNormal style="BACKGROUND: silver; MARGIN: 0cm 0cm 0pt"><span lang=EN-US><span style="mso-tab-count: 2">              </span>m_Data = NULL;</span> </p> <p class=MsoNormal style="BACKGROUND: silver; MARGIN: 0cm 0cm 0pt"><span lang=EN-US><span style="mso-tab-count: 1">       </span>}//</span> <span style="FONT-FAMILY: ?hu)? mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">以上判断是否必要</span> <span lang=EN-US>??</span> </p> <p class=MsoNormal style="BACKGROUND: silver; MARGIN: 0cm 0cm 0pt"><span lang=EN-US><span style="mso-tab-count: 1">       </span></span></p> <p class=MsoNormal style="BACKGROUND: silver; MARGIN: 0cm 0cm 0pt"><span lang=EN-US><span style="mso-tab-count: 1">       </span>m_Data = new char[m_nLength];</span> </p> <p class=MsoNormal style="BACKGROUND: silver; MARGIN: 0cm 0cm 0pt"><span lang=EN-US><span style="mso-tab-count: 1">       </span>memcpy(m_Data, Str.m_Data, m_nLength);</span> </p> <p class=MsoNormal style="BACKGROUND: silver; MARGIN: 0cm 0cm 0pt"><span lang=EN-US><span style="mso-tab-count: 1">       </span>return *this;</span> </p> <p class=MsoNormal style="BACKGROUND: silver; MARGIN: 0cm 0cm 0pt"><span lang=EN-US>}</span> </p> <p class=MsoNormal style="BACKGROUND: silver; MARGIN: 0cm 0cm 0pt"><span lang=EN-US><o:p> </o:p> </span></p> <p class=MsoNormal style="BACKGROUND: silver; MARGIN: 0cm 0cm 0pt"><span lang=EN-US>String::~String()</span> </p> <p class=MsoNormal style="BACKGROUND: silver; MARGIN: 0cm 0cm 0pt"><span lang=EN-US>{</span> </p> <p class=MsoNormal style="BACKGROUND: silver; MARGIN: 0cm 0cm 0pt"><span lang=EN-US><span style="mso-tab-count: 1">       </span>if (m_Data != NULL)</span> </p> <p class=MsoNormal style="BACKGROUND: silver; MARGIN: 0cm 0cm 0pt"><span lang=EN-US><span style="mso-tab-count: 1">       </span>{</span> </p> <p class=MsoNormal style="BACKGROUND: silver; MARGIN: 0cm 0cm 0pt"><span lang=EN-US><span style="mso-tab-count: 2">              </span>delete [] m_Data;</span> </p> <p class=MsoNormal style="BACKGROUND: silver; MARGIN: 0cm 0cm 0pt"><span lang=EN-US><span style="mso-tab-count: 2">              </span>m_Data = NULL;</span> </p> <p class=MsoNormal style="BACKGROUND: silver; MARGIN: 0cm 0cm 0pt"><span lang=EN-US><span style="mso-tab-count: 1">       </span>}</span> </p> <p class=MsoNormal style="BACKGROUND: silver; MARGIN: 0cm 0cm 0pt"><span lang=EN-US>}</span> </p> <p class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><span lang=EN-US><o:p> </o:p> </span></p> <p class=MsoNormal style="MARGIN: 0cm 0cm 0pt 18pt; TEXT-INDENT: -18pt; mso-list: l0 level1 lfo1; tab-stops: list 18.0pt"><span lang=EN-US style="mso-fareast-font-family: 'Times New Roman'"><span style="mso-list: Ignore">2?span style="FONT: 7pt 'Times New Roman'">  </span></span></span><span style="FONT-FAMILY: ?hu)? mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">关于内存分配</span> </p> <p class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 21pt; mso-char-indent-count: 2.0"><span style="FONT-FAMILY: ?hu)? mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">q个题目很简单,q?jin)一个函敎ͼ然后问函数内的局部变量存攑֜哪里Q我也不知道Z么当时会(x)选择</span> <span lang=EN-US>heap(</span> <span style="FONT-FAMILY: ?hu)? mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">?/span> <span lang=EN-US>)</span> <span style="FONT-FAMILY: ?hu)? mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">Q下面再把几个概늽列出来:(x)</span> </p> <p class=MsoNormal style="MARGIN: 0cm 0cm 0pt 47.25pt; TEXT-INDENT: -26.25pt; mso-list: l0 level2 lfo1; tab-stops: list 47.25pt"><span lang=EN-US style="mso-fareast-font-family: 'Times New Roman'"><span style="mso-list: Ignore">1.<span style="FONT: 7pt 'Times New Roman'">            </span></span></span><span style="FONT-FAMILY: ?hu)? mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">堆区Q?/span> <span lang=EN-US>heap</span> <span style="FONT-FAMILY: ?hu)? mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">Q:(x)q序员甌分配和释放,属动态内存分配方式,若程序员?sh)释放,E序l束时可能会(x)?/span> <span lang=EN-US>OS</span> <span style="FONT-FAMILY: ?hu)? mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">回收。不q这个内存分配很Ҏ(gu)引v问题Q如果申L(fng)内存?sh)释攑ְ会(x)造成内存泄漏Q如果释攄不是所要释攄内存Q则轻者引L(fng)序运行结果出错,重者系l崩溃?/span> </p> <p class=MsoNormal style="MARGIN: 0cm 0cm 0pt 47.25pt; TEXT-INDENT: -26.25pt; mso-list: l0 level2 lfo1; tab-stops: list 47.25pt"><span lang=EN-US style="mso-fareast-font-family: 'Times New Roman'"><span style="mso-list: Ignore">2.<span style="FONT: 7pt 'Times New Roman'">            </span></span></span><span style="FONT-FAMILY: ?hu)? mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">栈区Q?/span> <span lang=EN-US>stack</span> <span style="FONT-FAMILY: ?hu)? mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">Q:(x)~译器自动分配释放,存放函数的Ş参倹{局部变量的|也是属于动态内存分配方式,它由pȝ分配Q所以执行效率也高,不过自由度小Q声明时得军_其具体大?/span> </p> <p class=MsoNormal style="MARGIN: 0cm 0cm 0pt 47.25pt; TEXT-INDENT: -26.25pt; mso-list: l0 level2 lfo1; tab-stops: list 47.25pt"><span lang=EN-US style="mso-fareast-font-family: 'Times New Roman'"><span style="mso-list: Ignore">3.<span style="FONT: 7pt 'Times New Roman'">            </span></span></span><span style="FONT-FAMILY: ?hu)? mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">全局区(?rn)态区Q(</span> <span lang=EN-US>static</span> <span style="FONT-FAMILY: ?hu)? mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">Q:(x)全局变量和静(rn)态变量的存储是放在一块的Q而且初始化的全局变量和静(rn)态变量在一块区域,未初始化的全局变量和未初始化的?rn)态变量在盔R的另一块区域。程序结束后ql释放,所以也不会(x)造成内存问题?/span> </p> <p class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 21pt; mso-char-indent-count: 2.0"><span style="FONT-FAMILY: ?hu)? mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">除了(jin)以上的变量外Q还有两cd放位|,文字帔R区和E序代码区,两者都是由pȝ分配和释放,且文字常量区和前面三区合成ؓ(f)E序数据区,与程序代码区相对应?/span> </p> <p class=MsoNormal style="MARGIN: 0cm 0cm 0pt 18pt; TEXT-INDENT: -18pt; mso-list: l0 level1 lfo1; tab-stops: list 18.0pt"><span lang=EN-US style="mso-fareast-font-family: 'Times New Roman'"><span style="mso-list: Ignore">3?span style="FONT: 7pt 'Times New Roman'">  </span></span></span><span style="FONT-FAMILY: ?hu)? mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">关于cȝ承的构造和析构函数</span> </p> <p class=MsoNormal style="BACKGROUND: silver; MARGIN: 0cm 0cm 0pt"><span lang=EN-US>class Base</span> </p> <p class=MsoNormal style="BACKGROUND: silver; MARGIN: 0cm 0cm 0pt"><span lang=EN-US>{</span> </p> <p class=MsoNormal style="BACKGROUND: silver; MARGIN: 0cm 0cm 0pt"><span lang=EN-US>public:</span> </p> <p class=MsoNormal style="BACKGROUND: silver; MARGIN: 0cm 0cm 0pt"><span lang=EN-US><span style="mso-tab-count: 1">       </span>Base(){cout<< "Base" <<endl;};</span> </p> <p class=MsoNormal style="BACKGROUND: silver; MARGIN: 0cm 0cm 0pt"><span lang=EN-US><span style="mso-tab-count: 1">       </span>~Base(){cout<<"~Base"<<endl;};</span> </p> <p class=MsoNormal style="BACKGROUND: silver; MARGIN: 0cm 0cm 0pt"><span lang=EN-US>protected:</span> </p> <p class=MsoNormal style="BACKGROUND: silver; MARGIN: 0cm 0cm 0pt"><span lang=EN-US>private:</span> </p> <p class=MsoNormal style="BACKGROUND: silver; MARGIN: 0cm 0cm 0pt"><span lang=EN-US>};</span> </p> <p class=MsoNormal style="BACKGROUND: silver; MARGIN: 0cm 0cm 0pt"><span lang=EN-US><o:p> </o:p> </span></p> <p class=MsoNormal style="BACKGROUND: silver; MARGIN: 0cm 0cm 0pt"><span lang=EN-US>class First:public Base</span> </p> <p class=MsoNormal style="BACKGROUND: silver; MARGIN: 0cm 0cm 0pt"><span lang=EN-US>{</span> </p> <p class=MsoNormal style="BACKGROUND: silver; MARGIN: 0cm 0cm 0pt"><span lang=EN-US>public:</span> </p> <p class=MsoNormal style="BACKGROUND: silver; MARGIN: 0cm 0cm 0pt"><span lang=EN-US><span style="mso-tab-count: 1">       </span>First(){cout << "First" << endl;};</span> </p> <p class=MsoNormal style="BACKGROUND: silver; MARGIN: 0cm 0cm 0pt"><span lang=EN-US><span style="mso-tab-count: 1">       </span>~First(){cout << "~First" <<endl;};</span> </p> <p class=MsoNormal style="BACKGROUND: silver; MARGIN: 0cm 0cm 0pt"><span lang=EN-US>};</span> </p> <p class=MsoNormal style="BACKGROUND: silver; MARGIN: 0cm 0cm 0pt"><span lang=EN-US><o:p> </o:p> </span></p> <p class=MsoNormal style="BACKGROUND: silver; MARGIN: 0cm 0cm 0pt"><span lang=EN-US>int main()</span> </p> <p class=MsoNormal style="BACKGROUND: silver; MARGIN: 0cm 0cm 0pt"><span lang=EN-US>{</span> </p> <p class=MsoNormal style="BACKGROUND: silver; MARGIN: 0cm 0cm 0pt"><span lang=EN-US><span style="mso-tab-count: 1">       </span>Base *a = new First;</span> </p> <p class=MsoNormal style="BACKGROUND: silver; MARGIN: 0cm 0cm 0pt"><span lang=EN-US><span style="mso-tab-count: 1">       </span>delete a;</span> </p> <p class=MsoNormal style="BACKGROUND: silver; MARGIN: 0cm 0cm 0pt"><span lang=EN-US>}</span> </p> <p class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><span style="FONT-FAMILY: ?hu)? mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">问程序的输出?x)是什么?</span> </p> <p class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><span style="FONT-FAMILY: ?hu)? mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">l果很简单,也就?/span> <span lang=EN-US>Base</span> </p> <p class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><span lang=EN-US><span style="mso-tab-count: 4">                            </span><span style="mso-spacerun: yes">   </span>First</span> </p> <p class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><span lang=EN-US><span style="mso-spacerun: yes">                   </span>~Base</span> </p> <p class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><span style="FONT-FAMILY: ?hu)? mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">其它q有一个关?/span> <span lang=EN-US>&</span> <span style="FONT-FAMILY: ?hu)? mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">的题目,把我搞的云里N的,q要再看些东西才知道怎么来解释?/span> </p> <img src ="http://www.shnenglu.com/franksunny/aggbug/19199.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.shnenglu.com/franksunny/" target="_blank">frank.sunny</a> 2007-03-04 21:53 <a href="http://www.shnenglu.com/franksunny/archive/2007/03/04/19199.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>[转蝲]关于寚w和sizeof函数http://www.shnenglu.com/franksunny/archive/2006/12/27/16894.htmlfrank.sunnyfrank.sunnyTue, 26 Dec 2006 16:01:00 GMThttp://www.shnenglu.com/franksunny/archive/2006/12/27/16894.htmlhttp://www.shnenglu.com/franksunny/comments/16894.htmlhttp://www.shnenglu.com/franksunny/archive/2006/12/27/16894.html#Feedback0http://www.shnenglu.com/franksunny/comments/commentRss/16894.htmlhttp://www.shnenglu.com/franksunny/services/trackbacks/16894.html
关于指针和数l、结构和cȝsizeof讲解http://blog.vckbase.com/billdavid/archive/2004/06/23/509.html
关于联合体的sizeof讲解http://blog.vckbase.com/smileonce/archive/2005/08/08/10658.html
关于寚w的小l性文?a >http://blog.vckbase.com/zhangjw_cn/archive/2005/08/09/10701.html
自己懒得打开链接Q再把小l性文章的结Q也拿来转一下?br>
最后得C(jin)以下l论Q?br>    1. 成员的对齐是按声明顺序进行的Q?br>    2. 寚w值由~译指示和最大成员(sh)者较?yu)的值决定;
    3. 未对齐到寚w值的成员?sh)起Ş成块寚wQ联合对齐)(j)Q?br>    4. 上一个(下一个)(j)寚w采用自己较大则不变,自己较小则填充自己对齐到上一个(下一个)(j)大小Q?br>    5. 每成员对齐:(x)如果前面已对齐到寚w|下一个对齐自己。如果前面未寚w到对齐|如果加上下一个成员(sh)大于寚w|下一个对齐自己,否则填充自己块对齐到寚w倹{?br>    6. 最后还未对齐到寚w值的Q填充空间块寚w到对齐倹{?br>
从这些结论,可以得到Q?br>    1. 以上的对齐原则其实是量整齐排列、尽量节省内存?br>    2. 声明成员应该量避免不同cd错杂开来,最好采用从到大或者从大到的序Q错开后,?x)因Z寚w和下寚w而增加填充开销Q?br>    3. ~译器缺省采?字节寚w主要是因为最大基本类型ؓ(f)8自己Q以前自׃明白Q在论坛提过问,后来Q以为是SSE指o(h)的原因)(j)?br>    4. 手算sizeof是没有必要的Q负责的Q可以先寚w出对齐块Q用块数乘对齐|(j)?

frank.sunny 2006-12-27 00:01 发表评论
]]>
W记一、数据类?/title><link>http://www.shnenglu.com/franksunny/archive/2006/11/10/14988.html</link><dc:creator>frank.sunny</dc:creator><author>frank.sunny</author><pubDate>Fri, 10 Nov 2006 15:43:00 GMT</pubDate><guid>http://www.shnenglu.com/franksunny/archive/2006/11/10/14988.html</guid><wfw:comment>http://www.shnenglu.com/franksunny/comments/14988.html</wfw:comment><comments>http://www.shnenglu.com/franksunny/archive/2006/11/10/14988.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.shnenglu.com/franksunny/comments/commentRss/14988.html</wfw:commentRss><trackback:ping>http://www.shnenglu.com/franksunny/services/trackbacks/14988.html</trackback:ping><description><![CDATA[     摘要:   < W记一、数据类?> 现在业界普遍认可以下{式 E序 = 数据l构   + 法 + 文 q第一笔记就只对以上提到的数据结构结合本人的理解展开做些ȝ?   cd E序的输入输出的实体是数据信息Q而对q些数据信息l以归类和组l,我们q为数据结构。因此数据结构就是对数据的组lŞ式,也可以说是对内存的编码规则?..  <a href='http://www.shnenglu.com/franksunny/archive/2006/11/10/14988.html'>阅读全文</a><img src ="http://www.shnenglu.com/franksunny/aggbug/14988.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.shnenglu.com/franksunny/" target="_blank">frank.sunny</a> 2006-11-10 23:43 <a href="http://www.shnenglu.com/franksunny/archive/2006/11/10/14988.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>׃道面试题来看Struct的对界(再谈l构Q?/title><link>http://www.shnenglu.com/franksunny/archive/2006/10/20/13925.html</link><dc:creator>frank.sunny</dc:creator><author>frank.sunny</author><pubDate>Fri, 20 Oct 2006 13:52:00 GMT</pubDate><guid>http://www.shnenglu.com/franksunny/archive/2006/10/20/13925.html</guid><wfw:comment>http://www.shnenglu.com/franksunny/comments/13925.html</wfw:comment><comments>http://www.shnenglu.com/franksunny/archive/2006/10/20/13925.html#Feedback</comments><slash:comments>6</slash:comments><wfw:commentRss>http://www.shnenglu.com/franksunny/comments/commentRss/13925.html</wfw:commentRss><trackback:ping>http://www.shnenglu.com/franksunny/services/trackbacks/13925.html</trackback:ping><description><![CDATA[     摘要: ׃道面试题来看 Struct 的对?   本文节选自?hu)宝华的C/C++的struct深层探烦(ch)一文,本h对其所描述的struct寚w比较喜欢Qؓ(f)此{来与大家分nQ原文见http://blog.donews.com/21cnbao/archive/2005/09/08/544877.aspx   Intel 、微软等公司曄?gu)一道类似的面试题:(x) 1. #in...  <a href='http://www.shnenglu.com/franksunny/archive/2006/10/20/13925.html'>阅读全文</a><img src ="http://www.shnenglu.com/franksunny/aggbug/13925.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.shnenglu.com/franksunny/" target="_blank">frank.sunny</a> 2006-10-20 21:52 <a href="http://www.shnenglu.com/franksunny/archive/2006/10/20/13925.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>C/C++l构体的一个高U特性――指定成员的位数http://www.shnenglu.com/franksunny/archive/2006/10/20/13887.htmlfrank.sunnyfrank.sunnyThu, 19 Oct 2006 16:05:00 GMThttp://www.shnenglu.com/franksunny/archive/2006/10/20/13887.htmlhttp://www.shnenglu.com/franksunny/comments/13887.htmlhttp://www.shnenglu.com/franksunny/archive/2006/10/20/13887.html#Feedback7http://www.shnenglu.com/franksunny/comments/commentRss/13887.htmlhttp://www.shnenglu.com/franksunny/services/trackbacks/13887.html阅读全文

frank.sunny 2006-10-20 00:05 发表评论
]]>
青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品
  • <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
      <noscript id="pjuwb"></noscript>
            <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
              <dd id="pjuwb"></dd>
              <abbr id="pjuwb"></abbr>
              ޾ƷŮ߹ۿ| һƷ| þþþþþۺձ| þþþùɫavѹۿɫ| ƵƵþ| һ| һպ| þ99߹ۿ| ޼һ| Ʒҽ| þۺϸϾþúݺݺ97ɫ69| ޹պۺһ| Ƶۿ| þۺ| ŷþþþþ| 㽶þһ޶ӰԺ| þѹۿ| ԴۿƵվѲ| ƷŮ999| þ㽶߿ۿav| 99re߾Ʒ| þþŷƷsmվ| һ12Ƶdvd| պŷۺһ| ŷ99xxxxx| ŷһɫ| 91þþùƷ| þþžre6оƷ| һһþaþþƷۺ| պav| ŷ㶮| ŷƬ| պŷ| ŷ| þþƷ| ߵһҳ| Ƶ| ݺɫۺӰԺ| ŷպѹۿ| þþƷ | þþƷþ| 99Ʒ99| ޼ۺ| ŷӰƨsp| þŮ| þþƷۺ| ŷձƷ| һŮƷŷ| ղƷbd| ޵һþӰԺ| õƵһ| Ʒպһ| ŷպһ| ŷƷӰ| ŷһ| ţţƷƵ| þҹɫƷ| þùֻƬ| þþƷ| þþƷ30| ŷmvպmvվ| ˳ɾƷþþþ| ŷjjzz| ŷձ| ŷƷ| ŷƵվ| ŷ߹ۿ| ŷ| ŷƵ| ޾ƷƵ߹ۿ| ޾Ʒһ| ޹ҹһ| ޼| ѹۿ߹ۿ| һƵ | ˳վ77777| Ļպ| ԻavӰ| ŷһձһһ| þþƷƵһ| 鶹һƷƵ߹ۿ| þĻ| ŷƷvպƷvƷv | ŷػaѴƬa| ŷպˮ| 鶹ҹƷ| ޾ƷŮ| ޹Ƶ| | ŷ߲Ÿ徫Ʒ| Ƶ߹ۿ| ޵һƷӰ| լ66һ66| Ƶ߹ۿŷ| ۺպŷ| ŷպɫһ| ձŷ| ޸| Ƶ| þ޴ɫĻ| ŷձר| ŷ| Ůһ| Ʒþþ99| ݺɫۺӰԺ | ŷƷĻ| 鶹avavþav| ޾ƷƷԲ | þþ7777| Ʒþ77777| һɫݾƷƵ߿| һɫ| һ| һһþۺϺݺϾӰҵ | ޾ƷƷþ99| ŷ8khd弫Ʒ| ŷպ| ҹ޾Ʒվ| պһŷ| þۺϾþۺϾɫ| պ444www| þù˿| ƷƵxxxx| ޾Ʒھþ| þ㽶߿ۿ| һ| ŷ˹ˬˬˬ| һ߹ۿѲ | ۺ| 㽶Ʒ999Ƶһ | õ**ŷ| ޹߳ƷŮ| ŷһƵ| ޾ƷƵһ| ţţƷƵ| һƵۿ| ŷպһ| ޾Ʒٸ30p| ѹۿ³³³³³Ƶ| ھƷ˾þþþavһ| ŷһ| һɫþۺϺݺƪŵ| ĢƵһ| ޸Ƶַ| þһƷ| þùƷ99Ʒ| Ʒþþþþ| ߹ۿƵ| 99ھƷ| ŷպһƵ| ޾Ʒ| ޹˾Ʒþþùһ| þù66| ߾Ʒŷ| ŷ| ŷһ| | Ů߿| ŷպɫƬ| ҹ| վ| ŷһƵ| þþƷһ| 㽶˾þ| Բ͵ĸƷһ| þ͵Ƶ| þƵվ| ޺һ| պĻ߲| ŷҳ| Ů͵| ŷ| ޵һƷӰ| Ƶ| Ʒþþþþþþ| 㽶Ʒ͵߹ۿ| ŷƵѹۿ| ޹Ƭɫ| պƵ| Ʒˬ| þþþþҹƷƷ| 鶹Ʒһ| Ļһ| ŷսþþþþþþ| ˳˿ļۺ| ޹Ʒ| Ʒ߹ۿ| þþƷ| Ůջһ| Ʒ˿޲| ŷ| ѹۿ߹ۿ| һõۺϾþþƷ| | ձŷ| һþ| պƵѹۿƵ| ҹ޾Ʒ| ޿ͨŷƷ| һ|