kohi icon indicating copy to clipboard operation
kohi copied to clipboard

Forward-declare platform_state structure instead of using void pointers.

Open travisvroman opened this issue 1 year ago • 1 comments

Not really a bug, but a good point brought up by keldwikchaldain9545 on YouTube. Tracking this here so it doesn't get lost.

Quote from the comment post:

I know I'm years too late, but the platform_state could be defined in a way that I think would be a little better.
If you declare a struct with a typedef, you don't have to give it fields, you can leave it undefined, and you can then handle instances of that structure as long as you only do so behind a pointer. So  you could do the following:
typedef struct platform_state_t *PlatformState;
And then from all your external code you handle a PlatformState which is a pointer to the platform state, and then inside your implementation you take that pointer and it's already the correct type. No need to do implicit casts with void *.

travisvroman avatar Sep 26 '23 17:09 travisvroman

It's better to avoid typedefing pointers. Otherwise one may experience a lot of headaches with const qualifier. For example in:

typedef struct platform_state_t *PlatformState;
const PlatformState platform_state;

The type of platform_state is struct platform_state * const, not const struct platform_state *. The pointer itself is const, not the object it points two.

tstanisl avatar Nov 10 '23 15:11 tstanisl

This was previously fixed in the 0.7.0 refactor. Closing this.

travisvroman avatar Jul 21 '24 00:07 travisvroman