#ifdef _DEBUG #define new DEBUG_NEW #undef THIS_FILE static char THIS_FILE[] = __FILE__; #endif 语句解释

VS2010环境下,添加MFC Class时,程序会自动生成框架代码,cpp文件的include下面有如下语句:

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
以下是这段代码的相关解释:

#ifdef _DEBUG //如果定义了_DEBUG,也就是调试版程序,则执行以下三句 
#define new DEBUG_NEW //定义new为DEBUG_NEW 
#undef THIS_FILE //取消以前对THIS_FILE的定义 
static char THIS_FILE[] = __FILE__; //声明并初始化THIS_FILEO数组,数据的初始数据为该文件的文件名,__FILE__ 为当前源文件的名称
#endif 
作用:帮助查找内存错误。用户在程序中使用DEBUG_NEW,用户通常使用new运算符来从堆上分配。在Debug模式下(但定义了一个DEBUG符号),DEBUG_NEW为它分配的每个对象记录文件名和行号。然后,在用户使用CMemoryState::DumpAllObjectSince成员函数时,每个以DEBUG_NEW分配的对象分配的地方显示出文件名和行号。 为了使用DEBUG_NEW,应在用户的资源文件中插入以下指令: #define new DEBUG_NEW 一旦用户插入本指令,预处理程序将在使用new的地方插入DEBUG_NEW,而MFC作其余的工作。但用户编译自己的程序的一个发行版时,DEBUG_NEW便进行简单的new操作,而且不产生文件名和行号消息。 

// zuoye07.cpp : Defines the entry point for the console application. // #include "stdafx.h" #include "zuoye07.h" #ifdef _DEBUG #define new DEBUG_NEW #undef THIS_FILE static char THIS_FILE[] = __FILE__; #endif DWORD BufferSize=1024; char buf[1024]; / // The one and only application object CWinApp theApp; using namespace std; void FileReadWrite_NoBuffer(char*source,char*destination); int _tmain(int argc,TCHAR*angv[],TCHAR*envp[]) { int nRetCode=0; printf("Call FileReadWrite_NoBuffer!\n"); //调用FileReadWrite_NoBuffer(char*source,char*destination)函数 FileReadWrite_NoBuffer("source.txt","nobuffer.txt"); return nRetCode; } void FileReadWrite_NoBuffer(char*source,char*destination) { HANDLE handle_src,handle_dst; DWORD NumberOfByteWrite; bool cycle; char*buffer; buffer=buf; //创建文件source.txt handle_src=CreateFile(source, GENERIC_READ, 0, NULL, OPEN_ALWAYS, FILE_FLAG_NO_BUFFERING, NULL); //创建文件nobuffer.txt handle_dst=CreateFile(destination, GENERIC_WRITE, NULL, NULL, OPEN_ALWAYS, NULL, NULL); //判断文件是否创建失败,若失败打印输出提示信息并退出 if(handle_src==INVALID_HANDLE_VALUE || handle_dst==INVALID_HANDLE_VALUE) { printf("File Create Fail!\n"); exit(1); } cycle=true; while(cycle) { NumberOfByteWrite=BufferSize; //读取文件source.txt if(!ReadFile(handle_src,buffer,NumberOfByteWrite,&NumberOfByteWrite,NULL)) { //读取文件source.txt失败 printf("Read File Error!%d\n",GetLastError()); exit(1); } if(NumberOfByteWrite<BufferSize)cycle=false; //写入文件nobuffer.txt if(!WriteFile(handle_dst,buffer,NumberOfByteWrite,&NumberOfByteWrite,NULL)) { //写入文件nobuffer.txt失败 printf("Write File Error!%d\n",GetLastError()); exit(1); } } //关闭文件句柄(source.txt,nobuffer.txt) CloseHandle(handle_src); CloseHandle(handle_dst); }
06-09
vc++利用ADOX创建数据库 // ADOXCreateDatabaseDlg.cpp : implementation file // #include "stdafx.h" #include "ADOXCreateDatabase.h" #include "ADOXCreateDatabaseDlg.h" #include "Shlwapi.h" #pragma comment(lib,"shlwapi.lib") // Download by http://www.codefans.net #ifdef _DEBUG #define new DEBUG_NEW #undef THIS_FILE static char THIS_FILE[] = __FILE__; #endif ///////////////////////////////////////////////////////////////////////////// // CAboutDlg dialog used for App About class CAboutDlg : public CDialog { public: CAboutDlg(); // Dialog Data //{{AFX_DATA(CAboutDlg) enum { IDD = IDD_ABOUTBOX }; //}}AFX_DATA // ClassWizard generated virtual function overrides //{{AFX_VIRTUAL(CAboutDlg) protected: virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support //}}AFX_VIRTUAL // Implementation protected: //{{AFX_MSG(CAboutDlg) //}}AFX_MSG DECLARE_MESSAGE_MAP() }; CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD) { //{{AFX_DATA_INIT(CAboutDlg) //}}AFX_DATA_INIT } void CAboutDlg::DoDataExchange(CDataExchange* pDX) { CDialog::DoDataExchange(pDX); //{{AFX_DATA_MAP(CAboutDlg) //}}AFX_DATA_MAP } BEGIN_MESSAGE_MAP(CAboutDlg, CDialog) //{{AFX_MSG_MAP(CAboutDlg) // No message handlers //}}AFX_MSG_MAP END_MESSAGE_MAP() ///////////////////////////////////////////////////////////////////////////// // CADOXCreateDatabaseDlg dialog CADOXCreateDatabaseDlg::CADOXCreateDatabaseDlg(CWnd* pParent /*=NULL*/) : CDialog(CADOXCreateDatabaseDlg::IDD, pParent) { //{{AFX_DATA_INIT(CADOXCreateDatabaseDlg) m_dbName = _T(""); //}}AFX_DATA_INIT // Note that LoadIcon does not require a subsequent DestroyIcon in Win32 m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME); } void CADOXCreateDatabaseDlg::DoDataExchange(CDataExchange* pDX) { CDialog::DoDataExchange(pDX); //{{AFX_DATA_MAP(CADOXCreateDatabaseDlg) DDX_Text(pDX, IDC_DBNAME, m_dbName); //}}AFX_DATA_MAP } BEGIN_MESSAGE_MAP(CADOXCreateDatabaseDlg, CDialog) //{{AFX_MSG_MAP(CADOXCreateDatabaseDlg) ON_WM_SYSCOMM
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值