0

I have a C-function called "count" that looks like this:

void count(){ 
  static int c = 0;
  printf("Counter=%i", c);
  c++;
}

Futhermore I have a vector of Cpp-objects and each object calls the "count" function. Since the counter variable is static a call made by one object will increase the counter value for all other objects as well. What I actually want is a dedicated counter for each object given the restrication that the "count"-function is Device-Under-Test and may not be changed. I think this should be possible using namespaces... Any ideas?


My initial idea was to use namespaces ...

namespace c1 {
#ifdef __cplusplus
  extern "C" {
#endif
    #include "count.h"
#ifdef __cplusplus
  }
#endif
}

namespace c2 {
#ifdef __cplusplus
  extern "C" {
#endif
    #include "count.h"
#ifdef __cplusplus
  }
#endif
}

And call from within Cpp-Object like this ...

if (objNr == 1) c1::count();
else if (objNr == 2) c2::count();
...

It did not work for me. Any idea why?

12
  • 3
    So what you are saying the code presented cannot be changed? Doesn't make sense. It will get incremented each time called. Period. Commented May 26, 2016 at 17:36
  • 6
    If you want a dedicated counter for each object add that counter as a member of the class. Commented May 26, 2016 at 17:40
  • 3
    Moreover, variables declared inside a function have no linkage, so you cannot access them directly from anywhere outside their declaring function. This prevents, say, having your test code reset the variable between tests. Commented May 26, 2016 at 17:40
  • 2
    I'm voting to close this question as off-topic because the problem has no possible solution. Commented May 26, 2016 at 17:44
  • 1
    I am just wondering, what you are testing here... Commented May 26, 2016 at 17:45

3 Answers 3

1

The problem as asked can not be solved. If the function is unmodifiable, there is nothing which can be done to start counting individual instances.

Sign up to request clarification or add additional context in comments.

Comments

1

Variables with static storage class, such as the one in your example, are global in the sense that there is only one copy anywhere in the program. This is independent of of their linkage, which determines from where they can be referenced. Regardless of their storage class, functions' local variables have no linkage, meaning that they can be directly accessed only from within the function body.

If you cannot modify the function, then there is no way for it to make variable c accessible elsewhere (such as by exposing a pointer to it), so there is no alternative for the test routines to, say, reset its value between tests or to read it out. Therefore, if different test objects must have their own copies of that particular variable, then it follows that they must have their own copy of the function that contains it.

The easiest and most general way to achieve that is to run each test object in a separate program. It might also be possible to play games such as dynamically loading and unloading a library containing that function (per @VadimKey), but that depends on features outside those of standard C or C++, and it makes the test environment rather different from most of the other environments the function is likely to see.

Otherwise, if multiple objects must run tests within the same run of the same test program, then there is no way to make them have private copies of functions' static variables. Your best bet might simply be to structure your tests to accommodate that.

Comments

0

If you can access to source code change it in some way to make this counter external. Either pass it as parameter, either make a class with counter as a member.

If you can't change the source code with this function then you may create a wrapper class with a separate counter.

2 Comments

The OP remarks that "the 'count'-function is Device-Under-Test and may not be changed."
@JohnBollinger then there only one simple solution run each test as separate process. Or, make a shared library and load/unload it each time...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.