Advertisement

How to add another name to a variable?

Started by January 09, 2016 01:39 AM
8 comments, last by Infinisearch 8 years, 11 months ago

So I'm RE some old Network NDIS driver and I get tired of dealing what variable is doing what. As I'm translating the code directly from IDA Pro I need to name my variables simple so I can easily compare both outputs. For example I name all my variables as this : 'v ?' as '?' is some number, the functions arguments like this : 'a ?' and the structure members like this: 'm_?' as in this case the number is byte offset starting from the structure first location.

However I would like to have another name, alias to them which will have a meaning in what are they actually doing. If I simply add this to the current name this could break my existing code - I would need to rename all the instances of it.

What I want is something like this:


struct SomeData {
    int m_0; //here I want to refer to this member also as like for example 'StructureSize'
    int m_4; 
} d;

And so I can both write "d.m_0" and "d.StructureSize" which to refer to the same exact location. Any ideas?

union?
Advertisement

However I would like to have another name, alias to them which will have a meaning in what are they actually doing. If I simply add this to the current name this could break my existing code - I would need to rename all the instances of it.

Most modern IDEs allows you to right click on the variable name and rename all places where that specific variable is used (without accidentally changing similar names used by different variables).

If I'm understanding your correctly, typedef / using would work for you. If you want to rename the actual variable then just do what Servant said above. The only way I can think of that would allow you to rename existing variables is to use preprocessor direcive #define's.


#define variableWhoseNameILove variableWhoseNameIHate

int variableWhoseNameIHate = 0;

variableWhoseNameILove++; //this corresponds to variableWhoseNameIHate and is incremented by 1

Ugly, but it works.

If I'm understanding your correctly, typedef / using would work for you. If you want to rename the actual variable then just do what Servant said above. The only way I can think of that would allow you to rename existing variables is to use preprocessor direcive #define's.

But don't try this at home, unless you want to confuse yourself beyond what you consider possible.

#define has so many gotchas that it's much better to stay away from them completely until you understand what you're doing, and even then, only touch them with a very long pole when there is really no other way.

Edit:

The above was inspired mostly by posting in "Beginners", where imho you shouldn't discuss trickery with #define here.

Upon further thought:

The idea proposed by Nypyren seems to work, at least this compiles for me


struct Y {  // Better names struct
    int size;
    int m4;
};

union {
    struct { // Original struct
        int m0;
        int m4;
    };
    struct Y mine;
} d;

int main()
{
    d.m0 = 3;
    printf("%d\n", d.mine.size);
}

Not sure this is very maintainable, if the original struct and the Y struct go out of sync you're in deep trouble. To avoid, you can generate the union code.

In the end, the suggestion by the Servant may be the simplest of all. Rename variables as you go. Note that renaming has the nice property that the compiler dies if you mess up, so it's fairly easy to keep things in sync.

Thanks for the answers. I think the "Alberth" solution worked for me. Although I think I'll try encapsulating it in macros for ease.

Advertisement

Just out of curiosity, why the down-vote? That is a solution to the question he asked as it was phrased. I stated it was ugly and hacky, but it works.

It's often not so much the solution in itself, but also how you bring it. On the other hand, some people just downvote things they don't like

FYI I did not downvote you, although I did consider it.

Just out of curiosity, why the down-vote? That is a solution to the question he asked as it was phrased. I stated it was ugly and hacky, but it works.

Usually, the person downvoting isn't actively participating in the conversation but still wanted to register their disagreement.

The people downvoting and the people responding are typically different people, unless you say something terribly wrong (in which case, you'll rapidly accumulate three or four downvotes).

If I was to guess why the person downvoted, it's probably because it's the For Beginner forum, and you handed the beginner a tool that if they went with ("because it works"), would permit them to rapidly and frequently shoot themselves in the foot. The disclaimer "it's ugly" doesn't help much.

If someone says their hand is hurting, and how can they ignore the pain, and you reply, "It's ugly and hacky, but if you take this object called a 'gun' and point it at your foot and pull the trigger, you'll instantly be distracted the pain in your hand.", twenty people would downvote you. This is a milder version of the same idea, so only one person downvoted you.

I had already considered mentioning #defines, and also references ghosting the variables, but both of those situations hide the problem instead of fixing it, which will add to the OP's increasing code complexity and cause him more trouble down the way, and using macroes casually would very quickly lead to even more problems. So, I dismissed the idea of posting about them, because a ""solution"" that ""works"" does not automatically mean a solution I should share with a beginner. Instead I posted the correct ""solution"": Fix the problem, don't hide it.

But again, the person who downvoted you wasn't anyone who posted in this thread.

Out of curiosity could you accomplish this goal with references? They provide an alias for a variable don't they?

-potential energy is easily made kinetic-

This topic is closed to new replies.

Advertisement