0

I have this "buggy" code :

int arr[15];
memset(arr, 1, sizeof(arr));

memset sets each byte to 1, but since int is generally 4-bytes, it won't give the desired output. I know that each int in the array will we initalized to 0x01010101 = 16843009. Since I have a weak (very) understanding of hex values and memory layouts, can someone explain why it gets initialized to that hex value ? What will be the case if I have say, 4, in place of 1 ?

Jarvis
  • 8,494
  • 3
  • 27
  • 58
  • I don't understand your question, because you already have the answer in your first line. *memset sets each byte to 1 = 0x01, since int is generally 4-bytes* 4 bytes will be set to 0x01010101 . Can you clarify your weakness of understand in the memory layout? This might be more helpful. – UmNyobe Feb 14 '17 at 06:46
  • Remember that `sizeof(arr)` == 60 (bytes) not 15 (integers). – acraig5075 Feb 14 '17 at 06:46
  • Yeah, so I know I'm assigning every 1 byte with a 4-byte value `1`, how can a 4-byte value be assigned to a single byte ? @acraig5075 – Jarvis Feb 14 '17 at 06:47
  • Are you sure your real question is not http://stackoverflow.com/questions/13477281/initializing-an-array-of-ints – UmNyobe Feb 14 '17 at 06:48
  • Nope. NOPE. That's why I wrote "buggy". @UmNyobe – Jarvis Feb 14 '17 at 06:49
  • @Jarvis: do you want to set '1' for each bit in array? – pstanisz Feb 14 '17 at 06:52
  • Nope, NOPE (again, and why would I do something like that). I was just asking how it happens since it's already happening. @pstanisz – Jarvis Feb 14 '17 at 06:53
  • My objective was to know how 4-byte value is assigned to 1-byte, and what will be the hexadecimal representations involved, which are resolved now, so please lift the down-vote from question if you have down-voted. @UmNyobe – Jarvis Feb 14 '17 at 07:23
  • May I point out `std::fill`? Works on arrays as well. – MSalters Feb 14 '17 at 12:51

3 Answers3

2

If I trust the man page

The memset() function writes len bytes of value c (converted to an unsigned char) to the string b.

In your case it will convert 0x00000001 (as an int) into 0x01 (as an unsigned char), then fill each byte of the memory with this value. You can fit 4 of that in an int, that is, each int will become 0x01010101.

If you had 4, it would be casted into the unsigned char 0x04, and each int would be filled with 0x04040404.

Does that make sense to you ?

Antoine Trouve
  • 1,198
  • 10
  • 21
1

What memset does is

Converts the value ch to unsigned char and copies it into each of the first count characters of the object pointed to by dest.

So, first your value (1) will be converted to unsigned char, which occupies 1 byte, so that will be 0b00000001. Then memset will fill the whole array's memory with these values. Since an int takes 4 bytes on your machine, the value of each int int the array would be 00000001000000010000000100000001 which is 16843009. If you place another value instead of 1, the array's memory will be filled with that value instead.

SingerOfTheFall
  • 29,228
  • 8
  • 68
  • 105
0

Note that memset converts its second argument to an unsigned char which is one byte. One byte is eight bits, and you're setting each byte to the value 1. So we get

0b00000001 00000001 00000001 00000001

or in hexadecimal,

0x01010101

or the decimal number 16843009. Why that value? Because

0b00000001000000010000000100000001 = 1*2^0 + 1*2^8 + 1*2^16 + 1*2^24
                                   = 1 + 256 + 65536 + 16777216
                                   = 16843009

Each group of four binary digits corresponds to one hexadecimal digit. Since 0b0000 = 0x0 and 0b0001 = 0x1, your final value is 0x01010101. With memset(arr, 4, sizeof(arr)); you would get 0x04040404 and with 12 you would get 0x0c0c0c0c.

Greg Kikola
  • 573
  • 3
  • 6