3

I have this array

 unsigned char        bit_table_[10][100];

What is the right way to fill it with 0. I tried

std::fill_n(bit_table_,sizeof(bit_table_),0x00);

but vc 2010 flags it as error.

8
  • possible duplicate of How to initialize an array in C, C++ array initialization Commented Dec 22, 2011 at 5:23
  • i have no problem in initializing it when it is just a one dimension. My problem is when I have two dimension or more, Commented Dec 22, 2011 at 5:26
  • Is the use of fill_n a requirement or simply how you're attempting to solve another problem? Be careful your question doesn't suffer from the XY problem. Commented Dec 22, 2011 at 5:27
  • 1
    bit_table_ isn't a two-dimensional array, it's an array of pointers. If the elements of bit_table_ are supposed to point to arrays, you'll need to allocate those before initializing them. In either case, this is still a duplicate, as there are also plenty of questions about zero-initializing multidimensional arrays. Commented Dec 22, 2011 at 5:29
  • 2
    @John: memset is generally safe for data types for which the zero bit pattern is well defined. That's the case for every builtin type (of which I am aware...), as well as every POD type (again, of which I am aware). More to the point, if you're seeing a compiler error, include the entire error (minus long file names, of course) in the question. It makes it easier for people to see why the compiler may be complaining; often the fault is not in the code that you post. Commented Dec 22, 2011 at 5:52

3 Answers 3

6

On initialization:

unsigned char bit_table_[10][100] = {};

If it's a class member, you can initialize it in the constructor, like this:

MyClass::MyClass()
    :bit_table_()
{}

Otherwise:

std::fill_n(*bit_table_,sizeof(bit_table_),0);
Sign up to request clarification or add additional context in comments.

3 Comments

i have tried the first one but it is not possible because bit_table_ is data member of a class. The second one, I will just try it now.
@John: For a member, there is another way, see updated answer.
sizeof(bit_table_) works in this case because you are using byte-sized pieces. If you want to use fill_n on something bigger: double doubles[10][100]; then you use the dimensions of the array for the second parameter: std::fill_n(*doubles, 10*100, 123.45);
2

The type of bit_table_ is unsigned char [10][100], which will decay (that is, the compiler allows it to be implicitly converted to) into unsigned char (*)[100], that is, a pointer to an array of 100 unsigned chars.

std::fill_n(bit_table_, ...) is then instantiated as: std::fill_n(unsigned char (*)[100], ...) which means it expects a value of type unsigned char [100] to initialize bit_table_ with. 0 is not convertible to that type, so the compilation fails.

Another way to think about it is that the STL functions that deal with iterators only deal with a single dimension. If you are passing in a multidimensional structure those STL functions will only deal with a single dimension.

Ultimately, you can't do this; there is no way to assign to an array type. I.e., since you can't do this:

char table[100];
char another_table[100]= { };
table= another_table;

you can't use std::fill_n on multidimensional arrays.

Comments

0

You can also try unsigned char bit_table_[10][100]= { 0 } to fill it with zeros.

int main()
{
    unsigned char        bit_table_[10][100]= { 0 };
    return 0;

}

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.