2

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.

Aaron Yodaiken
  • 19,163
  • 32
  • 103
  • 184
John
  • 794
  • 2
  • 18
  • 34
  • possible duplicate of [How to initialize an array in C](http://stackoverflow.com/questions/201101/how-to-initialize-an-array-in-c), [C++ array initialization](http://stackoverflow.com/questions/1920430/) – outis Dec 22 '11 at 05: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, – John Dec 22 '11 at 05: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](http://meta.stackexchange.com/questions/66377/). – outis Dec 22 '11 at 05:27
  • may be `std::fill_n(&bit_table_[0][0],sizeof(bit_table_),0x00);` will work. – Donotalo Dec 22 '11 at 05:28
  • 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. – outis Dec 22 '11 at 05:29
  • i have to use the fill_n because i have read somewhere that memset is not always safe – John Dec 22 '11 at 05: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. – Billy ONeal Dec 22 '11 at 05:52
  • @luxun: Sorry I never have done so. I am just new to this kind of Q&As. I have found many helpful answers from you guys and wanna thank you all for that. And I will remember to accept answers. – John Dec 22 '11 at 06:07

3 Answers3

5

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);
Benjamin Lindley
  • 101,917
  • 9
  • 204
  • 274
  • 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 Dec 22 '11 at 06:09
  • @John: For a member, there is another way, see updated answer. – Benjamin Lindley Dec 22 '11 at 06:15
  • 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);` – David Feb 13 '18 at 21:38
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.

MSN
  • 53,214
  • 7
  • 75
  • 105
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;

}
Software_Designer
  • 8,490
  • 3
  • 24
  • 28