views:

77

answers:

3

Is there a way to reset variables declared as static within a function? The goal is to make sure that the function is not called with lingering values from an unrelated call. For example, I have a function opearting on columns of a matrix.

int foo(matrix *A, int colnum, int rownum){
static int whichColumn;
static int *v; //vector of length A->nrows 
   if (column != whichColumn){
    memset(v,0,size);
    whichColumn = which;
   } 
   //do other things
}

The function is called n times, once for each column. Is this a proper way of "re-setting" the static variable? Are there other general fool-proof ways of resetting static variables? For example, I want to make sure that if the call is made with a new matrix with possibly different dimensions then the vector v is resized and zeroed etc. It seems the easiest way may be to call the function with a NULL pointer:

int foo(matrix *A, int colnum, int rownum){
static int whichColumn;
static int *v; //vector of length A->nrows 
   if (A == NULL){
    FREE(v);
    whichColumn = 0;
   } 
   //do other things
}

Any suggestions will be gretly appreciated. Thanks ~SM

+1  A: 

Use an idempotent initializer function and global variables instead.

For example:

int foo;
int *m = NULL;

static void InitVars() {
    foo = 0;
    if (m != NULL) {
        free(m);
    }
    m = malloc(sizeof(int)*5);
    memset(m, 0, sizeof(int)*5);
}

If your initializer is really idempotent, you can call it again to reset the variables.

If you need this to be called automagically, use __attribute__((constructor)) (for GCC) like so:

static void InitVars __attribute__((constructor)) ();

However, you should note that if you need to do this, you should reconsider using in-function static variables and instead use passed-in fresh ones that are returned/written and passed to subsequent related calls.

Borealid
Hi Thanks for the answer. I decided to follow the advise and not use *v as a static variable- instead just use whichColumn as a statitc variable. v is now part of the struct as recommended by Jesse below. passing (column = -1) resets the statitic variable and reinitalizes the work vector. Then it is updated appropriately for each time (column != whichColumn). Your other suggestion sounds useful and I will learn about it. Thanks again. sm
A: 

I'd recommend turning it into a struct and writing a small helper function for managing the semantics of what you're trying to do. It could return the buffer if the request is appropriate for its size, or create a new one on demand (and free the old one) if necessary.

Jesse Collins
Write a wrapper class? In C?
Beta
Whoops, how about a struct + helper function?
Jesse Collins
Thanks Jesse, I used a hybrid of the teo approaches as my comment to Borealid indicates. "Reset" is still managed by the function to hide details outside. I do not know C++, so wrapper class is ootq.
A: 

One approach I've seen used when a C module was imported to C++ was to surround the whole module with a class wrapper, and replace all static variables inside functions with uniquely-named "global" varaibles outside the functions. I don't know any good way to achieve a similar effect for projects involving multiple source files, though I'd like to know if one exists. I have some embedded system code in C, which I simulate by adding some C++ wrappers in VS2005. For example, I have I/O registers defined so that something like TX1CON = 0x5C; would translate into something like IOMAP(0x251).P = 0x5C; IOMAP is a property which would send "write 0x5C to address 0x251" to a hardware-simulation program. This approach works well, but I can't do a clean reset. Any ideas?

supercat