# "Memset" about the initial maximum minimum value of memset

Source: Internet
Author: User

Statement: Also from the internet on the road of the great God.

The normal usage of memset is that it can only be used to initialize an array of type char, that is, it only accepts 0x00-0xff assignments .

Since char is 1 bytes, memset is assigned by Byte, which is equivalent to setting each byte to that number, so the array of char type can be assigned any value ;

For an int type that is also commonly used, int is 4 bytes, when memset (, 1,sizeof ()), 1 is equivalent to ASSCII code to binary 00000001, as a byte, a byte 8 bits, an int of 4 bytes, So the initialization of each number is 00000001000000010000000100000001 = 16843009;

Memset (, 0xff,sizeof ()), 0xff to binary 11111111,int is 4 bytes so the last 11111111111111111111111111111111 is-1. (as a binary complement and then assign a value).

Can be fully assigned to 0,0 bits 000000000000000000000000000000000, or -1,-1 binary is 11111111111111111111111111111111, So memset can be initialized directly (0,-1);
For example: 0xFF to bits 11111111, just one, 0x1f less than 0xFF, and 0x59 is less than 0xff, so these can be used to initialize, as long as can fill 8 bits of the binary, you can.
If you want to maximize the initial, the first bit is the sign bit, cannot be 1, the rest is all 1, that is, 7 1,1111111 to hexadecimal just for 0x7f, so memset (, 0x7f,sizeof ());

the technique of setting infinite constants in Memset
If the scope of the data in the problem is clear, then infinity is not a problem, in the case of unclear, many programmers take0x7fffffffAs infinitely large as this ismaximum value of 32-bit int。 If this infinity is used only for general comparisons (such as the initial value of the min variable at the minimum), then 0x7fffffff is indeed a perfect choice, but in more cases0x7fffffff is not a good choice.。
Most of the time we do not simply compare the infinity, but we will do the comparison after the operation, for example, in most of the shortest path algorithm will be used in the relaxation operation:
if (D[u]+w[u][v]<d[v]) d[v]=d[u]+w[u][v];
We know that if there is no edge between u,v, then W[u][v]=inf, if our INF takes 0x7fffffff, then d[u]+w[u][v] will overflow and become negative, our relaxation operation is wrong, more generally speaking, 0x7fffffff can not meet the " Infinity plus a poor number is still infinite ", it becomes a very small negative.
In addition to being satisfied plus a constant is still infinity, our constants should also meet "infinity plus infinity is still infinite", at least two infinite sum should not be catastrophic error, this point 0x7fffffff still can not satisfy us.
So we need a better guy to replace 0x7fffffff, the most rigorous way of course is to deal with Infinity in particular rather than find a big big constant to replace it (or simulate it), but this will make our programming process cumbersome. In the code I've read,The most ingenious infinity constant value is 0x3f3f3f3f, I don't know who was the first to use this subtle constant to do infinity, but I did learn from a Acmer Id:staginner blog that he/she used this constant in many of his code, so I tried it myself and found it very useful, And when I do a more in-depth analysis of this constant, I find it really ingenious.

0x3f3f3f3f decimal is 1061109567, that is, 10^9 level (and 0x7fffffff an order of magnitude), and the general situation of the data is less than 10^9, so it can be used as infinity without the case of data greater than infinity.
On the other hand, since the general data is not greater than 10^9, when we add infinity to a data, it does not overflow (this satisfies the "infinity plus a poor number is still infinite"), in fact 0x3f3f3f3f+0x3f3f3f3f= 2122219134, this is very large but not more than the 32-bit int range, so 0x3f3f3f3f also meet our "Infinity plus infinity or infinity" demand.
Finally, 0X3F3F3F3F can bring us an unexpected additional benefit: if we want to clear an array, we usually use code like memset (A,0,sizeof (a)) to do it (convenient and efficient), But when we want to assign an array to infinity (for example, to initialize the adjacency matrix when solving graph theory problems), you can't use the memset function to write loops (it's really painful to write these unimportant code), and we know that because Memset is in bytes, It can clear the array 0 because 0 of each byte is 0, now well, if we set infinity as 0x3f3f3f3f, then the miracle happened, 0x3f3f3f3f each byte is 0x3f! So to set a memory to infinity, we only need memset (A,0x3f,sizeof (a)).

So in the usual situation, 0x3f3f3f3f really is a great choice.

Other assignment values:

memset (arr,0x7f,sizeof (arr)); It assigns all values in arr to 2139062143, which is the maximum value that can be achieved by assigning a value of memset to int

Similar to the following:

memset (arr,0x80,sizeof (arr)); Set int to-2139062144
memset (arr,0x7f,sizeof (arr)); Set double to 1.38242e+306
memset (arr,0xfe,sizeof (arr)); Set Double to-5.31401e+303

"Memset" about the initial maximum minimum value of memset

Related Keywords:

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.