Thread: I want to run a counter until overflow but counting stops at 43178

  1. #1
    Registered User
    Join Date
    Jan 2016
    Posts
    76

    I want to run a counter until overflow but counting stops at 43178

    Hello,

    I'm trying to emulate Arduino
    Code:
    millis()
    function which is used for functions timing, ... etc.

    I tried this function which is working until it hits 43178 and stops every time I run this function, I don't know why.

    I think it should counts and prints the values until it overflows, that's what I'm expecting, but it stops at this value every time.

    This is my code:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <stdint.h>
    #include <stdbool.h>
    
    
    // enumerations
    typedef enum {
        START, OVERFLOW
    }MILLIS_STATES;
    
    // variables
    static uint32_t millis_counter;
    
    
    
    void timer_run(void){
        if(START == 0){
            millis_counter++;
            printf("%u\n",millis_counter);
            timer_run();
        }
        else{printf("finished\n");}
    }
    
    
    int main(){
    
    
       timer_run();
    
    
        return 0;
    }

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    4,602
    Exactly what do you expect to overflow?

    Why would you expect it to run until it "overflows"?

    You're probably blowing away your stack with all the recursive calls.

  3. #3
    Registered User
    Join Date
    Jan 2016
    Posts
    76
    Yes, I think the recursive calls are causing the problem. A normal while loop works perfectly.

    Yes, the way I'm calling the function from itself is wrong I guess, I should call another problem.

  4. #4
    Registered User
    Join Date
    Jan 2016
    Posts
    76
    But the same method is known in C programming. Check this link:

    C - Recursion - Tutorialspoint

  5. #5
    Registered User
    Join Date
    May 2010
    Posts
    4,602
    Please answer the first two questions I posted.

    Exactly what do you expect to overflow?

    Why would you expect it to run until it "overflows"?

    You have infinite recursion so your stack overflows and the program crashes, so what is your main question?

  6. #6
    Registered User
    Join Date
    Dec 2017
    Posts
    1,127
    You have infinite recursion so your stack overflows
    Since it's tail recursion, as long as it's optimized only a single stack frame would be used.
    The best argument against democracy is a five minute conversation with the average voter. - Churchill

  7. #7
    Registered User
    Join Date
    May 2012
    Location
    Arizona, USA
    Posts
    778
    Tail call optimization is not guaranteed by C, so you are very likely running into a call stack limit as @jimblumberg pointed out.

  8. #8
    Registered User
    Join Date
    Dec 2017
    Posts
    1,127
    Obviously he's running into a stack limit.
    And although tail call optimization is not guaranteed by the standard, any decent compiler will do it.
    But it is not done if you don't request optimization, which he also probably isn't doing.
    The best argument against democracy is a five minute conversation with the average voter. - Churchill

  9. #9
    Registered User
    Join Date
    Jan 2016
    Posts
    76
    Quote Originally Posted by jimblumberg View Post
    so what is your main question?
    I'm searching now for software timers.

    I want a timer to run the background, I want to do some multitasking and learn a little bit of threading.

  10. #10
    Registered User
    Join Date
    Jan 2016
    Posts
    76
    Quote Originally Posted by john.c View Post
    Obviously he's running into a stack limit.
    And although tail call optimization is not guaranteed by the standard, any decent compiler will do it.
    It's ok, I'm not insisting on this method, II just tried it and thought why it crashes every time.

    My alternative solution is to run a normal timer; like:

    Code:
    uint32_t millis(void){
            millis_counter++;
        return millis_counter;
    }

    this counter is ok, but at this point I think there are tools in desktop programming to know if the timer is running by milliseconds. Am I right here ?

    I'm currently checking topics regarding timers in windows programming.

    But it is not done if you don't request optimization, which he also probably isn't doing.
    Yes, in codeblocks compiler settings, I noticed some optimization options.

  11. #11
    Registered User
    Join Date
    Jan 2016
    Posts
    76
    Quote Originally Posted by john.c View Post
    Since it's tail recursion, as long as it's optimized only a single stack frame would be used.
    OK, so unless I optimize the compiler, it would be like free machine. Basically a compiler like the GCC is doing anything freely without optimizations. The programmer has to do them.

    Are there any default optimizations that are running by default ? I think there should be some, and there are optimizations that has to set by the programmer.

  12. #12
    Registered User
    Join Date
    Dec 2017
    Posts
    1,127
    You turn on optimization with the -O flag. Try -O2
    (That's the capital letter O.)
    The best argument against democracy is a five minute conversation with the average voter. - Churchill

  13. #13
    Registered User
    Join Date
    Jan 2016
    Posts
    76
    I checked the optimizations in codeblocks, there are 11. Which one to use ?

    Also, went to the gcc website, this link, there are a lot of stuff here:

    Optimize Options (Using the GNU Compiler Collection (GCC))

    1. which ones are for C compiler ?
    2. how to use it in codeblocks environment ?

    But now mostly I would like to get back to my code and my search now is about: soft timers in windows, threading and linked list for Arduino environment.

  14. #14
    Registered User
    Join Date
    Jan 2016
    Posts
    76
    I found this link which talks about the goals I want to reach for God's will.

    Arduino Linked List Menu for LCD | Arduino | C++ Programming | Freelancer

    Yes, I want a code that manage the writes to LCD and multitask other components too.

  15. #15
    Registered User
    Join Date
    Dec 2017
    Posts
    1,127
    for God's will
    WTF?!
    The best argument against democracy is a five minute conversation with the average voter. - Churchill

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Client/server problem; server either stops receiving data or client stops sending
    By robot-ic in forum Networking/Device Communication
    Replies: 10
    Last Post: 02-16-2009, 11:45 AM
  2. Overflow in Stack Counter
    By mlsrar in forum C Programming
    Replies: 36
    Last Post: 10-02-2008, 11:58 AM
  3. Page File counter and Private Bytes Counter
    By George2 in forum Tech Board
    Replies: 0
    Last Post: 01-31-2008, 03:17 AM
  4. Counting Numbers in Array, not counting last number!
    By metaljester in forum C++ Programming
    Replies: 11
    Last Post: 10-18-2006, 11:25 AM
  5. counter isnt counting right
    By gunshipolitico in forum C++ Programming
    Replies: 8
    Last Post: 11-25-2005, 12:19 AM

Tags for this Thread