Some places of Linux attention

Source: Internet
Author: User
Tags assert

The prototype of an Assert macro is defined in <assert.h>, and its function is to terminate the program execution if its condition returns an error

#include <assert.h>
void assert (int expression);

The function of an assert is to evaluate an expression, if its value is false (that is, 0), then it prints an error message to stderr and then terminates the program by calling Abort

The disadvantage of using assert () is that frequent calls can greatly affect the performance of the program and add additional overhead. After debugging, you can disable the Assert call by inserting the #define NDEBUG before the statement that contains # include <assert.h>, as shown in the following example code:

#include <stdio.h>
#define Ndebug
#include <assert.h>

Usage Summary and Precautions:

1) Verify the legitimacy of incoming parameters at the beginning of the function, such as:

int resetbuffersize (int nnewsize)
{
Function: Change buffer size,
Parameter: Nnewsize buffer new length
Return value: Buffer current length
Note: Keep the original information content unchanged nnewsize<=0 means clear buffer
ASSERT (nnewsize >= 0);
ASSERT (Nnewsize <= max_buffer_size);
...
}


2) Each assert examines only one condition, because when multiple conditions are checked, if the assertion fails, it is not possible to visually determine which condition failed, such as:

Not good:

ASSERT (noffset>=0 && noffset+nsize<=m_ninfomationsize);

Good:

ASSERT (noffset >= 0);
ASSERT (Noffset+nsize <= m_ninfomationsize);

3) You cannot use a statement that alters the environment, because assert only takes effect in debug, and if you do, you will use the program to run into problems when it is actually running, such as:

Error:

ASSERT (i++ < 100);

This is because if there is an error, such as i=100 before execution, then this statement will not be executed, then i++ This command will not be executed.

That's right:

ASSERT (I < 100);
i++;

4) The Assert and subsequent statements should be empty lines to form a logical and visual sense of consistency.

5) In some places, assert cannot replace conditional filtering.

Transferred from: http://www.cnblogs.com/ggzss/archive/2011/08/18/2145017.html

C + + in a ' # ' and two ' # # ' meaning:

Original link: http://thatax.blog.163.com/blog/static/20892680200882391827116/

# # (two number of Wells) and # (a pound) What do you mean?

Connector # # (two number of wells)

Do not know what symbol # (a pound sign)

# # The connection symbol consists of two well numbers, and its function is to join the two substrings (tokens) in the macro definition with parameters to form a new substring. But it cannot be the first or last substring. The so-called substring (token) refers to the smallest syntax unit that the compiler can recognize. Specific definitions are explained in detail in the compilation principle.

#符是把传递过来的参数当成字符串进行替代.

Suppose a macro with a parameter has been defined in the program:

#define PRINT (N) printf ("token" #n "=%d", token# #n)

It also defines two shaping variables:

int token9 = 9;

Now call this macro in the main program in the following way:

PRINT (9);

So at compile time, the above sentence is extended to:

printf ("token" "9" "=%d", token9);

Note that in this example, PRINT (9), in which the "9" is treated as a string, is connected to "token" and becomes a token9. And #n is replaced by "9".

It is conceivable that the result of the above program operation is to print on the screen

Token9=9

Still a little confused?!

One more example:

#define PRINT (N) printf ("token" #n "=%d", game# #n)

int token9 = 9;

int game9 = 99;

Call:

PRINT (9);

Print on the screen:

TOKEN9 = 99

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.