views:

1034

answers:

10

I want to repeatedly zero a large 2d array in C. This is what I do at the moment:

for(j = 0; j < n; j++)
{
    for(i = 0; i < n; i++)
    {  
        array[i][j] = 0;
    }
}

I've tried using memset:

memset(array, 0, sizeof(array))

But this only works for 1D arrays. When I printf the contents of the 2D array, the first row is zeroes, but then I got a load of random large numbers and it crashes.

+35  A: 
memset(array, 0, sizeof(array[0][0]) * m * n);

Where m and n are the width and height of the two-dimensional array (in your example, you have a square two-dimensional array, so m == n).

James McNellis
i and j should be n
Skizz
@Skizz: Thanks!
James McNellis
It doesn't seem to work. I get 'process returned -1073741819' on codeblocks, which is a seg fault right?
Eddy
@Eddy: Show us the declaration of the array.
GMan
I bet it's crashing on other lines, not the `memset`, because you mentioned crashing from zeroing out just one row too.
Blindy
int n = 10; int array[n][n];
Eddy
if I use 'memset(array, 0, sizeof(array[0]) * n * n)', it crashes on memset. If I use 'memset(array, 0, sizeof(array[0] * n)' then it crashes while its printing the array.
Eddy
@Eddy: What compiler are you using and does it support C99 variable length arrays (and have you enabled support for them)? With respect to your `memset` code, `sizeof(array[0] * n)` gives the size of the result of the expression `array[0] * n`, which is probably the same as `sizeof(array[0])`.
James McNellis
Sorry my bad, i made a typo in the printf routine. It works now
Eddy
Also, you need to change it to 'memset(array, 0, sizeof(array[0]) * n)' not 'memset(array, 0, sizeof(array[0]) * n * n)
Eddy
sizeof(array[0])*n is the correct size of the array, since the sizeof(array[0]) is the size of the entire first row, not just the first element
Eddy
@Eddy: Thanks; you're quite right; I missed a subscript. Sorry about that.
James McNellis
Huh. Just tried a test an array declared as `int d0=10, d1=20; int arr[d0][d1]`, and `memset(arr, 0, sizeof arr);` worked as expected (gcc 3.4.6, compiled with `-std=c99 -Wall` flags). I realize that "it works on my machine" means diddly squat, but `memset(arr, 0, sizeof arr);` **should** have worked. `sizeof arr` **should** return the number of bytes used by the entire array (d0 * d1 * sizeof(int)). `sizeof array[0] * m * n` will not give you the correct size of the array.
John Bode
@John Bode: True, but it depends how the array is obtained. If you have a function that takes a parameter `int array[][10]`, then `sizeof(array) == sizeof(int*)` since the size of the first dimension is not known. The OP didn't specify how the array was obtained.
James McNellis
+3  A: 

How was your 2D array declared?

If it something like:

int arr[20][30];

You can zero it by doing:

memset(arr, sizeof(int)*20*30);
Pablo Santa Cruz
A: 

check this out: so

Dani
A: 

This happens because sizeof(array) gives you the allocation size of the object pointed to by array. (array is just a pointer to the first row of your multidimensional array). However, you allocated j arrays of size i. Consequently, you need to multiply the size of one row, which is returned by sizeof(array) with the number of rows you allocated, e.g.:

bzero(array, sizeof(array) * j);

Also note that sizeof(array) will only work for statically allocated arrays. For a dynamically allocated array you would write

size_t arrayByteSize = sizeof(int) * i * j; 
int *array = malloc(array2dByteSite);
bzero(array, arrayByteSize);
VoidPointer
better use calloc
Benoît
The first part is wrong. For `sizeof` operator, `array` is not a pointer (if it was declared an array). See my answer for an example.
Alok
+7  A: 

Well, the fastest way to do it is to not do it at all.

Sounds odd I know, here's some pseudocode:

int array [][];
bool array_is_empty;


void ClearArray ()
{
   array_is_empty = true;
}

int ReadValue (int x, int y)
{
   return array_is_empty ? 0 : array [x][y];
}

void SetValue (int x, int y, int value)
{
   if (array_is_empty)
   {
      memset (array, 0, number of byte the array uses);
      array_is_empty = false;
   }
   array [x][y] = value;
}

Actually, it's still clearing the array, but only when something is being written to the array. This isn't a big advantage here. However, if the 2D array was implemented using, say, a quad tree (not a dynamic one mind), or a collection of rows of data, then you can localise the effect of the boolean flag, but you'd need more flags. In the quad tree just set the empty flag for the root node, in the array of rows just set the flag for each row.

Which leads to the question "why do you want to repeatedly zero a large 2d array"? What is the array used for? Is there a way to change the code so that the array doesn't need zeroing?

For example, if you had:

clear array
for each set of data
  for each element in data set
    array += element 

that is, use it for an accumulation buffer, then changing it like this would improve the performance no end:

 for set 0 and set 1
   for each element in each set
     array = element1 + element2

 for remaining data sets
   for each element in data set
     array += element 

This doesn't require the array to be cleared but still works. And that will be far faster than clearing the array. Like I said, the fastest way is to not do it in the first place.

Skizz
Interesting alternate way to look at the issue.
Beska
I am not sure adding an extra comparison/branch for every single read is worth deferring the initialization of the array in this case (though may be yours). If the array really is so big that the initialization time poses a serious concern, then he could use a hash instead.
tixxit
A: 

memset(array, 0, sizeof(int [n][n]));

swestrup
array[n][n] is the size of 1 element of the array, hence only the first element of the array would be initialized.
EvilTeach
Oops. Right you are. I meant to put a type signature in the parens, not a array lookup. Fixed it.
swestrup
+16  A: 

If array is truly an array, then you can "zero it out" with:

memset(array, 0, sizeof array);

But there are two points you should know:

  • this works only if array is really a "two-d array", i.e., was declared T array[M][N]; for some type T.
  • it works only in the scope where array was declared. If you pass it to a function, then the name array decays to a pointer, and sizeof will not give you the size of the array.

Let's do an experiment:

#include <stdio.h>

void f(int (*arr)[5])
{
    printf("f:    sizeof arr:       %zu\n", sizeof arr);
    printf("f:    sizeof arr[0]:    %zu\n", sizeof arr[0]);
    printf("f:    sizeof arr[0][0]: %zu\n", sizeof arr[0][0]);
}

int main(void)
{
    int arr[10][5];
    printf("main: sizeof arr:       %zu\n", sizeof arr);
    printf("main: sizeof arr[0]:    %zu\n", sizeof arr[0]);
    printf("main: sizeof arr[0][0]: %zu\n\n", sizeof arr[0][0]);
    f(arr);
    return 0;
}

On my machine, the above prints:

main: sizeof arr:       200
main: sizeof arr[0]:    20
main: sizeof arr[0][0]: 4

f:    sizeof arr:       8
f:    sizeof arr[0]:    20
f:    sizeof arr[0][0]: 4

Even though arr is an array, it decays to a pointer to its first element when passed to f(), and therefore the sizes printed in f() are "wrong". Also, in f() the size of arr[0] is the size of the array arr[0], which is an "array [5] of int". It is not the size of an int *, because the "decaying" only happens at the first level, and that is why we need to declare f() as taking a pointer to an array of the correct size.

So, as I said, what you were doing originally will work only if the two conditions above are satisfied. If not, you will need to do what others have said:

memset(array, 0, n*n*sizeof array[0][0]);

Finally, memset() and the for loop you posted are not equivalent in the strict sense. There could be (and have been) compilers where "all bits zero" does not equal zero for certain types, such as pointers and floating-point values. I doubt that you need to worry about that though.

Alok
+1 because sizeof array is sufficient if it actually is 2-d.
N 1.1
A: 

Use array initialization

Benoît
+1  A: 

If you initialize the array with malloc, use calloc instead; it will zero your array for free. (Same perf obviously as memset, just less code for you.)

quixoto
Is this faster than memset if I want to repeatedly zero my array?
Eddy
calloc will zero it once, at initialization time, and likely won't be any faster than calling malloc followed by memset. You're on your own after that, and can just use memset if you want to zero it out again. Unless your array is really enormous, perf isn't really a consideration here on any reasonable machine.
quixoto
A: 
mack369