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?

Wolfgang
  • 3,470
  • 1
  • 20
  • 36
Miyako
  • 9
  • 3
  • 3
    So what you are saying the code presented cannot be changed? Doesn't make sense. It will get incremented each time called. Period. – Eugene Sh. May 26 '16 at 17:36
  • 6
    If you want a dedicated counter for each object add that counter as a member of the class. – Jean-Baptiste Yunès May 26 '16 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. – John Bollinger May 26 '16 at 17:40
  • The only potentially viable approach I see is to perform your tests in a separate run of the program for each object. – John Bollinger May 26 '16 at 17:42
  • 2
    I'm voting to close this question as off-topic because the problem has no possible solution. – SergeyA May 26 '16 at 17:44
  • 1
    I am just wondering, what you are testing here... – Eugene Sh. May 26 '16 at 17:45
  • 1
    @SergeyA, there not being a solution to the *problem* does not mean that the *question* is off-topic or impossible to answer. Indeed, in that case "The problem cannot be solved" *is* an answer. – John Bollinger May 26 '16 at 17:45
  • @JohnBollinger, hmmm... You might be right. I will see if my vote is revocable. – SergeyA May 26 '16 at 17:46
  • What do you mean by "the 'count'-function is Device-Under-Test'? IMHO it is clear that a function is not a device. I've not heard this phrasing before. – 2785528 May 26 '16 at 17:53
  • "...will increase the counter value for all other objects ..." - Wrong! It will increment **that same** variable. And if it a DUT, you schould not change the behaiour of the tested code. – too honest for this site May 26 '16 at 18:02
  • Namespace is a C++ feature. You need to move your ifdef __cplusplus around the namespaces otherwise you will get a compilation error when you build with C. – cup May 26 '16 at 18:11
  • My suggestion: forget the function exists, never call it. Place a private variable in the class. (with no 'static' modifier) and add accessor functions to the class. Then call those accessor functions. – user3629249 May 28 '16 at 02:30

3 Answers3

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.

SergeyA
  • 61,605
  • 5
  • 78
  • 137
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.

John Bollinger
  • 160,171
  • 8
  • 81
  • 157
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.

Vadim Key
  • 1,242
  • 6
  • 15
  • 2
    The OP remarks that "the 'count'-function is Device-Under-Test and may not be changed." – John Bollinger May 26 '16 at 17:43
  • 1
    @JohnBollinger then there only one simple solution run each test as separate process. Or, make a shared library and load/unload it each time... – Vadim Key May 26 '16 at 17:50