Thread: variable assuming arbitrary large values

  1. #1
    Registered User
    Join Date
    Apr 2011
    Posts
    74

    variable assuming arbitrary large values

    Hi,

    I have a variable of type unsigned long in my program.

    unsigned long l_load;

    The program runs in a loop and assigns it the values, that keep on incrementing. Let us say, a program run that assigns certain values in sequence.

    For example,

    0, 56, 112, 168, 224, 240

    These are all, expected numbers, and can be larger than that but within range of the variable type that is unsigned long.

    But, at certain runs, the program takes the following arbitrary values for the same variable. i.e.

    2147680256

    I was not able to figure out what can be the source of such an error,

    Any ideas, what precautions I might be missing ? or under which scenario can these problems happen ?

    Thanks,

  2. #2
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    Can you post relevant part of code.

  3. #3
    Registered User
    Join Date
    Apr 2011
    Posts
    74
    Hi,

    Please find below the part of the code.



    Code:
    // a structure used in the code
    
    typedef struct 
    {
    	unsigned long up;
    	unsigned long down;
    }LOAD;
    
    
    static void update_load(
    		LOAD *cpl,
    		void *mesg,
    		unsigned short pkt
    		)
    {
    
        unsigned char c;
    
        for (c=0; c<3; c++)
        {
            update_first(&(cpl[c]), &(cpl[0]), m_size_us);
        }
        update_second(&(cpl[0]), m_size_us);
    }
    
    
    // Where the function update_load is
    
    static void update_first(LOAD *load_p, LOAD *p_link, unsigned long m_size_us)
    {
        unsigned long start;
        start = p_link_p->up;
    
        load_p->down += m_size_us;
    }
    
    static void update_second(LOAD *load_p, unsigned long m_size_us)
    {
        load_p->up += m_size_us;
    }

    I have pasted some code, Will look to the inputs.

    Thanks,

  4. #4
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    I am not seeing any problem.
    It's possible that you've trashed memory. or fail to initialize variables?
    Is LOAD* dynamically allocated? does it have 3 elements...?
    and use debugger?
    Code:
    static void update_first(LOAD *load_p, LOAD *p_link, unsigned long m_size_us)
    {
        unsigned long start;
        start = p_link_p->up;     // where is p_link_p ?
    
        load_p->down += m_size_us;
    }

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Not that it matters to your issue: you pass p_link in to your update_first but don't use it. Your compiler is probably giving you warnings too, but only you know whether that's the right thing.

    As to the issue at hand, it all comes down to what value m_size_us is, and who knows? We don't.

  6. #6
    Registered User
    Join Date
    Apr 2011
    Posts
    74
    Hi,

    thanks for the reply, it is mistake, It is actually

    Code:
     start = p_link -> up
    yes, I have checked the variables are initialized. load_p and p_link is passed by reference, so we have the updated values after the function returns.

    I guess, I have to debug more, and come back again ...

  7. #7
    Registered User
    Join Date
    Apr 2011
    Posts
    74
    Hi,

    Thanks every one for the replies.

    I have now figured out the problem.

    I missed initializing one of the variables. In essence, I was going though the "for" loop one time short. And, it was because of a "for" loop limit variable that was pre-configured, and should have been changed to account for more variables in the program.

    Thanks,

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Displaying a table of arbitrary values?
    By cbmurdock in forum C Programming
    Replies: 1
    Last Post: 02-29-2008, 12:59 AM
  2. Calculating min and max values of an arbitrary mesh.
    By psychopath in forum Game Programming
    Replies: 12
    Last Post: 04-21-2006, 11:33 AM
  3. #define not working for large values
    By jjphysicist in forum C Programming
    Replies: 4
    Last Post: 03-21-2006, 03:02 PM
  4. Computing Large Values
    By swbluto in forum C++ Programming
    Replies: 8
    Last Post: 04-07-2005, 03:04 AM
  5. Adding Large float values--need help
    By vaspro in forum C Programming
    Replies: 10
    Last Post: 03-24-2002, 04:16 AM