Use of Macros in VC (reproduced)

Source: Internet
Author: User

 

From: http://blog.csdn.net/strikebone/archive/2007/11/02/1863523.aspx

 

Here is a question about macros. I have used ATL's String Conversion macros, including w2a, which I don't quite understand at first. To use these macros, you must use uses_conversion at the beginning of the function to initialize some local variables. Use it, but let's look at the macro definition. It has the following code:
// In atlconv. # define uses_conversion/INT _ convert;/uint _ ACP = getacp (); _ ACP;/lpcwstr _ lpw ;/? Why do we use "int X;" for the lpcstr _ LPA; _ LPA? -- this is followed by the variable declaration?

Many people have encountered this confusing problem and later found that the answer is simple: Prohibit the warning information of the compiler (warning ). If there is a single line of code:
Int X;
If X has never been used, the compiler reports the error "unreferenced local variable: X", which means that the local variable X has not been referenced. If the warning information output is adjusted to the maximum value. To avoid annoying warnings, uses_conversion references declared variables.
Int X; // declare X; // use this variable in the era before C ++, programmers sometimes use function parameters in C to avoid "unreferenced formal parameter" or other obscure compilation errors.
Void myfunc (int x, char y) {X; y;...} Of course, the following code can be used to accomplish the same thing more effectively:
// The parameter X is not void myfunc (INT/* x */){......} that is to say, the parameter is declared, but it cannot be named. Local variables cannot be used like this; it must be explicitly referenced. In this way, no commands are added to the Code. A maximum of several bytes may be added to the stack (Reserved space for X ). Clever compilers do not even worry about X being used. Although curiosity may wonder: If the compiler is powerful enough, why does it complain (compilation error) If X is never used? The answer is that demanding programmers (whose programming capabilities are related to their personal memory) Use a compilation warning to remind themselves to delete a part of the code when the variable is obsolete. This warning is useful in the C era. You must declare variables at the top of each function, so that you are far away from using their code.
Now let's look at another question: why do we need uses_conversion at the beginning? That is, Why do macros such as w2a and CO still need another macro to declare their own variables; why not declare this variable directly in w2a?
# Define w2a (X)/INT _ convert ;/...... ETC obviously cannot do this, because if you use w2a twice, you will get a copied variable. So why don't we put the whole macro into the new range created by the curly arc?
# Define w2a (x) q {/INT _ convert ;/...... /} Solves the naming conflict, but cannot be encoded as follows:
Dosomething (w2a (pwstr ));
There is no way to return values from the code block, so w2a cannot be passed in function calls. So how about inline functions?
Inline lpcstr w2a (lpwstr W) {int_convert ;......} This solves the range problem-any variable required by w2a can be declared in this function within its own range, without the need for another macro. It also provides a way to return values so that you can use w2a (x) in function calls and assignments ). However, this method is ineffective because w2a is more complicated than other macros.
No matter when Unicode conversion is performed, the string cannot be converted locally. A temporary string must be assigned to accommodate the converted bytes. Typically, assign a string by calling New:
Int Len = multibytetowidechar (...,? Mystr, null, 0); // or the length of lpwstr P = new wchar [nlen]; // allocate the memory multibytetowidechar (..., p, Len); // convert somecomfunction (p); // use the Delete [] P; // destroy this code is not only annoying, but also inefficient; multibytetowidechar must be called twice (one is the calculation length and one is the actual conversion). You have to allocate P from the heap, which is very slow. Solve the first problem by allocating 2 * len bytes. Here the length Len is the length of the ASCII string-but what should we do with the second problem? If you want to see how a2w is expanded, see:
// Simplified version # define a2w (s)/_ Len = 2 * strlen (s); afxa2whelper (lpwstr) alloca (_ Len); afxa2whelper is an auxiliary function that calls multibytetowidechar. A2w uses 2 * Len to cleverly avoid calling multibytetowidechar twice. However, a2w and other conversion macros are really smart. Instead of calling the new operation to assign a temporary string, they call alloca to allocate bytes in the stack rather than in the heap. This is very fast, because the compiler only needs to add the stack pointer. Do not call a function or process memory blocks. It also avoids memory fragments and does not need to call the delete operation because the memory is automatically released when the address called to control the exit from alloca. This shows why a2w cannot be used as an inline function. If so, the temporary string created by alloca will be destroyed before the return, in addition, you will terminate the call of somecomfunction (in this example) by deleting the string.
A2w must call alloca from the same address called by alloca -- so a2w must be a macro rather than a function; therefore, it requires another macro users_conversion to declare _ Len and other variables used (I omitted it for simplicity ). When you think about it, the whole process tells us that writing a set of macros like a2w to allocate memory from the stack will reduce a lot of unnecessary trouble.
In addition, you can call alloca whenever you want to quickly obtain the temporary memory. The following code is common:
Char * P = new char [Len]; dosomething (p); Delete [] P; it is more efficient to replace the following code:
Char * P = (char *) alloca (LEN); dosomething (p); // Delete P is not required! Of course, if there are not so many in the stack, you will see a nasty message box. This method has some limitations. For more information, see the documentation. Experience tells me that, no matter when you find something strange in MFC, ATL or somewhere else, it is best to drill into it and you may find something useful.

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.