Fastest way to zero out a 2d array in C?

前端 未结 12 1277
后悔当初
后悔当初 2021-01-29 18:58

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

// Array of size n * m, where n may not equal m
for(j = 0; j < n; j++)
{
            


        
12条回答
  •  野的像风
    2021-01-29 19:00

    Use calloc instead of malloc . calloc will initiate all fields to 0.

    int *a = (int *)calloc(n,size of(int)) ;

    //all cells of a have been initialized to 0

提交回复
热议问题