Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Your question is probably best answered by an explanation of what <code>void</code> means.</p> <p><code>void</code> basically denotes the absence of a type. As a consequence of this, you cannot tell the compiler to create an object of type <code>void</code>, e.g. with the following statements:</p> <ul> <li><code>new void</code></li> <li><code>new void[...]</code></li> </ul> <p>The meaning of these expressions is roughly the following: "create something", or "create an array of somethings" respectively. This is clearly not a precise enough statement for the compiler.</p> <p><code>void*</code> (pointer to <code>void</code>) however is permitted because pointers are fundamentally always the same thing to the compiler: a memory address of another object. You can think of a pointer as some kind of arrow pointing to something. If you're working with a pointer, the compiler sees that arrow, and not the actual thing it points at. Therefore the compiler doesn't care that the "target type" is <code>void</code>.</p> <p>The consequence of this, however, is that you cannot dereference a <code>void*</code> (pointer to <code>void</code>), because then you make the compiler effectively look at the pointed-to thing, which would be a <code>void</code> value, which doesn't make any sense to the compiler.</p> <p><strong>Summary</strong>:</p> <p><strong>1)</strong> You <em>cannot</em> create an array of type <code>void</code>, as in <code>new void[...]</code>.</p> <p><strong>2)</strong> You <em>can</em> create a pointer to <code>void</code> (<code>void*</code>), or even a pointer to a pointer to <code>void*</code> (<code>void**</code>). </p> <p><strong>3)</strong> You <em>cannot</em> dereference a <code>void*</code> pointer (but you <em>can</em> dereference a <code>void**</code> pointer).</p> <p><strong>Conclusions</strong>:</p> <p><strong>4)</strong> You <em>can</em> create an <code>int*</code> array and let a <code>void*</code> refer to it:</p> <pre><code>int** m; // ... (create the dangling array as in the OP's code and let m point to it) void* v = (void*)m; </code></pre> <p>(See the comments below on why a <code>void*</code> is used here instead of <code>void**</code>!)</p> <p><strong>5)</strong> Because of statement #3, all you can reasonably do with such a pointer is pass it around, but you <em>cannot</em> work on the array's actual content. In order to do this, you need to type-cast it back to the correct data type:</p> <pre><code>int **m2 = (int**)v; </code></pre>
    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. 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