[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 388
  • Last Modified:

static variables

Hello,

I was given a function that looks like this:

void Something()
{
    static float f1, *f2;
    f2 = new float[2];

    // ...

    delete [] f2;
}

I need to make this function usable by threads. So can I just do this:

void Something(float &f1)
{
    float f2[2];

    // ....
}

I would pass f1 from the caller so it is in effect static for the duration of my calls. I am uncertain about how that f2 variable is working right now.

Thanks
   
0
minnirok
Asked:
minnirok
  • 3
  • 3
1 Solution
 
e_tadeuCommented:
If f2 is created in the heap in the beggining of the function and deleted at the end, then there is no reason the f2 pointer should be static!
Your solution is completely right!
0
 
minnirokAuthor Commented:
Hi e_tadeu,

Does that static keyword have any effect on that variable though? That's what I was most concerned about,

Thanks
0
 
jkrCommented:
>>Does that static keyword have any effect on that variable though?

Yes, it makes it *unsafe* to be used with threads. If two threads enter the function at the same time, the ist will allocate memory and the second, too. Both pointers however are stored at the *same* memory location, which will lead to at least memory leaks:

------------------>8----------------------------

static declarator

When modifying a variable, the static keyword specifies that the variable has static duration (it is allocated when the program begins and deallocated when the program ends) and initializes it to 0 unless another value is specified.

------------------>8----------------------------

int max_so_far( int curr )
{
   static int biggest;    // Variable whose value is retained
                          //    between each function call
   if( curr > biggest )
      biggest = curr;

   return biggest;
}

If you want to be 'multithreaded', do *not* use static variables and avoid using globals without sync'ing access to them.
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
e_tadeuCommented:
Even if your apllication is single threaded, if you called Something() recursively, the static variables would be unsafe!

Example:

void Something()
{
    static float f1, *f2;
    f2 = new float[2];

    // ...
    Something(); // call recursively

    delete [] f2;
}


When you call it recursively, f2 will get a new chunk of 2 floats from the heap, and the pointer to this chunk will be written over the *old chunk*, and this old chunk of memory will be lost forever (memory leak). At the end of the recursive call, the new chunk will be deleted, but f2 will still be pointing to it. When it returns, the outer Something() will try to delete[] f2 again, but f2 is pointing to that new chunk that was already deleted.. the result: you will delete an already deleted block: a crash!
0
 
e_tadeuCommented:
You are not forbidden to use static variables in multithreaded applications. Just take a *lot of care* :)
You can safely use static consts.. and when the operation on statics are atomic and simple, for example, counting for debug purposes:

void func() {  
  static const char* safeName = "func()";
  static int count = 0;
  ++count;

  // note that if the thread is switched right here, you will get a little problem, but nothing too serious

  cout << "I've entered " << safeName << " " << count << " times!" << endl;
}
0
 
jkrCommented:
Sorry, have I been unclear here? You cannot use it like that without risking memory leaks or data corruption. All threads share the same pointer variable.
0
 
jkrCommented:
Erro, sorry, got confused - too much to do at the same time. If you get rid of 'static', everything is fine.
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now