28

How do I clear/empty a C++ array? Theres array::fill, but looks like its C++11 only? I am using VC++ 2010. How do I empty it (reset to all 0)?

10
  • 1
    Resetting all to 0 is not emptying. Commented Nov 9, 2012 at 12:39
  • What do you mean clear/empty? An array is never empty. Commented Nov 9, 2012 at 12:39
  • Huh? The array of zero-size is mostly empty. Commented Nov 9, 2012 at 12:41
  • 1
    @MichaelKrelin-hacker Right, I should have phrased it "an array can never be emptied" Commented Nov 9, 2012 at 12:48
  • 1
    @juanchopanza More generally: the size of an array can never be changes. (Emptying is just a special case of the more general rule.) And of course, since there's no case where you'd ever dynamically allocate an array, it's probably OK to say that an array can never be empty. Commented Nov 9, 2012 at 12:50

7 Answers 7

40
std::fill_n(array, elementCount, 0);

Assuming array is a normal array (e.g. int[])

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

1 Comment

The C++ standard type (since C++11) array<T, N> has a member function fill(Value) will do. The underlying implementation may use memset.
12

Assuming a C-style array a of size N, with elements of a type implicitly convertible from 0, the following sets all the elements to values constructed from 0.

std::fill(a, a+N, 0);

Note that this is not the same as "emptying" or "clearing".

Edit: Following james Kanze's suggestion, in C++11 you could use the more idiomatic alternative

std::fill( std::begin( a ), std::end( a ), 0 );

In the absence of C++11, you could roll out your own solution along these lines:

template <typename T, std::size_t N> T* end_(T(&arr)[N]) { return arr + N; }

template <typename T, std::size_t N> T* begin_(T(&arr)[N]) { return arr; }

std::fill( begin_( a ), end_( a ), 0 );

4 Comments

In C++11, you could also write std::fill( std::begin( a ), std::end( a ), 0 ). Somehow this seems more idiomatic. (And is there anyone using pre-C++11 who doesn't have something similar in their toolkit?)
@MichaelKrelin-hacker, what I interpret it as is setting it to its default values, so for int, its 0? Ok, maybe its not the right word to use. But I'd like to know what it actually meant
@JamesKanze Agreed, but since OP implied C++11 was out of bounds, I presented the less elegant solution. I will added some (not fully tested) home-made solution.
@JiewMeng you could make that clearer in your question. Note you didn't even mention your array was holding ints!
8
std::fill(a.begin(),a.end(),0);

Comments

0

Hey i think The fastest way to handle that kind of operation is to memset() the memory.

Example-

memset(&myPage.pageArray[0][0], 0, sizeof(myPage.pageArray));

A similar C++ way would be to use std::fill

char *begin = myPage.pageArray[0][0];
char *end = begin + sizeof(myPage.pageArray);
std::fill(begin, end, 0);

3 Comments

std::memset is not faster than std::fill.
@KonradRudolph cole std::fill(buf, buf + n, '\0'); would be transformed to memset
@AndrewPilikin Exactly. That’s my point.
0

If only to 0 then you can use memset:

int* a = new int[6];

memset(a, 0, 6*sizeof(int));

18 Comments

Please, no memset. Use the appropriate C++ equivalents instead of the C functions. And don’t use manually managed memory for dynamic arrays. Use std::vector instead.
Why not? memset is just fine, but only provided this is a kind of array the OP meant. Another - more serious - issue is that the second parameter is count. Meaning 6*sizeof(*a).
@Michael Among other things, it only works for PODs so it has very limited scope. It simply has no place in C++, it’s wholly replaced by std::fill.
No, memset is still allowed even if you dislike it. And it's a joy to see memset once in a while for an old-fashioned hacker ;-) That said, I'm glad we both expressed our opinions. This way the empact will neither be eradication memset nor abuse thereof ;)
@KonradRudolph I agree with you: memset is too error prone, given that there are safer alternatives (which are typically faster as well). It has the additional disadvantage in what it tells the reader: that the author doesn't really know C++.
|
0

for (auto& a : SomeArray) a = 0;

Comments

0

Should you want to populate the array with something other than intrinsic values, std::fill wont cut it; instead I found std::generate useful. e.g. I had a vector of lists I wanted to initialize

std::generate(v.begin(), v.end(), [] () { return std::list<X>(); });

You can do ints too e.g.

std::generate(v.begin(), v.end(), [n = 0] () mutable { return n++; });

or just

std::generate(v.begin(), v.end(), [] (){ return 0; });

but I imagine std::fill is faster for the simplest case

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.