1

Say, you wanted to know how many times a program has been executed. Is it possible to have a variable, then when the program has been accessed increment the variable by 1 etc. and then store it back as that original variable so that the next time it is executed, it can be incremented again.

E.g. (This is not correct or real code)

/*variable to be stored*/
int num;

/*initial value*/
num = 0;
.
/*some stuff I'll do*/


/*so that num becomes incremented*/
/*please don't rage that I have done it this way*/
num = num + 1;

...and then somehow store it back into int num.

Please help me, suggestions welcome.

ASP
  • 3,645
  • 1
  • 31
  • 45
bananafish
  • 15
  • 1
  • 4
  • 1
    You'd need to store this value in a file or a database or something if it has to persist between multiple runs of the program. – shree.pat18 Apr 06 '15 at 11:06

1 Answers1

0

In your question, the term program is a little ambiguous. For the sake of clarity, let me divide the question is two seperate parts.

If your question is regarding the possibility for below cases,

how many times a function has been executed?

Yes, use a static variable as a counter.

how many times a binary has been executed?

Yes again, but cannot be achieved using without a file i/o. You need to use file i/o to write the value to a file once a binary has been executed. For each execution of the binary, read the value from the file, update and write back to it.

Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
  • How many times a binary has been executed is what I mean sorry – bananafish Apr 06 '15 at 11:31
  • Ok, I understand what you mean, but then what happens when the computer boots for the first time? Because, normally devices only usually present the setup menu(s) or utilities when your computer has booted up for the first time. So, how would this be done? – bananafish Apr 07 '15 at 07:41
  • @bananafish I did not understand you fully. Are you asking for non-existance file case? You need to check the presence of the file, if not present, create and start using it. That it part of basic sanity checking I guess. is this what you want to know? – Sourav Ghosh Apr 07 '15 at 07:50
  • Yea something like that. – bananafish Apr 07 '15 at 10:52
  • What I wanted to ask was that since operating systems have a way to log how many times it has been booted (I think, probably does) , is there a way to implement that sort of mechanism and how would it work? – bananafish Apr 07 '15 at 10:54
  • @bananafish yeah. if there is such mechansim, it's also file IO, IMHO. the file mentioned here shall reside at non-volatile memory. then, on every bootup, OS can modify the file entry. Concept is more or less simmilar. :-) – Sourav Ghosh Apr 07 '15 at 10:58