Note that there are some explanatory texts on larger screens.

plurals
  1. POPointers to void pointers in C - can I use void** for rudimentary polymorphism?
    text
    copied!<p>I can understand how a <code>void**</code> might look in memory, but I'm wondering if I'm using it quite right. Are there any fundamental flaws in what I describe below? For example, although I can say "it works for me", am I creating bad / unportable code in some way?</p> <p>So I have an Asteroids clone. There are three entities that can fire bullets, the players (<code>SHIP *player_1</code>, <code>SHIP *player_2</code>) and the UFO (<code>UFO *ufo</code>). When a bullet is fired, it's important to know who fired the bullet; if it was a player, when it hits something their score needs to be incremented. So, the bullet will store what kind of entity it belongs to (<code>owner_type</code>) and also a pointer directly to the owner (<code>owner</code>):</p> <pre><code>enum ShipType { SHIP_PLAYER, SHIP_UFO }; typedef struct Bullet { // ...other properties enum ShipType owner_type; void **owner; } BULLET; </code></pre> <p>Then, when the player hits the button or the UFO sees a target, one of these functions will be called:</p> <pre><code>void ship_fire(SHIP **shipp) { BULLET *bullet = calloc(1, sizeof(BULLET)); bullet-&gt;owner_type = SHIP_PLAYER; bullet-&gt;owner = (void**)shipp; // do other things } void ufo_fire(UFO **ufop) { BULLET *bullet = calloc(1, sizeof(BULLET)); bullet-&gt;owner_type = SHIP_UFO; bullet-&gt;owner = (void**)ufop; // do other things } </code></pre> <p>... they may be called, for example, like this:</p> <pre><code>ship_fire(&amp;player_1); </code></pre> <p>Finally, when the bullet hits a target (such as an asteroid), we dereference the owner. If it's a ship, we can increment the score there and then.</p> <pre><code>void hit_asteroid(ASTEROID *ast, BULLET *bullet) { SHIP *ship_owner; if (bullet-&gt;owner_type == SHIP_PLAYER &amp;&amp; *bullet-&gt;owner != NULL) { ship_owner = (SHIP*)*bullet-&gt;owner; ship_owner-&gt;score += 1000; } } </code></pre> <p>Does that seem a reasonable approach? Like I say, it works for me, but I only have a couple of months of C experience.</p> <p>A final note: why do I not use a <code>void*</code> instead of a <code>void**</code>? Because I want to avoid dangling pointers. In other words, say that <code>player_1</code> dies and is free'd, but their bullet keeps going and hits an asteroid. If I only have a <code>void*</code>, the <code>hit_asteroid</code> function has no way of knowing that <code>bullet-&gt;owner</code> points to de-allocated memory. But with a <code>void**</code>, I can validly check to see if it's NULL; if <code>player_1</code> is NULL, then <code>*bullet-&gt;owner</code> will be NULL too.</p> <p><strong>EDIT:</strong> All respondents so far concur that using a void** probably isn't necessary here because I can avoid the dangling pointers issue (by just statically allocating the base object, for instance). They're correct and I will refactor. But I'm still kinda interested to know if I've used void** in a way that might break something e.g. in terms of memory allocation / casting. But I guess if no-one has thrown their hands in the air and declared it faulty, it at least resembles something that would technically work.</p> <p>Thanks!</p>
 

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