0

At each iteration of a loop I wish to zero all elements in a dynamically defined multidimensional array.

void my_function(int window_size, int row_size){

        double **window_arr;
        window_arr = (double **)calloc((window_size * 2), sizeof(double*));
            for (i = 0; i < (window_size * 2); ++i){
                window_arr[i] = (double*)calloc(3, sizeof(double));
            }
        for (i = 0; i < row_size; ++i){
           ...
           memset(window_arr, 0, sizeof(window_arr) * (window_size * 2) * 3);
        }
    }

This seg faults. Setting a break point before the first memset, but after allocation, looks good.

(gdb) p window_arr[1]
$1 = (double *) 0x22604f50
(gdb) p window_arr[1][0]
$2 = 0
(gdb) q

The break point after the memset

(gdb) p snp_window_arr[1]
$1 = (double *) 0x0
(gdb) p window_arr[1][0]
Cannot access memory at address 0x0
(gdb) q

I have figured out how to use memset for 1d arrays; I would really like to learn how to use memset in the above-described scenario.

6
  • Could you call memset immediately after the second calloc (inside the loop)? Commented Oct 30, 2014 at 1:17
  • Also, I can't really make sense of your use of memset. Shouldn't its third arg be something like: sizeof(double) * (window_size * 2) * 3 Commented Oct 30, 2014 at 1:19
  • 2
    You're also assuming 2d arrays are contiguous, which I'm not sure is guaranteed (it may be). If not, my first suggestion is safest -- memset each row separately. Commented Oct 30, 2014 at 1:22
  • It is not portable to zero floating point variables by zeroing memory (with either calloc or memset). You should loop through the entries and set them individually to 0.0. (But why would you use memset after using calloc? calloc already zeroes the memory it returns.) Commented Oct 30, 2014 at 1:25
  • @ooga, because each iteration of the loop uses the array for some calculation, which then needs to then be zeroed. Commented Oct 30, 2014 at 1:35

1 Answer 1

1

You're building an array of pointers to arrays, rather than a single 2D array. That means you have an array of pointers, and each pointer points to a 1D array. Then you try to memset() the entire 2D space, but this is impossible because it is not contiguously allocated.

You should consider allocating a single array with space for all your elements at once, since your logical 2D array is rectangular anyway. Just do this:

double *window_arr = calloc((window_size * 2) * 3, sizeof(double));

Then:

memset(window_arr, 0, (window_size * 2) * 3);

Of course you will then index into this 2D array as window_arr[x*window_size*2 + y] or similar, rather than window_arr[x][y].

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

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.