• Advertisement
Sign in to follow this  

(bad practice ?) Pointers for having better variable names

This topic is 549 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

In my C game engine, i have an array of structs representing all the active game objects. Those structs contains classic data (type of object, x/y pos...), a pointer to the function handling the object's behavior and a bunch of 'generic' variables (ints, floats...) to be used by this function if needed.

 

Depending on the object, those variables have different uses so they are named generically varA, varB, varC....

 

What do you think about creating pointers for them at the beginning of the object's function, just for the sake of having good readable names ?

void function(myobject *o){

int* iaState = &o->varA;
int* otherSuperVariable = &o->varB;

// do stuff with them

}

Since the pointer will never change after its creation, do you think the compiler automatically optimize it or it'll add useless processing each time i use it ?

Edited by xxFuttBucker444

Share this post


Link to post
Share on other sites
Advertisement

In release mode, it's highly likely that there will be no extra variable created and no runtime overhead at all.

 

However, I doubt that this set of 'generic' variables can't be replaced with something better. At the very minimum, can't you use a union? Or just have the top level object contain different structs for different object types?

Share this post


Link to post
Share on other sites

I practice C since a quite long time, and to be honest, i didn't know about the union existence ! Thank you for this tip, it seems a great way to avoid wasting space because of inappropriately-typed variables.

I think your 2nd solution, with different structs for each object type, is the total opposite and would waste so much space it would be dangerous. Or i didnt understand what you mean?

 

I could also have only a void pointer per object, a bunch of typedef structs defining the variables needed for every object type, and do a malloc on the object creation. I don't know what is better

Edited by xxFuttBucker444

Share this post


Link to post
Share on other sites

The idea would be that the shared data is in a 'base' struct, and that struct would be contained in one of many 'child' structs, one for each type of object. It would be similar to C++ classes but you would cast the pointer based on which type of object it is. There would be no wasted space as each object would contain only the exact fields it needs (compared to your current system which wastes space if a certain type of object doesn't need some of the generic variables.) This would only work if you were allocating each one separately; you couldn't store them all in an array, that way.

Share this post


Link to post
Share on other sites
Kylotan's suggestion of using unions is the way to go. You can see an example of how to use them for the sort of thing you are wanting to do by looking at SDL's event handling - specifically SDL_events.h. Look at the SDL_Event union typedef at line 525.

Share this post


Link to post
Share on other sites

Thanks, i'll use union to save memory

 

For the variable names, should i declare them all in the unions (so I have to decide here which can overlap, all the variables names for all objects will be declared into unions... ) like this:

union{
int playerLifes;
int platformSpeed;
float something;
float otherthing;
};

union{
int levelId;
float gravity;
};

union{
 ...

, or should i keep the union generic like :

union {
int i;
float f;
} data[10]; // each object can have a maximum of 10 variables of mixed types

and go with the pointers to get more readable names?

 

I guess the whole purpose of the thing is to use the 1st method, but I feel weird putting the mixed stuff of all objects here.

Edited by xxFuttBucker444

Share this post


Link to post
Share on other sites

Thanks, i'll use union to save memory

For the variable names, should i declare them all in the unions (so I have to decide here which can overlap, all the variables names for all objects will be declared into unions... )

You should have a union of structures not a union of unions. The latter will put all the variables in the sample space.

Share this post


Link to post
Share on other sites

I decided to use this way of storing the objects after analyzing the disassembly of Sonic on Genesis, which is quite an achievement for its era. All objects take 64 bytes in ram, they have common attributes and a free area that every object can use to store its specific data.

 

I'm doing a similar thing : most of the attributes in Object struct are somewhat generic (mostly graphical related) and could be needed by everything : alive, type, subtype, x/y speed, angle, animationId, framecount, currentframe... + those additional "free usage" values to be used the way the object wants. The player releated variables are handled outside the object struct since it's very specific.

 

If i use separate arrays for every object type, i may end with heavily duplicated code. Should i really have 40 structs if i have 40 objects types, each having 80% of common attributes?

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement