Thread: Why a variable's value changes without altering it?

  1. #1
    Registered User
    Join Date
    Nov 2011
    Location
    Laramie, Wyoming
    Posts
    8

    Why a variable's value changes without altering it?

    Hi there,

    I'm writing a function to solve polynomial problems. A strange thing happened, that, the variable b, in the program, changes to another value, though I didn't alter it. Can someone give me a hint?

    Code:
    #include <stdio.h>
    #include <math.h> 
    
    void polinomial(double a, double b, double c, double * x1, double * x2) {
        double bSqrMns4ac;
        printf("{a, b, c} = {%f, %f, %f}\n", a, b, c);
        bSqrMns4ac = pow(b, 2) - 4 * a * c;
        printf("bSqrMns4ac = %f\n", bSqrMns4ac);
        if (bSqrMns4ac < 0) {
            *x1 = 0;
            *x2 = 0;
        } else {
            *x1 = (-b - sqrt(bSqrMns4ac))/ (2 * a);
            *x2 = (-b + sqrt(bSqrMns4ac))/ (2 * a);
            printf("{x1, x2} = {%f, %f}\n", (-b - sqrt(bSqrMns4ac))/ (2 * a), (-b + sqrt(bSqrMns4ac))/ (2 * a));
        }
    }
    
    int main (void) {
        double beta1 = -1.88138742; // beta1: price coefficient
        double beta2 = 0.07182447; // beta2: price^2 coefficient; Note that beta2 is a in the polinomial.
        double beta3 = 0.30941525; // beta3: coefficient of MSC labelling effect on wild salmon
        double beta4 = 0.70772077; // beta4: coefficient of AB labelling effect on farmed cod
        double beta5 = 0.69330401; // beta5: coefficient of AB labelling effect on farmed salmon
        double betaLabel[3] = {beta3, beta4, beta5}; // We have three c for the polinomial; each labelling effect coefficient reflects a c.
        int priceStar = 8;
        double b, wtpMSC[2], wtpABCod[2], wtpABSalmon[2];
        
        printf("\nbeta1 = %f, \nbeta2 = %f, \nbeta3 = %f (%f), \nbeta4 = %f (%f), \nbeta5 = %f (%f)\n\n", beta1, beta2, beta3, betaLabel[0], beta4, betaLabel[1], beta5, betaLabel[2]);
        
        b = 2 * beta2 * priceStar + beta1;    
        polinomial(beta2, b, betaLabel[0], & wtpMSC[1], & wtpMSC[2]);
        printf("WTP(MSC) = %f, %f\n\n", wtpMSC[1], wtpMSC[2]);
         
        polinomial(beta2, b, betaLabel[1], & wtpABCod[1], & wtpABCod[2]);
        printf("WTP(AB on cod) = %f, %f\n\n", wtpABCod[1], wtpABCod[2]);
         
        polinomial(beta2, b, betaLabel[2], & wtpABSalmon[1], & wtpABSalmon[2]);
        printf("WTP(AB on salmon) = %f, %f\n\n", wtpABSalmon[1], wtpABSalmon[2]);
        
        return 0;
    }
    Last edited by xianwen; 11-28-2011 at 05:06 PM.

  2. #2
    Registered User TheBigH's Avatar
    Join Date
    May 2010
    Location
    Melbourne, Australia
    Posts
    426
    You've declared it twice.
    Code:
    while(!asleep) {
       sheep++;
    }

  3. #3
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by xianwen View Post
    I'm writing a function to solve polynomial problems. A strange thing happened, that, the variable b, in the program, changes to another value, though I didn't alter it.
    Which "b" is that?

    Code:
            double a = -1.88138742;
            double b = 0.07182447;
            double c[3] = {0.30941525, 0.70772077, 0.69330401};
            double b, x1[2], x2[2], x3[2];
    Presuming this didn't result in a compiler error (it probably should), I'd guess b #2 counts, which is uninitialized, and hence, a random value.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  4. #4
    Registered User
    Join Date
    Nov 2011
    Location
    Laramie, Wyoming
    Posts
    8
    Hi.

    Thank you. You were right. After removing second b, it worked. However, it didn't solve the original problem ('cause the code I put on-line was a simplified version).

    I've now put the original code in the post. Can you have another look at it?
    Quote Originally Posted by TheBigH View Post
    You've declared it twice.

  5. #5
    Registered User
    Join Date
    Nov 2011
    Location
    Laramie, Wyoming
    Posts
    8
    Hi MK27,

    Thank you for your hints.

    I've updated the codes to the one I'm actually running. Can you have another look at it? It doesn't have duplicated declaration problem; but still, the value of b changes.

    Quote Originally Posted by MK27 View Post
    Which "b" is that?

    Code:
            double a = -1.88138742;
            double b = 0.07182447;
            double c[3] = {0.30941525, 0.70772077, 0.69330401};
            double b, x1[2], x2[2], x3[2];
    Presuming this didn't result in a compiler error (it probably should), I'd guess b #2 counts, which is uninitialized, and hence, a random value.

  6. #6
    Registered User TheBigH's Avatar
    Join Date
    May 2010
    Location
    Melbourne, Australia
    Posts
    426
    Aha. I found it.

    The problem is that you've declared these:
    double b, wtpMSC[2], wtpABCod[2], wtpABSalmon[2];

    But then you call this:
    polinomial(beta2, b, betaLabel[0], & wtpMSC[1], & wtpMSC[2]);

    wtpMSC[2] does not exist. You are accessing and modifying memory that you shouldn't, and by bad luck this is messing with b.
    Code:
    while(!asleep) {
       sheep++;
    }

  7. #7
    Registered User
    Join Date
    Nov 2011
    Location
    Laramie, Wyoming
    Posts
    8
    Hi TheBigH,

    Thank you very much for finding one of the errors. However, the value of b still changes.

    As I changed the code to

    Code:
    #include <stdio.h>
    #include <math.h> 
    
    void polinomial(double a, double b, double c, double * x1, double * x2) {
        double bSqrMns4ac;
        printf("{a, b, c} = {%f, %f, %f}\n", a, b, c);
        bSqrMns4ac = pow(b, 2) - 4 * a * c;
        printf("bSqrMns4ac = %f\n", bSqrMns4ac);
        if (bSqrMns4ac < 0) {
            *x1 = 0;
            *x2 = 0;
        } else {
            *x1 = (-b - sqrt(bSqrMns4ac))/ (2 * a);
            *x2 = (-b + sqrt(bSqrMns4ac))/ (2 * a);
            printf("{x1, x2} = {%f, %f}\n", (-b - sqrt(bSqrMns4ac))/ (2 * a), (-b + sqrt(bSqrMns4ac))/ (2 * a));
        }
    }
    
    int main (void) {
        double beta1 = -1.88138742; // beta1: price coefficient
        double beta2 = 0.07182447; // beta2: price^2 coefficient; Note that beta2 is a in the polinomial.
        double beta3 = 0.30941525; // beta3: coefficient of MSC labelling effect on wild salmon
        double beta4 = 0.70772077; // beta4: coefficient of AB labelling effect on farmed cod
        double beta5 = 0.69330401; // beta5: coefficient of AB labelling effect on farmed salmon
        double betaLabel[3] = {beta3, beta4, beta5}; // We have three c for the polinomial; each labelling effect coefficient reflects a c.
        int priceStar = 8;
        double b;
        double wtpMSC[2], wtpABCod[2], wtpABSalmon[2];
        
        printf("\nbeta1 = %f, \nbeta2 = %f, \nbeta3 = %f (%f), \nbeta4 = %f (%f), \nbeta5 = %f (%f)\n\n", beta1, beta2, beta3, betaLabel[0], beta4, betaLabel[1], beta5, betaLabel[2]);
        
        b = 2 * beta2 * priceStar + beta1;    
        polinomial(beta2, b, betaLabel[0], & wtpMSC[0], & wtpMSC[1]);
        printf("WTP(MSC) = %f, %f\n\n", wtpMSC[0], wtpMSC[1]);
        
        polinomial(beta2, b, betaLabel[1], & wtpABCod[0], & wtpABCod[1]);
        printf("WTP(AB on cod) = %f, %f\n\n", wtpABCod[0], wtpABCod[1]);
        
        polinomial(beta2, b, betaLabel[2], & wtpABSalmon[0], & wtpABSalmon[1]);
        printf("WTP(AB on salmon) = %f, %f\n\n", wtpABSalmon[0], wtpABSalmon[1]);
        
        return 0;
    }
    , the value of b changes to x2 when polinomial was called for the first time.

    I don't really know why it behaves this way; but I guess we're closer to finding out the last bug.

  8. #8
    Registered User TheBigH's Avatar
    Join Date
    May 2010
    Location
    Melbourne, Australia
    Posts
    426
    Hmmm, this is very strange. I can't reproduce the bug now. When I compile and run it, b does not change at all.
    Code:
    while(!asleep) {
       sheep++;
    }

  9. #9
    Registered User
    Join Date
    Nov 2011
    Location
    Laramie, Wyoming
    Posts
    8
    Then it may be the problem of my compiler. Which compiler do you use?

  10. #10
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    I can't replicate that behavior with the code you just posted. How are you determining that b changes to x2? What print statement is showing that? I added code to print b immediately after you calculate it, then again after every call to polinomial. Here's the output:
    Code:
    $ ./fish
    
    
    beta1 = -1.881387,
    beta2 = 0.071824,
    beta3 = 0.309415 (0.309415),
    beta4 = 0.707721 (0.707721),
    beta5 = 0.693304 (0.693304)
    
    
    b = -0.732196
    {a, b, c} = {0.071824, -0.732196, 0.309415}
    bSqrMns4ac = 0.447216
    {x1, x2} = {0.441726, 9.752514}
    b = -0.732196
    WTP(MSC) = 0.441726, 9.752514
    
    
    {a, b, c} = {0.071824, -0.732196, 0.707721}
    bSqrMns4ac = 0.332784
    {x1, x2} = {1.081257, 9.112983}
    b = -0.732196
    WTP(AB on cod) = 1.081257, 9.112983
    
    
    {a, b, c} = {0.071824, -0.732196, 0.693304}
    bSqrMns4ac = 0.336926
    {x1, x2} = {1.056343, 9.137897}
    b = -0.732196
    WTP(AB on salmon) = 1.056343, 9.137897
    Note that the first one is immediately after you calculate b = 2 * beta2 * priceStar + beta1. The rest are after each call to polinomial. It never changes. Perhaps the code wasn't rebuilt properly to include the most recent changes?

  11. #11
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Ahh, I see TheBigH already got to it (I'm slow today). I use GCC 4.4.5. Which compiler do you use?

  12. #12
    Registered User
    Join Date
    Nov 2011
    Location
    Laramie, Wyoming
    Posts
    8
    Thank you very much anduril462. I was using tcc TCC : Tiny C Compiler under Windows 'cause I was using Windows in the school.

    Now I'm at home and the code is running fine with GCC. I've sent and email to tcc mailing list and hopefully they'll be able to answer me.

    Have a good night

    xianwen

    Quote Originally Posted by anduril462 View Post
    Ahh, I see TheBigH already got to it (I'm slow today). I use GCC 4.4.5. Which compiler do you use?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help: Simple String Altering
    By mrdibby in forum C Programming
    Replies: 8
    Last Post: 11-16-2009, 05:16 PM
  2. Need help with altering info on a txt file
    By Netflyer in forum C Programming
    Replies: 29
    Last Post: 02-14-2008, 07:23 AM
  3. name altering
    By lilhawk2892 in forum C++ Programming
    Replies: 17
    Last Post: 09-15-2006, 09:18 PM
  4. Altering The Program's Executable?
    By Aidman in forum C++ Programming
    Replies: 7
    Last Post: 12-31-2002, 05:11 AM
  5. Altering files in another file system??
    By Raavin in forum C++ Programming
    Replies: 2
    Last Post: 09-28-2001, 09:18 AM