Archived

This topic is now archived and is closed to further replies.

Pointer shortcuts

This topic is 5014 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

I was wondering if using a "short worded" pointer, point to a "huge worded" variable slower than just using the variable for example. Also, would that be considered bad programming practices? I would hate to write this over and over again and getting syntax errors factory[0]->model.make.type=5; this is much easier, but is it faster? ANYOBJECT *point = &factory[0]->model.make.type; point = 5;

Share this post


Link to post
Share on other sites
A good optimizing compiler will probably turn both versions into the same machine code. Do whichever one you think will make your code easier to understand.

Share this post


Link to post
Share on other sites
What you want to do is sort of possible, but you may run into issues with type declarations. For example:
struct Binkey {
int type;
int color;
float radius;
enum FlavorT flavor;
};

struct Binkey * mybinkey = new struct Binkey;
int * shortcut = &(myBinkey->type);
*shortcut = 1;

That would work yes, but only on type and color. Because shortcut is a integer pointer, it wont work on radius, and its up in the air if it''d work with the FlavorT enumeration.

Share this post


Link to post
Share on other sites
If this is C++, I sometimes use references when my lines are way too long for similar issues as this.
void SomeFunction(CSomeClass Instance)
{
float& rX = Instance.pMember->Variables.pLocation->x;
//Which is basically identical to

float* pX = &(Instance.pMember->Variables.pLocation->x);
//But the reference looks nicer, since you

//don''t have to dereference it all the time.

}
Now I''m not an expert on this stuff. I don''t know how much lines like Instance.pMember->Variables.pLocation->x slow down the program, but I am pretty sure that using a single reference or pointer shouldn''t hurt your performance, and it might make it better. Either way, it makes your code look nicer, and thus increases readability and reduces the chance of making stupid bugs or syntax errors. So I''d fully recommend it.


int Agony() { return *((int*)0); }    Mwahaha... >8)

Share this post


Link to post
Share on other sites