[Good Programming habits] Use sizeof to reduce memory operation errors

Source: Internet
Author: User

In programming, in a large number of cases, you need to use the memset () function to set the memory to zero initialization. In addition to the memset () mentioned here, there are actually many functions, such as snprintf (), strncpy () and so on can all borrow the method mentioned here). The following three situations are errors often encountered in this aspect during work, most of which are caused by negligence.
Example 1

Char * buf [FAIP_WITHDOT_MAX_LEN + 1];
Memset (buf, 0, FAIP_WITHDOT_MAX_LEN + 1 );

Example 2

# Define DIGEST_LEN 17
# Define DIGEST_MAX 16

Char digest [DIGEST_MAX];
Memset (digest, 0, DIGEST_LEN );

Example 3

Dll_node_t * p_node = malloc (sizeof (dll_node_t ));
If (p_node = 0)
Return;
Memset (p_node, 0, sizeof (dll_t ));


The following example shows how to use the general method to correct the above three errors.
Example 1 Revision 1

Char * buf [FAIP_WITHDOT_MAX_LEN + 1];
Memset (buf, 0, sizeof (char *) * (FAIP_WITHDOT_MAX_LEN + 1 ));

Example 2 Revision 1

# Define DIGEST_LEN 17
# Define DIGEST_MAX 16

Char digest [DIGEST_MAX];
Memset (digest, 0, DIGEST_MAX );

Example 3 Revision 1

Dll_node_t * p_node = malloc (sizeof (dll_node_t ));
If (p_node = 0)
Return;
Memset (p_node, 0, sizeof (dll_node_t ));


You need to think about how to avoid such "low-level" errors as much as possible at work. Although code review can be used to increase the error detection rate, this effect is not good. A better way is for programmers to develop certain programming habits. For example, using the sizeof () method to be proposed here can significantly reduce such errors. The following example shows how to use sizeof () to correct errors.
Example 1 Revision 2

Char * buf [FAIP_WITHDOT_MAX_LEN + 1];
Memset (buf, 0, sizeof (buf ));

Example 2 Revision 2

# Define DIGEST_LEN 17
# Define DIGEST_MAX 16

Char digest [DIGEST_MAX];
Memset (digest, 0, sizeof (digest ));

Example 3 Revision 2

Dll_node_t * p_node = malloc (sizeof (dll_node_t ));
If (p_node = 0)
Return;
Memset (p_node, 0, sizeof (* p_node ));


What is the idea behind sizeof () to avoid errors? The human brain is doomed to go wrong. If there are a large number of rules in the brain to process the same transaction or there are no rules at all, it is often easier to handle this transaction. Using the memset () function to initialize the memory is a type of transaction that is prone to errors, because when you specify the size of the actually initialized memory, the general method is to determine the memory size passed to the memset () function based on the context of the initialized memory. If sizeof () is used to obtain the size of the memory required for the first time and solidify it into a programming habit, it is not prone to errors.

For the first and second errors, you only need to treat the array as the sizeof () parameter, without worrying about the size of the array during definition. Even if the size of the target array is changed as needed, sizeof () returns the actual size of the modified array, without making any manual changes to the call of the memset () function. For the third type of error, the method is similar, except in the form of sizeof (* xxx), where xxx is the target pointer variable, such as p_node in the third example.

Have you noticed the commonality of the sizeof () method? Both use the target variable to be initialized as the sizeof () parameter, rather than the type or macro of the target variable as its parameter. This makes the written program more error-prone. The sizeof () method is used to simplify the size of the initialized memory to only two rules:
1) if the target variable is an array, the memory size is obtained in sizeof (xxx) format, where xxx is the variable name of the index group, obviously, this array variable can be nested in another data structure.
2) If the target variable is a pointer to the data structure, sizeof (* xxx) is used to obtain the memory size, where xxx is the index data structure pointer variable name rather than the data structure type.

The last question is, will sizeof () Reduce program performance? No! Because sizeof () is calculated by the compiler during program compilation and finally generates a number, it is a static behavior rather than a runtime behavior.

This article from "to Jane Li cloud" blog, please be sure to keep this source http://yunli.blog.51cto.com/831344/227130

Related Article

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.