[C + +] memset and sizeof use note

Source: Internet
Author: User

Because it is often necessary to clear the array when writing small topics using C + +, note the use of the sizeof operator for the memset function.

Memset is characterized by the sequential memory (including the given address) after the given address, which is initialized one byte to the value indicated in the parameter.

Because it is byte-wise initialized, memset is generally used only for emptying (assigned to 0)

If you do not assign a value of 0, what is the result? Assuming for int a[], if you use memset (A, 1, sizeof (a)), then each value of array A is initialized to 0x01010101

Because of this, generally only memset (a, 0, sizeof (a)) will appear

For a heap allocation array, int *a = new Int[n], memset (A, 0, sizeof (a[0]) *n) can also achieve the effect, but note that the third parameter here cannot use sizeof (a), but to indicate the number of bytes.

To know why, you need to know the role of the sizeof operator. It returns the number of bytes occupied by the stack space. If the array is declared in the form of an int a[n], then sizeof (a) returns the number of bytes occupied by the entire array of a. If you return with int *a = new Int[n],sizeof (a) is still the byte number occupied by an int *, that is, the 32-bit compiler returns 4, and the 64-bit compiler returns 8. It is important to note that for both compilers, sizeof has a difference in int *, and sizeof (int) or sizeof (a) (A is a parameter declared as int) returns 4 on both compilers.

Here is a test program. The compilation environment is GCC 4.8.2 64-bit

    int*B; intb; cout<<"sizeof B:"<<sizeof(B) << Endl;//8, because my compiler is 64-bitcout <<"sizeof B:"<<sizeof(b) << Endl;//4, regardless of 32-bit or 64-bit compiler, int takes up 4 bytesB=New int[5]; b[4] =7; b[3] =6; b[2] =5; b[1] =3; b[0] =1; cout<<"-------B---------\ n";     for(i =0; I <5; ++i) cout << B[i] <<' ';//1 3 5) 6 7cout <<Endl; memset (B,0,sizeof(B));  for(i =0; I <5; ++i) cout << B[i] <<' ';//0 0 5 6 7 because B accounted for 8 bytes, so the first 8 byte 0cout <<Endl; intc[3]; c[2] =6; c[1] =4; c[0] =2; cout<<"\ n--------C--------\ n"; cout<<"size of C:"<<sizeof(C) << Endl;//12, returns the number of bytes occupied by the array     for(i =0; I <3; ++i) cout << C[i] <<' ';//2 4 6cout <<Endl; memset (C,0,sizeof(C));  for(i =0; I <3; ++i) cout << C[i] <<' ';//0 0 0cout << Endl; 

Knowing the above principle, let's take a look at the initialization of a two-dimensional array.

int a[2][3] For such a definition, we can still use memset (a, 0, sizeof (a)) to complete the initialization of the entire two-dimensional array, because the two-dimensional array declared in this way is essentially a one-dimensional arrangement.

If it is an int **a, then dynamically declare a 2*3 two-dimensional array? Can we do the initialization of a two-dimensional array, as above, by memset (A, 0, sizeof (a[0][0]) *2*3)?

The answer, of course, is no.

A dynamically allocated one-dimensional array can still be assigned with memset (although the third parameter specifies the number of bytes), because in the heap, the members of one-dimensional arrays are still continuously arranged.

and is the int member in the dynamically allocated two-dimensional array still arranged in the heap area? Of course not, since it is not, memset can not get the desired result. What's the actual result? Memset will empty the contiguous memory that holds the level two pointer, with some other areas of memory removed. Any subsequent access to such things as a[0][1] would throw a memory error, since the structure of the two-dimensional array has been compromised.

High-dimensional arrays are similar to two-dimensional arrays when initialized, and are not discussed here.

[C + +] memset and sizeof use note

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.