[Original] A knowledge point caused by memset

Source: Internet
Author: User


[Original] A knowledge point caused by memset

 

When debugging a base Sorting Problem, I want to assign an initial value to the int * that was initially allocated, similar to zeromemory on windows, but I found that things are not that simple. Example: # include <iostream> # include <cstring> using namespace STD; int main (){ Int * C = new int [10]; Memset (C, 0x0, 10 ); For (INT I = 0; I <10; I ++) cout <C [I] <"->"; cout <"\ n "; return 0 ;} Output: 0-> 0-> 1193541632-> 1397052160-> 1313818963-> 1162690894-> 1852785469-> 1701605235-> 1937330944-> 1148020084-> 1. Memset (C, 0x0, 10 ); => Memset (C, 0x0, 10 * sizeof (INT) ); Output: 0-> 0-> 0-> 0-> 0-> 0-> 0-> 0-> 0-> 0-> 2. memset (C, 0x0 , 10); => memset (C, 0x1 , 10); output: 16843009-> 16843009 -> 1193541889-> 1397052160-> 1313818963-> 1162690894-> 1852785469-> 1701605235-> 1937330944-> 1148020084-> 3. memset (C, 0x0, 10); => memset (C, 0x1, 10 * sizeof (INT) ); Output: 16843009-> 16843009-> 16843009-> 16843009-> 16843009-> 16843009-> 16843009-> 16843009-> 16843009-> reason (http://blog.ednchina.com/fpga2006/16745/message.aspx ): memset prototype: _ cribd void * _ cdecl _ mingw_nothrow memset (void *, Int, size_t); memset (A, 1, 20 ); it is to assign a value to the 20 bytes of memory that a points to. Each character is filled with the ASCII value 1. After being converted to binary, 1 is 00000001, which occupies one byte. An int element is 4 bytes, starting with 1000000010000000100000001, which is equal to 16843009, The assignment of an int element is completed. Another article Article The general usage of http://www.cppblog.com/SHFACM/archive/2009/02/04/73004.aspx memset can only be used to initialize an array of the char type, that is, it only accepts the value of 0x00-0xff, however, in most cases, it is incorrect to assign a relatively large or small value to an array of double or Int values: memset (ARR, 2147483647, sizeof (ARR )); however, you can use some tips to get a similar maximum value, such as memset (ARR, 0x7f, sizeof (ARR). It assigns all values in arr to 2139062143 (note: here, the value of ARR [0] of Int Is 0x7f7f7f7f = 2139062143. You may try it.) This is the maximum value that can be achieved by assigning values to int using memset: 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

 

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.