Wierd problem

This is a discussion on Wierd problem within the C Programming forums, part of the General Programming Boards category; The problem I'm having with my code is located at Code: void fGR(double x[NSTEPS], double y[NSTEPS], double sig[NSTEPS], double **GRalpha, ...

  1. #1
    Registered User
    Join Date
    Jul 2008
    Posts
    9

    Wierd problem

    The problem I'm having with my code is located at
    Code:
    void fGR(double x[NSTEPS], double y[NSTEPS], double sig[NSTEPS],
             double **GRalpha, double **GRbeta, int ma, double chi,
             int nu, double Q, double GRa[GRma])
    
     * * *
    
      fout1 << "GRma:" << ma << " GR[]:" << GRa[0] << " " << GRa[1] << endl;
    
      fout1 << "FF L50:  GRalpha[][0]\tGRalpha[][1]\tGRbeta[][0]" << endl;
      for(i=1; i<=ma; i++)
        {
            fout1 << endl << "54GR:" << GRa[0] << " " << GRa[1] << endl;
          fout1 << "[" << i << "][] \t ";
          for(j=1; j<=ma; j++)                                        
            {
              GRalpha[i][j] = 0.0;
              fout1 << GRalpha[i][j] << "\t\t";
            }
            fout1 << endl << "61GR:" << GRa[0] << " " << GRa[1] << endl;
          GRbeta[i][0] = 0.0;
          fout1 << GRbeta[i][0] << endl;
        }
    As you can see, there doesn't seem to be any trouble; I'm mostly just outputting some data to a file. The problem I'm having is (output; not code):

    Code:
    GRma:2 GR[]:430.819 6.0904
    FF L50:  GRalpha[][0]   GRalpha[][1]    GRbeta[][0]
    
    54GR:430.819 6.0904
    [1][]    0              0               
    61GR:430.819 6.0904
    0
    
    54GR:430.819 6.0904
    [2][]    0              0               
    61GR:0 6.0904
    0
    I input into the function GRa[0] = 430.819, GRa[1] = 6.0904. After the second iteration of the outer for loop, it suddenly changes GRa[0] to 0 for no apparent reason. Why would a program suddenly do this?

  2. #2
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    This isn't C, it's C++.

    Run it through a debugger and you shall see the answer . It'd also help if you cleaned the code up a bit, including indenting, avoid writing 'endl' until the end of lines (not at the start).

  3. #3
    Registered User
    Join Date
    Jul 2008
    Posts
    9
    If you just look at the output "fout <<", yeah, it would be C++, but that wasn't the point since I'm mostly writing C with C++ convenience.

    I can see that the could be confusing, but this isn't the entire code. Basically it comes down to my final question: why would a for-loop suddenly change the value through one iteration when I'm not even adjusting that term in the function?

    Here is a cleaner version of that portion (lines labeled where I send output):

    Code:
    void fGR(double x[NSTEPS], double y[NSTEPS], double sig[NSTEPS],
             double **GRalpha, double **GRbeta, int ma, double chi,
             int nu, double Q, double GRa[GRma])
    {
     * * *
    
    (50)
    
      for(i=1; i<=ma; i++)
        {
    (54)
          fout1 << "[" << i << "][] \t ";
          for(j=1; j<=ma; j++)                                        
            {
              GRalpha[i][j] = 0.0;
              fout1 << GRalpha[i][j] << "\t\t";
            }
    (61)
          GRbeta[i][0] = 0.0;
          fout1 << GRbeta[i][0] << endl;
        }
    
     * * *
    }

  4. #4
    Registered User The Dog's Avatar
    Join Date
    May 2002
    Location
    Cape Town
    Posts
    788
    Remove the file output stream and use printf instead, then check to see if does the same thing...

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    I'd guess that you are going beyond the size of your GRAlpha array, and it's overwriting other variables. Try changing your 0.0 to -4711.3 and see if the value that gets overwritten is changing along with it - I'm 99% sure it will. If that is what happens, then we need to see the declaration (and allocation if there is one) of the GRAlpha array.

    I'm particularly thinking that this line:
    Code:
          for(j=1; j<=ma; j++)
    goes one iteration further than you think.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  6. #6
    Registered User
    Join Date
    Jul 2008
    Posts
    9
    Yeah, it does (output, not code):

    Code:
    GRma:2 GR[]:430.819400 6.090400
    
    54GR:430.819400 6.090400
    
    61GR:430.819400 6.090400
    
    54GR:430.819400 6.090400
    
    61GR:0.000000 6.090400
    I only replaced the lines for that particular problem (separately replacing all of those in the loop). Additionally, I turned it into a dynamically allocated pointer (changed to double *GRa). It works up to this point as well.

  7. #7
    Registered User
    Join Date
    Jul 2008
    Posts
    9
    Here is if I increase the number:
    Code:
    GRma:2 GR[]:4308.194000 6.090400
    
    54GR:4308.194000 6.090400
    
    61GR:4308.194000 6.090400
    
    54GR:4308.194000 6.090400
    
    61GR:0.000000 6.090400
    Here is if I decrease the value:
    Code:
    GRma:2 GR[]:4.308194 6.090400
    
    54GR:4.308194 6.090400
    
    61GR:4.308194 6.090400
    
    54GR:4.308194 6.090400
    
    61GR:0.000000 6.090400
    Here is the pertinent declaration in my main() code:
    Code:
    int main()
    {
     * * *
      double GRbeta[GRma][0], GRalpha[GRma][GRma];
      double **beta, **alpha, *a;
      double GRX, GRN;
    
    //  GRX = 430.8194;
      GRX = 4.308194;
      GRN = 6.0904;
    
      GR[0] = GRX;
      GR[1] = GRN;
    
      alpha = dmatrix(1,GRma,1,GRma);
      beta  = dmatrix(1,GRma,0,0);
      a     = dvector(1,GRma);
    
      for(i=1; i<=GRma; i++)
        {
          alpha[i] = GRalpha[i];
          beta[i]  = GRbeta[i];
        }
      a = GR;
    
      oFile << "GR:" << a[0] << " " << a[1] << endl;
    
      fGR(x, y, sig, alpha, beta, GRma, GRchi, GRnu, GRQ, a);
    
     * * *
    }
    Here is the function as of now:
    Code:
    void fGR(double x[NSTEPS], double y[NSTEPS], double sig[NSTEPS],
             double **GRalpha, double **GRbeta, int ma, double chi,
             int nu, double Q, double *GRa)
    {}
    That is everything relevant to this problem. If there's anything I've missed, I'm basing this on the Numerical Recipes' gaussj and mrqmin functions.

  8. #8
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    As Mats pointed out, GRalpha[GRma] and GRbeta[GRma] just don't exist (arrays in C go from 0 to n-1, not 1 to n).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 03:51 PM
  2. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 07:54 PM
  3. Laptop Problem
    By Boomba in forum Tech Board
    Replies: 1
    Last Post: 03-07-2006, 06:24 PM
  4. Replies: 5
    Last Post: 11-07-2005, 11:34 PM
  5. half ADT (nested struct) problem...
    By CyC|OpS in forum C Programming
    Replies: 1
    Last Post: 10-26-2002, 09:37 AM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21