6

I have initialised the entire array with value 1 but the output is showing some garbage value. But this program works correctly if i use 0 or -1 in place of 1. So are there some restrictions on what type of values can be initialised using memset.

 int main(){
    int a[100];
    memset(a,1,sizeof(a));
    cout<<a[5]<<endl;
    return 0;
    }
4
  • yup it works with -1 . you can try it Commented Sep 21, 2015 at 10:40
  • You should probably first figure out what sizeof(a) does, and then you should figure out what is the type of the third argument of memset(). This should clear it up. PS. Don't do this in C++, it is unnecessary, and as you see, wrong. Commented Sep 21, 2015 at 10:43
  • 1
    If you have a modern compiler, you might want to try std::fill(std::begin(a), std::end(a), 1); Commented Sep 21, 2015 at 11:57
  • 1
    Your code isn't C++, it's C. If you really use a C++ compiler, don't write C code. Commented Sep 21, 2015 at 12:58

4 Answers 4

7

memset, as the other say, sets every byte of the array at the specified value.

The reason this works with 0 and -1 is because both use the same repeating pattern on arbitrary sizes:

(int) -1 is 0xffffffff
(char) -1 is 0xff

so filling a memory region with 0xff will effectively fill the array with -1.

However, if you're filling it with 1, you are setting every byte to 0x01; hence, it would be the same as setting every integer to the value 0x01010101, which is very unlikely what you want.

Sign up to request clarification or add additional context in comments.

Comments

3

Memset fills bytes, from cppreference:

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

Your int takes several bytes, e.g. a 32bit int will be filled with 1,1,1,1 (in base 256, endianess doesn't matter in this case), which you then falsly interpreted as a "garbage" value.

1 Comment

Specifically 0x01010101 which is what you'll be forming in each a is 16843009 decimal, which I predict is the "garbage" value you're seeing. -1 "works" (for the wrong definition of "works") because it fills the array's memory with 0xff and 0xffffffff is the 32-bit representation of -1.
2

The other answers have explained std::memset already. But it's best to avoid such low level features and program at a higher level. So just use the Standard Library and its C++11 std::array

#include <array>

std::array<int, 100> a;
a.fill(1);

Or if you prefer C-style arrays, still use the Standard Library with the std::fill algorithm as indicated by @BoPersson

#include <algorithm>
#include <iterator>

int a[100];
std::fill(std::begin(a), std::end(a), 1);

In most implementations, both versions will call std::memset if it is safe to do so.

2 Comments

little typo, should be std::end(a)
@karsten tnx, fixed
-2

memset is an operation that sets bits.

If you want to set a value use a for-loop.

Consider a 4-bit-integer:

Its value is 1 when the bits are 0001 but memset sets it to 1111

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.