Thread: variable setting (very basic very strange)

  1. #1
    Registered User
    Join Date
    Feb 2006
    Posts
    1

    variable setting (very basic very strange)

    Hi all ;

    I have two lines of code as the following :

    last_recived = (mh->seq)+1 ;
    fprintf(stderr,"me %d ,mh->seq= %d \n ",last_recived,(mh->seq)+1 ) ;

    both variables (last_recived and (mh->seq)+1) are Int . when this is being printed the values of last_recived and the (mh->seq)+1 are different!!! I am sure that this is the only where in my program that some thing is being printed out ! how is that possible? These two lines are exactly after each other! I am thinking about some sort of compiler bug or so . if I put

    last_recived = 1 ;

    before printing then 1 is being printed for last_recived ; more surprisingly if I put

    last_recived ++ ;

    before printing some values like 2789 are being printed which is almost 500 less that the (mh->seq)+1

    and finally if some where in my program (before these two lines) I put last_recived++ ; the the printed value for last_recived will be one less than (mh->seq)+1 !!!!!!!

    I am using an old machine with Red hat Linux installed on that. I’m sure both variables are int. is there any possibility for computer resources shortage such as memory or ...?

    Many thank & Regards

    Rouzbeh

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    I'd recommend posting the exact code that causes the error: too often paraphrased code acquires unrelated problems. But what you've shown sounds considerably like this.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Without you posting a whole program which shows the problem, who knows what's going on.
    Most likely, you trashed something a long time ago and now you're just noticing the problems.

    Maybe mh is a pointer to a local variable in another function you just called (this is of course illegal).

  4. #4
    Hardware Engineer
    Join Date
    Sep 2001
    Posts
    1,398
    …printed value for last_recived will be one less than…
    Probably a precedence issue… that is, “last_recived = (mh->seq)” being performed before the “+1”

    Try adding another pair of parenthesis:
    last_recived = ( (mh->seq) +1) ;

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  2. Variable question I can't find answer to
    By joelmon in forum C++ Programming
    Replies: 3
    Last Post: 02-12-2002, 04:11 AM
  3. Need help
    By awkeller in forum C Programming
    Replies: 2
    Last Post: 12-09-2001, 03:02 PM
  4. Variable Allocation in a simple operating system
    By awkeller in forum C Programming
    Replies: 1
    Last Post: 12-08-2001, 02:26 PM