bobbay
bobbay

Reputation: 493

Should I declare a variable used in many functions as a member variable?

I have many overloaded functions in a class. In this case, should I declare the int32_t data as a member variable of the class, so I am not declaring it over and over in each function? The Fill function is always setting a value through reference, so I don't think I should need to declare it every time in every function.

There is about 20 more of these functions not listed here:

void TransmitInfo(TypeA &dp, Id &tc)
{
    //do lots of other work here
    int32_t data;
    while (dp.Fill(data))  //Fill accepts a reference variable, "data" gets populated
    {
        Transmit(tc, data);  
    }
}

void TransmitInfo(TypeB &dp, Id &tc)
{
    //do lots of other work here
    int32_t data;
    while (dp.Fill(data))
    {
        Transmit(tc, data);  
    }
}

void TransmitInfo(TypeC &dp, Id &tc)
{
    //do lots of other work here
    int32_t data;
    while (dp.Fill(data))
    {
        Transmit(tc, data);  
    }
}

Upvotes: 3

Views: 78

Answers (3)

Scope is not the only thing to consider when choosing where to declare a variable. Just as important are the lifetime of the variable and when it is created.

When you declare a variable inside a function, it is created whenever that function is called, several times if need be (recursion!). And it's destroyed when that function exits. These creations/destructions are noops for the CPU in the case of simple types as int32_t.

When you declare it inside the class, you get only one copy of the variable per object you create. If one of your function calls another (or itself), they will both use the same variable. You also increase the size of your objects; your variable will consume memory even when it's not used.

So, the bottom line is: Use the different kinds of variables for the purposes they were designed for.

  • If a function needs to remember something while it runs, it's a function variable.
  • If a function needs to remember something across its invocations, it's a static function variable.
  • If an object needs to remember something across its member invocations, it's a member variable.
  • If a class needs to remember something across all objects, it's a static class variable.

Anything else leads to chaos.

Upvotes: 5

Lilith Daemon
Lilith Daemon

Reputation: 1473

You should refrain from using a member variable for any type of temporary data. The reason for this is that it guarantees that your code is not thread safe, and in this day-and-age of parallel computing, that is a major disadvantage. The cost of allocating an int32_t is extremely small as to be negligible so thus it is often better to allocate inside the function to maintain thread safety. Before a single int allocation becomes noticeable you will have to allocate it well over a million times, and even then the total loss will be in microseconds.

If your experiencing such difficulty with optimization that you have to resort to such a high degree of micro-optimization then you likely should try and rework your algorithm to create a better scaling as opposed to spending massive amounts of time optimizing something that is not a choke point. (You would also be better off using a good concurrent algorithm, as opposed to shaving picoseconds off of a serial algorithm.)

Upvotes: 2

Bathsheba
Bathsheba

Reputation: 234785

Absolutely do not do this. If it's only a temporary for the life of a function then keep it local.

Else you'll cause more problems than you solve; e.g. Multithreading and serialisation.

Leave such micro-optimisations to the compiler.

Upvotes: 2

Related Questions