exc_bad_access error when compiling c code in xcode

This is a discussion on exc_bad_access error when compiling c code in xcode within the C Programming forums, part of the General Programming Boards category; Hi all, I'm relatively new to c code. I encountered an error when I tried to compile a code I ...

  1. #1
    Registered User
    Join Date
    Jul 2011
    Posts
    4

    exc_bad_access error when compiling c code in xcode

    Hi all,

    I'm relatively new to c code. I encountered an error when I tried to compile a code I modified using xcode. I encounter a "EXC_BAD_ACCESS error" when compiling. I have gone through the code several times and I cannot figure out why I'm getting the error at that point. I would appreciate any input.

    Thanks.

    Code:
    #include<math.h>
    #include<stdio.h>
    #include<string.h>
    #include<time.h>
    
    float bilinterp(int A1[10], float A2[400][10], float B1[400][10], float e, float f, int P, int NRe);
    float interp(float A1[100], float A2[100], float a, int P);
    
    int main(int argc, char **argv){
        
        float alfam[400][10],Clm[400][10],Cdm[400][10]; 
        float Vel[100],RAD,hub,pi,phi,alfa,Cn,Ct,delta,sigma,f1,f2,F,dr[200];
        float x[200],r[200],c[200],theta[200],tsr[100];
        float ab[2],a,b,mu,rho,tol,W,Re,CL,CD,dT,dQ,ac,Ka,converge1,converge2;
        FILE *fpin, *fpout, *fpconvergedataout;
        int i,j,cc,d,k,N,M,P,NRe,B,ITER,WARN,start_time,Rev[10],ReRef,quit,nonconverg,convergedata,TL,delim,maxiter;
        
        start_time=time(NULL);
        pi = 4.0*atan(1.0);
        delta = 0.1;
        ac = 0.4;
        nonconverg=0;
        delim=-1;
        
        
        fpin=fopen(argv[1],"r");
        fpout=fopen(argv[2],"w");
        
        
        printf("Reading File...\n");
        fscanf(fpin,"%d",&P);
        fscanf(fpin,"%d",&NRe);
        
        for(i=0;i<NRe;++i){fscanf(fpin,"%i",&Rev[i]);}
        
        for(i=0;i<NRe;++i){                   
            for(j=0;j<P;++j){
                fscanf(fpin,"%f",&alfam[j][i]);} // EXC_BAD_ACCESS at this point
        }

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    EXC_BAD_ACCESS is a runtime error, not a compile-time error, so you could not have gotten such an error while compiling.

    Are you sure that P is less than or equal to 400 and NRe is less than or equal to 10?

  3. #3
    Registered User
    Join Date
    Jul 2011
    Posts
    4
    Thanks for the quick response.

    NRe is actually 20 and P < 400

  4. #4
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,444
    Check the values of your fopen calls. While it's not likely here, since you supposedly use fpin successfully before the EXC_BAD_ACCESS, you should still do the following everytime you open a file
    Code:
    fpin = fopen(argv[12], "r");
    if (fpin == NULL) {
        perror("fopen error");
        // exit or return from function if appropriate
    }
    I'm with tabstop on the idea that P and NRe might be out of range. Check to make sure fscanf correctly read P and NRe. If it didn't, they will have random values from the stack, and could easily cause this error. fscanf returns the number of items successfully converted. Check the docs for specifics and Google for examples.

    As a matter of fact, any IO function you call should be checked for failure and handled appropriately.

  5. #5
    Registered User
    Join Date
    Jul 2011
    Posts
    4
    Thanks anduril462, I checked fpin earlier and it opened the file without any issues. I will verify the values of P and NRe to ensure they were probably assigned.

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by new_cuser View Post
    Thanks for the quick response.

    NRe is actually 20 and P < 400
    That's a big problem, because you've only got 10 slots in your array.

  7. #7
    Registered User
    Join Date
    Jul 2011
    Posts
    4
    Thanks tabstop, that was actually the problem.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Unknown error Xcode
    By Insectoid in forum C Programming
    Replies: 2
    Last Post: 07-15-2011, 05:05 PM
  2. C Code in Xcode
    By rmcinnes in forum C Programming
    Replies: 2
    Last Post: 04-11-2011, 07:27 AM
  3. Replies: 6
    Last Post: 03-09-2011, 05:38 AM
  4. Linking error in Xcode.
    By bhdavis1978 in forum C Programming
    Replies: 2
    Last Post: 03-19-2010, 02:32 PM
  5. compiling my code, i can't fix error
    By howru in forum C Programming
    Replies: 2
    Last Post: 04-22-2008, 03:38 AM

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