Advanced C + + programming

Source: Internet
Author: User

Code Snippet 1:

//stdafx.h//stdafx.h: The standard system contains file containing files,//or frequently used but not often changed.//project-specific include files//#pragmaOnce//If you must target a platform that precedes the platform specified below, modify the following definitions. //for the latest information on different platform values, please refer to MSDN. #ifndef WINVER//allows the use of features specific to Windows XP or later. #defineWINVER 0x0501//change this value to the appropriate value to apply to other versions of Windows. #endif#ifndef _win32_winnt//allows the use of features specific to Windows XP or later. #define_win32_winnt 0x0501//change this value to the appropriate value to apply to other versions of Windows. #endif#ifndef _win32_windows//allows the use of features specific to Windows 98 or later. #define_win32_windows 0x0410//change this value to the appropriate value to specify that Windows Me or a later version be targeted. #endif#ifndef _win32_ie//allows the use of features specific to IE 6.0 or later. #define_win32_ie 0x0600//change this value to the appropriate value to apply to other versions of IE. #endif#defineWin32_lean_and_mean//exclude rarely used data from the Windows header#include<stdio.h>#include<tchar.h>#define_atl_cstring_explicit_constructors//some of the CString constructors will be explicit#ifndef Vc_extralean#defineVc_extralean//exclude rarely used data from the Windows header#endif#include<afx.h>#include<afxwin.h>//MFC core components and standard components#include <afxext.h>//MFC Extensions#ifndef _afx_no_ole_support#include<afxdtctl.h>//MFC support for Internet Explorer 4 common Controls#endif#ifndef _afx_no_afxcmn_support#include<afxcmn.h>//MFC support for Windows common controls#endif //_afx_no_afxcmn_support#include<iostream>//TODO: Refer to other header files required by the program here//Stdafx.cpp//stdafx.cpp: Include only source files for standard include files//lanfileman.pch will act as a precompiled header//stdafx.obj will contain precompiled type information#include"stdafx.h"//TODO: In StdAfx. H Medium//reference any additional header files that you want, rather than referencing them in this file//Defineh.h#include <WinSock2.h>#defineSendfilesize (10 * 1024 * 1024)#defineServermode 1#defineClientmode 2#defineExitapp 2#defineREDO 1#defineMaxclientnum 10typedefenumresult{funcsucess,//function Execution succeededFuncfaild,//general function Execution failedWSAStartup, Createsocket, Bindsocket, LISTEN, ACCEPT, CREATEFILE, READFILE, SEND, RECV, CONNECT, WriteFile, SetEndOfFile, closesocket} Result;#defineWsareturn (NODE)if(Nresult! =0) {cout<<"Error code:"<< WSAGetLastError () <<GetString (NODE); returnNODE; }#defineWsacontinue (NODE)

Code Snippet 2: About #ifdef _debug in MFC #undef this_file static char this_file[]=__file__; #endif

Scenario 1:
#ifdef _DEBUG
virtual void AssertValid () const; ASSERT (Assert) valid (valid, correct)
virtual void Dump (cdumpcontext& dc) const; Storage context
#endif

These two functions are for debugging purposes, and the first function checks for availability, that is, whether it is valid. If the second function does not change, the final call is CWnd::D UMP (), Output window class name, title name, and a series of information (in the Output window).

#ifdef _DEBUG
#endif
This is conditional compilation, that is, if there is a # define _DEBUG, these two functions are compiled, otherwise ignored. When you build with debug (relative to release) the development environment is automatically added to this macro definition, and these two functions are valid.

Case 2:
#ifdef _debug                                    //Determine if _DEBUG
is defined #undef this_file                                 //Cancel the definition of this_file
Static char this_file[]=__file__;               //Definition this_file point to file name
#define NEW debug_new                            //Define debug new macro, replace the new keyword
#endif//End

If _DEBUG is defined, it is compiled in debug state, so the definition of two symbols is modified accordingly.
This_file is a char array global variable, and the string value is the full path of the current file, so the error handling code in the debug version when the program is faulted can be used to tell you which file has a problem with the code.
After defining _DEBUG, because the _DEBUG is defined, the compiler determines that this is a debug, compile code between #ifdef _DEBUG and #endif. #undef means to clear the currently defined macro so that this_file is not defined.
__FILE__ is one of the 6 macros that the compiler can recognize for pre-defined ANSI C.
Debug_new locates memory leaks and tracks file names and line numbers.

Scenario 3:
#ifdef _DEBUG//If the DEBUG state
#undef this_file//Clear This_file
static char this_file[]=__file__; Define This_file as __file__ (this is the full path name of the current file)
#define NEW DEBUG_NEW//define New as debug_new (this can detect a memory leak, which is actually the few debugging functions that can start with the CRT)
#endif

6 Macros for ANSI C:
__FILE__ is the pre-compiler constant, returns the currently compiled file name, and a few common precompiled constants;
__line__ the first line of the file that the compiler is compiling;
__DATE__ returns the current date jul-20-2004;
__TIME__ returns the current time hh:mm:ss;
The predefined compiler macros for __timestamp__ always return timestamp information. In the Pacific Standard Time zone, regardless of local time and CL.EXE run location on the computer.
__stdc__ conditional compilation, meaning: if standard C or C + + is defined, then compile the phrase until the previous source code is #endif.
_stdc__cplusplus both are standard macros, and _STDC_ indicates whether the standard c;_cplusplus indicates whether C + + is met.


Once explained that the use of __file__ macros multiple times, although the contents of the string is the same, but may be different address, that is, the same string constant used multiple times to occupy a different address, resulting in the need for memory increase. In order to detect internal leaks, the Debug version of new appends the file name of the call to new and the line number of the call, which is implemented by __FILE__ and __line__, both of which are predefined internal macros, and This_file instead of __fil E__ is designed to reduce the size of the program: If you have 10,000 calls to new in a file, then a constant string of 10,000 current filenames is generated (the first one is extended by the __FILE__ macro), and the resulting target file is very large, and the This_file Instead, there is only one copy of the current file name, and the This_file pointer is used to pass the file name.

    __file__ are compiler-defined macros as well as __line__. When __FILE__ is encountered, the compiler replaces __file__ with a string that is the path name of the currently compiled file. Instead of using __file__ directly in the definition of debug_new, this_file is used to reduce the size of the target file. Assuming that there are 100 uses of new in a CPP file, if you use __file__ directly, the compiler will produce 100 constant strings, and the 100 strings are the path names of the CPP file, which is clearly redundant. If you use This_file, the compiler produces only a constant string, and the 100 new call uses a pointer to a constant string.
   
    in MFC, you can use the DEBUG_NEW macro instead of the NEW operator to help locate a memory leak. In the "Debug" version of the program, DEBUG_NEW will track the file name and line number for each assigned object. When the "release" version of the program is compiled, debug_new resolves to a simple NEW operation that does not contain the file name and line number information. Therefore, there is no speed loss in the "release" version of the program. &NBSP;&NBSP
   
    If you do not want to rewrite the entire program to use DEBUG_NEW instead of NEW, you can define the following macro in the source file:   
   
    #define   new  debug_new  
     When an object dump is made, each object allocated with DEBUG_NEW will display the file and line number assigned to it, allowing you to pinpoint the source of the memory leak.   
   
    the "Debug" version of the MFC framework automatically uses DEBUG_NEW, but the code does not automatically use it. If you want to take advantage of the benefits of debug_new, you must explicitly use the Debug_new or #define NEW, as shown above.   

In general, this code is useful to prevent memory leaks and reduce program compilation overhead.

Advanced C + + programming

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.