Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You're not really making a 2D array, you're making an array of pointers that point to arrays of pixels. That means your first <code>calloc</code> call should allocate enough space for pointers, not for pixels:</p> <pre><code>pixel **pixelvalue = calloc(imginformation.width, sizeof(pixel *)); </code></pre> <p>You didn't tag your question with a language, but assuming it's C (based on your <code>typedef</code>, which wouldn't be necessary in C++), you shouldn't be casting the return values of <code>calloc()</code>. Doing so can hide bugs with <code>#include</code> that could come back to bite you down the road.</p> <p><strong>Edit:</strong></p> <p>You asked a couple of follow-up questions. The first has been answered pretty well by several other answers, I think, but I'll try to summarize. The way you're doing the allocation, you are first going to allocate an array of pointers - each of those pointers is going to point to one row of your array. The rows themselves then need to be allocated - space for each <code>pixel</code> object goes there, and the pointers to the rows are stored in the first array of pointers.</p> <p>Your second question, bout the return value from <code>calloc()</code> is pretty straightforward. If you cast the return value, you can hide implicit declaration bugs from yourself. Since the return type of <code>calloc</code> is <code>void *</code>, if you do something like:</p> <pre><code>my_ptr = calloc(1, 2); </code></pre> <p>Everything works nicely. Now imagine that you didn't include <code>stdlib.h</code>, and therefore had no prototype of <code>calloc()</code> in your translation unit. That would lead the compiler to assume the signature of <code>calloc()</code> to be <code>int calloc(int, int)</code>, which isn't true. The same line of code above would throw you a warning about assuming a default signature for that function. Using a typecast like you have in your code will mask that warning and you might never know you were missing that <code>#include</code> line.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload