An in-depth understanding of empty array and string Initialization

Source: Internet
Author: User

An in-depth understanding of empty array and string Initialization
Question

1. initialize a one-dimensional array. You can assign values to only some elements, as shown below:

 

Int a [10] = {0, 1, 2, 3, 4 };
In this way, only the first five elements are assigned a value, and the last five elements are 0.
Consider char p [8]; as a character array, charp [8] = {0}; indicates that the first element is 0, and the subsequent seven elements are also 0;
Therefore, char p [8] ={} is the same as charp [8] = {0}. 8 elements are initialized to 0.
2. Use string constants to initialize character Arrays
Char p [10] = "china ";
The first five elements are 'C', 'h', 'I', 'n', 'A ',
The 6th elements are '\ 0'
The last four elements are null characters, that is, '\ 0'
Therefore, use char p [8] = ""; for initialization, the eight elements are '\ 0 ';
// '0' indicates the end mark, representing a character with an ASCII code of 0
Therefore, both char p [8] = ""; and char p [8] = {0}; can initialize eight elements to 0;
Char p [8] = {0}; as a character array, regular array initialization;
Char p [8] = ""; initialize with a String constant

Notes

 

 

#define ARRAYSIZE 2048voidmain() {chararrayA[ARRAYSIZE]={0};chararrayB[ARRAYSIZE];memset(array,0, ARRAYSIZE);}

 

Char arrayA [ARRAYSIZE] = {0 };
For compilation, arrayA [0] is assigned 0 values first, and then memset is called to initialize other array elements.

Char arrayA [ARRAYSIZE] = {0 };
It is difficult to calculate the efficiency than to use memset, because in summary, I think it is difficult to assign a value to a piece of memory to surpass memset.

 

Memset

Syntax:

# Include Void * memset (void * buffer, intch, size_tcount );

Memset () Copies ch to the count character before buffer and returns buffer. Memset () is very useful for initializing a memory segment with a certain value. For example, this command:

ConstintARRAY_LENGTH = 300; charthe_array [ARRAY_LENGTH]; // zero out of the contents of the_array memset (the_array, '\ 0', ARRAY_LENGTH );

... Is a very efficient way to set all values in the_array to zero.

The following table compares two different methods to initialize character Arrays: for Loop and memeset (). As the initial data volume increases, memset () is clearly faster:

Time consumed For Loop Memset
1000 0.016 0.017
10000 0.055 0.013
100000 0.443 0.029
1000000 4.337 0.291

 

Several immature suggestions

 

1: Do not use memset to clear memory unless necessary, especially for large memory blocks.

2: If a struct or memory block is assigned values one by one before use, memset Initialization is not required.

3: You can assign 0 values to the first byte of a string during initialization.

The string copied using strcpy does not need to be initialized.

For strings copied using strncpy, we recommend that you do not clear all memset values first. Instead, after strncpy, it is set to zero for the next byte of the string based on the actual length of the string.

When making process judgments, try to reduce the use of string comparison, and use integer or Boolean comparison.

4: Reduce struct conversion and copy code. We recommend that you use memcpy with caution when copying a struct, especially a struct that contains long strings, and assign values one by one.

5: if you do not need to use malloc or free, it will not only cause memory leakage, but also affect the allocation efficiency (for example, when there are many memory fragments) when the system dynamically allocates memory quickly ). We recommend that you define a large enough array for some dynamically allocated arrays.(Here is a question: what is the maximum size of a local variable? That is to say, what is the process stack space? This should be restricted, but what are the limits on each operating system or compiler? Should it be impossible to define a dozens of M or a GB array in the stack space ?)

6: unix programs run in process mode and can be used properly.Global static variables. Reduce the memory allocation overhead.Use global variables with caution in multithreaded programs.
 



 

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.