Function call changes variables that are supposed to be unrelated ?!?!

This is a discussion on Function call changes variables that are supposed to be unrelated ?!?! within the C Programming forums, part of the General Programming Boards category; I have been using a canned routine from numerical recipes called bnldev.c. when i call bnldev within my code, the ...

  1. #1
    Registered User
    Join Date
    Aug 2010
    Posts
    2

    Function call changes variables that are supposed to be unrelated ?!?!

    I have been using a canned routine from numerical recipes called bnldev.c. when i call bnldev within my code, the function apparently changes variables that should be completely unrelated! (for instance, it changes a counter variable used in the loop from where bnldev is called).

    does anyone know why this might be occurring and how i could fix it? i have attached the code reed3.c with some printf statements indicating an example of where the error occurs (lines 256-267, and bnldev is defined at the top of the code). i have also attached supporting libraries.

    thanks,

    chris
    Attached Files Attached Files

  2. #2
    cas
    cas is offline
    Registered User
    Join Date
    Sep 2007
    Posts
    979
    If your compiler has higher warning levels, crank them up.

    Gcc complains about stepsize (in reed3.c) being used uninitialized, which is correct. Icc spots even more problems which indicate a misunderstanding of how arrays work in C. When you have this:
    Code:
    int S[2];
    You have two ints: S[0] and S[1]. You do not have S[2]! My supposition is that you are under the belief that array indexing in C starts at 1, but it starts at 0. Fix all your array accesses and see if the problem persists.

  3. #3
    Registered User
    Join Date
    Aug 2010
    Posts
    2
    Thanks, that seems to solve the problems. I probably should invest in an icc license!

  4. #4
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,528
    Quote Originally Posted by cbauch View Post
    Thanks, that seems to solve the problems. I probably should invest in an icc license!
    You could also try Uno:

    Code:
    ~/tmp> uno -w reed3.c
    uno: in fct main, local variable never used (evaluated) 'buffer'
         statement  : reed3.c:134: float buffer;
    uno: in fct main, local variable never used (evaluated) 'num_vaccinated'
         statement  : reed3.c:123: int num_vaccinated;
    uno: in fct main, local variable never used (evaluated) 'prob_S_to_V'
         statement  : reed3.c:100: float prob_S_to_V,prob_V_to_S;
    uno: in fct main, local variable never used (evaluated) 'day_of_year'
         statement  : reed3.c:74: int jj,i,k,numdays,stepsize,day_of_year,samplefreq,total_years,year_vaccination_starts,total_days;
    uno: in fct main, uninitialized variable 'stepsize'
         statement  : reed3.c:159: aging_rate=((0.050000/365)/stepsize)
         declaration: reed3.c:74: int jj,i,k,numdays,stepsize,day_of_year,samplefreq,total_years,year_vaccination_starts,total_days;
    uno: in fct main, local variable never used (evaluated) 'numdays'
         statement  : reed3.c:74: int jj,i,k,numdays,stepsize,day_of_year,samplefreq,total_years,year_vaccination_starts,total_days;
    uno: in fct main, local variable never used (evaluated) 'argv'
         statement  : reed3.c:72: main(int argc,char *argv[])
         declaration: reed3.c:73: char *argv[];
    uno: in fct main, local variable never used (evaluated) 'argc'
         statement  : reed3.c:72: main(int argc,char *argv[])
         declaration: reed3.c:73: int argc;
    uno: reed3.c: 156 possible array indexing error 'N[2]' (array-size: 2)
    uno: in fct main, array index can exceed upper-bound (2>1), var 'k'
         statement  : reed3.c:188: A[k]=(int )(0.050000*N[k])
         declaration: reed3.c:74: int jj,i,k,numdays,stepsize,day_of_year,samplefreq,total_years,year_vaccination_starts,total_days;
    uno: in fct main, array index can exceed upper-bound (2>1), var 'k'
         statement  : reed3.c:188: A[k]=(int )(0.050000*N[k])
         declaration: reed3.c:74: int jj,i,k,numdays,stepsize,day_of_year,samplefreq,total_years,year_vaccination_starts,total_days;
    uno: in fct main, array index can exceed upper-bound (2>1), var 'k'
         statement  : reed3.c:189: O[k]=(int )(0.200000*N[k])
         declaration: reed3.c:74: int jj,i,k,numdays,stepsize,day_of_year,samplefreq,total_years,year_vaccination_starts,total_days;
    uno: in fct main, array index can exceed upper-bound (2>1), var 'k'
         statement  : reed3.c:189: O[k]=(int )(0.200000*N[k])
         declaration: reed3.c:74: int jj,i,k,numdays,stepsize,day_of_year,samplefreq,total_years,year_vaccination_starts,total_days;
    uno: in fct main, array index can exceed upper-bound (2>1), var 'k'
         statement  : reed3.c:190: S[k]=((N[k]-O[k])-A[k])
         declaration: reed3.c:74: int jj,i,k,numdays,stepsize,day_of_year,samplefreq,total_years,year_vaccination_starts,total_days;
    uno: in fct main, array index can exceed upper-bound (2>1), var 'k'
         statement  : reed3.c:190: S[k]=((N[k]-O[k])-A[k])
         declaration: reed3.c:74: int jj,i,k,numdays,stepsize,day_of_year,samplefreq,total_years,year_vaccination_starts,total_days;
    uno: in fct main, array index can exceed upper-bound (2>1), var 'k'
         statement  : reed3.c:190: S[k]=((N[k]-O[k])-A[k])
         declaration: reed3.c:74: int jj,i,k,numdays,stepsize,day_of_year,samplefreq,total_years,year_vaccination_starts,total_days;
    uno: in fct main, array index can exceed upper-bound (2>1), var 'k'
         statement  : reed3.c:190: S[k]=((N[k]-O[k])-A[k])
         declaration: reed3.c:74: int jj,i,k,numdays,stepsize,day_of_year,samplefreq,total_years,year_vaccination_starts,total_days;
    uno: in fct main, array index can exceed upper-bound (2>1), var 'k'
         statement  : reed3.c:191: Ainv[k]=0
         declaration: reed3.c:74: int jj,i,k,numdays,stepsize,day_of_year,samplefreq,total_years,year_vaccination_starts,total_days;
    uno: in fct main, array index can exceed upper-bound (2>1), var 'k'
         statement  : reed3.c:192: V[k]=0
         declaration: reed3.c:74: int jj,i,k,numdays,stepsize,day_of_year,samplefreq,total_years,year_vaccination_starts,total_days;
    uno: reed3.c: 232 possible array indexing error 'N[2]' (array-size: 2)
    uno: reed3.c: 232 possible array indexing error 'S[2]' (array-size: 2)
    uno: reed3.c: 232 possible array indexing error 'S[2]' (array-size: 2)
    uno: reed3.c: 235 possible array indexing error 'N[2]' (array-size: 2)
    uno: reed3.c: 235 possible array indexing error 'O[2]' (array-size: 2)
    uno: reed3.c: 235 possible array indexing error 'O[2]' (array-size: 2)
    uno: reed3.c: 238 possible array indexing error 'N[2]' (array-size: 2)
    uno: reed3.c: 238 possible array indexing error 'A[2]' (array-size: 2)
    uno: reed3.c: 238 possible array indexing error 'A[2]' (array-size: 2)
    uno: reed3.c: 241 possible array indexing error 'N[2]' (array-size: 2)
    uno: reed3.c: 241 possible array indexing error 'Ainv[2]' (array-size: 2)
    uno: reed3.c: 241 possible array indexing error 'Ainv[2]' (array-size: 2)
    uno: reed3.c: 244 possible array indexing error 'N[2]' (array-size: 2)
    uno: reed3.c: 244 possible array indexing error 'V[2]' (array-size: 2)
    uno: reed3.c: 244 possible array indexing error 'V[2]' (array-size: 2)
    uno: in fct main, array index can exceed upper-bound (2>1), var 'k'
         statement  : reed3.c:254: prob_S_to_O=((float )((beta*(1-mu))*O[k])/(N[k]*stepsize))
         declaration: reed3.c:74: int jj,i,k,numdays,stepsize,day_of_year,samplefreq,total_years,year_vaccination_starts,total_days;
    uno: in fct main, array index can exceed upper-bound (2>1), var 'k'
         statement  : reed3.c:254: prob_S_to_O=((float )((beta*(1-mu))*O[k])/(N[k]*stepsize))
         declaration: reed3.c:74: int jj,i,k,numdays,stepsize,day_of_year,samplefreq,total_years,year_vaccination_starts,total_days;
    uno: in fct main, array index can exceed upper-bound (2>1), var 'k'
         statement  : reed3.c:259: num_S_to_O[k]=(int )bnldev(prob_S_to_O,S[k],&(idum))
         declaration: reed3.c:74: int jj,i,k,numdays,stepsize,day_of_year,samplefreq,total_years,year_vaccination_starts,total_days;
    uno: in fct main, array index can exceed upper-bound (2>1), var 'k'
         statement  : reed3.c:259: num_S_to_O[k]=(int )bnldev(prob_S_to_O,S[k],&(idum))
         declaration: reed3.c:74: int jj,i,k,numdays,stepsize,day_of_year,samplefreq,total_years,year_vaccination_starts,total_days;
    uno: in fct main, array index can exceed upper-bound (2>1), var 'k'
         statement  : reed3.c:269: prob_S_to_A=(((float )((beta*(1-epsilon))*A[k])/(N[k]*stepsize))+((float )((beta*mu)*O[k])/(N[k]*stepsize)))
         declaration: reed3.c:74: int jj,i,k,numdays,stepsize,day_of_year,samplefreq,total_years,year_vaccination_starts,total_days;
    uno: in fct main, array index can exceed upper-bound (2>1), var 'k'
         statement  : reed3.c:269: prob_S_to_A=(((float )((beta*(1-epsilon))*A[k])/(N[k]*stepsize))+((float )((beta*mu)*O[k])/(N[k]*stepsize)))
         declaration: reed3.c:74: int jj,i,k,numdays,stepsize,day_of_year,samplefreq,total_years,year_vaccination_starts,total_days;
    uno: in fct main, array index can exceed upper-bound (2>1), var 'k'
         statement  : reed3.c:269: prob_S_to_A=(((float )((beta*(1-epsilon))*A[k])/(N[k]*stepsize))+((float )((beta*mu)*O[k])/(N[k]*stepsize)))
         declaration: reed3.c:74: int jj,i,k,numdays,stepsize,day_of_year,samplefreq,total_years,year_vaccination_starts,total_days;
    uno: in fct main, array index can exceed upper-bound (2>1), var 'k'
         statement  : reed3.c:269: prob_S_to_A=(((float )((beta*(1-epsilon))*A[k])/(N[k]*stepsize))+((float )((beta*mu)*O[k])/(N[k]*stepsize)))
         declaration: reed3.c:74: int jj,i,k,numdays,stepsize,day_of_year,samplefreq,total_years,year_vaccination_starts,total_days;
    uno: in fct main, array index can exceed upper-bound (2>1), var 'k'
         statement  : reed3.c:270: num_S_to_A[k]=(int )bnldev(prob_S_to_A,S[k],&(idum))
         declaration: reed3.c:74: int jj,i,k,numdays,stepsize,day_of_year,samplefreq,total_years,year_vaccination_starts,total_days;
    uno: in fct main, array index can exceed upper-bound (2>1), var 'k'
         statement  : reed3.c:270: num_S_to_A[k]=(int )bnldev(prob_S_to_A,S[k],&(idum))
         declaration: reed3.c:74: int jj,i,k,numdays,stepsize,day_of_year,samplefreq,total_years,year_vaccination_starts,total_days;
    uno: in fct main, array index can exceed upper-bound (2>1), var 'k'
         statement  : reed3.c:272: prob_S_to_Ainv=((float )((beta*epsilon)*A[k])/(N[k]*stepsize))
         declaration: reed3.c:74: int jj,i,k,numdays,stepsize,day_of_year,samplefreq,total_years,year_vaccination_starts,total_days;
    uno: in fct main, array index can exceed upper-bound (2>1), var 'k'
         statement  : reed3.c:272: prob_S_to_Ainv=((float )((beta*epsilon)*A[k])/(N[k]*stepsize))
         declaration: reed3.c:74: int jj,i,k,numdays,stepsize,day_of_year,samplefreq,total_years,year_vaccination_starts,total_days;
    uno: in fct main, array index can exceed upper-bound (2>1), var 'k'
         statement  : reed3.c:273: num_S_to_Ainv[k]=(int )bnldev(prob_S_to_Ainv,S[k],&(idum))
         declaration: reed3.c:74: int jj,i,k,numdays,stepsize,day_of_year,samplefreq,total_years,year_vaccination_starts,total_days;
    uno: in fct main, array index can exceed upper-bound (2>1), var 'k'
         statement  : reed3.c:273: num_S_to_Ainv[k]=(int )bnldev(prob_S_to_Ainv,S[k],&(idum))
         declaration: reed3.c:74: int jj,i,k,numdays,stepsize,day_of_year,samplefreq,total_years,year_vaccination_starts,total_days;
    uno: in fct main, array index can exceed upper-bound (2>1), var 'k'
         statement  : reed3.c:277: num_S_to_V[k]=(int )(((S[k]*coverage)*efficacy)/stepsize)
         declaration: reed3.c:74: int jj,i,k,numdays,stepsize,day_of_year,samplefreq,total_years,year_vaccination_starts,total_days;
    uno: in fct main, array index can exceed upper-bound (2>1), var 'k'
         statement  : reed3.c:277: num_S_to_V[k]=(int )(((S[k]*coverage)*efficacy)/stepsize)
         declaration: reed3.c:74: int jj,i,k,numdays,stepsize,day_of_year,samplefreq,total_years,year_vaccination_starts,total_days;
    uno: in fct main, array index can exceed upper-bound (2>1), var 'k'
         statement  : reed3.c:282: num_V_to_S[k]=(int )bnldev(prob_V_to_S,V[k],&(idum))
         declaration: reed3.c:74: int jj,i,k,numdays,stepsize,day_of_year,samplefreq,total_years,year_vaccination_starts,total_days;
    uno: in fct main, array index can exceed upper-bound (2>1), var 'k'
         statement  : reed3.c:282: num_V_to_S[k]=(int )bnldev(prob_V_to_S,V[k],&(idum))
         declaration: reed3.c:74: int jj,i,k,numdays,stepsize,day_of_year,samplefreq,total_years,year_vaccination_starts,total_days;
    uno: in fct main, array index can exceed upper-bound (2>1), var 'k'
         statement  : reed3.c:283: num_O_to_S[k]=(int )bnldev(prob_O_to_S,O[k],&(idum))
         declaration: reed3.c:74: int jj,i,k,numdays,stepsize,day_of_year,samplefreq,total_years,year_vaccination_starts,total_days;
    uno: in fct main, array index can exceed upper-bound (2>1), var 'k'
         statement  : reed3.c:283: num_O_to_S[k]=(int )bnldev(prob_O_to_S,O[k],&(idum))
         declaration: reed3.c:74: int jj,i,k,numdays,stepsize,day_of_year,samplefreq,total_years,year_vaccination_starts,total_days;
    uno: in fct main, array index can exceed upper-bound (2>1), var 'k'
         statement  : reed3.c:284: num_A_to_S[k]=(int )bnldev(prob_A_to_S,A[k],&(idum))
         declaration: reed3.c:74: int jj,i,k,numdays,stepsize,day_of_year,samplefreq,total_years,year_vaccination_starts,total_days;
    uno: in fct main, array index can exceed upper-bound (2>1), var 'k'
         statement  : reed3.c:284: num_A_to_S[k]=(int )bnldev(prob_A_to_S,A[k],&(idum))
         declaration: reed3.c:74: int jj,i,k,numdays,stepsize,day_of_year,samplefreq,total_years,year_vaccination_starts,total_days;
    uno: in fct main, array index can exceed upper-bound (2>1), var 'k'
         statement  : reed3.c:285: num_Ainv_to_S[k]=(int )bnldev(prob_Ainv_to_S,Ainv[k],&(idum))
         declaration: reed3.c:74: int jj,i,k,numdays,stepsize,day_of_year,samplefreq,total_years,year_vaccination_starts,total_days;
    uno: in fct main, array index can exceed upper-bound (2>1), var 'k'
         statement  : reed3.c:285: num_Ainv_to_S[k]=(int )bnldev(prob_Ainv_to_S,Ainv[k],&(idum))
         declaration: reed3.c:74: int jj,i,k,numdays,stepsize,day_of_year,samplefreq,total_years,year_vaccination_starts,total_days;
    uno: in fct main, array index can exceed upper-bound (2>1), var 'k'
         statement  : reed3.c:288: num_S_death[k]=(int )bnldev(prob_S_death,S[k],&(idum))
         declaration: reed3.c:74: int jj,i,k,numdays,stepsize,day_of_year,samplefreq,total_years,year_vaccination_starts,total_days;
    uno: in fct main, array index can exceed upper-bound (2>1), var 'k'
         statement  : reed3.c:288: num_S_death[k]=(int )bnldev(prob_S_death,S[k],&(idum))
         declaration: reed3.c:74: int jj,i,k,numdays,stepsize,day_of_year,samplefreq,total_years,year_vaccination_starts,total_days;
    uno: in fct main, array index can exceed upper-bound (2>1), var 'k'
         statement  : reed3.c:289: num_O_death[k]=(int )bnldev(prob_O_death,O[k],&(idum))
         declaration: reed3.c:74: int jj,i,k,numdays,stepsize,day_of_year,samplefreq,total_years,year_vaccination_starts,total_days;
    uno: in fct main, array index can exceed upper-bound (2>1), var 'k'
         statement  : reed3.c:289: num_O_death[k]=(int )bnldev(prob_O_death,O[k],&(idum))
         declaration: reed3.c:74: int jj,i,k,numdays,stepsize,day_of_year,samplefreq,total_years,year_vaccination_starts,total_days;
    uno: in fct main, array index can exceed upper-bound (2>1), var 'k'
         statement  : reed3.c:290: num_V_death[k]=(int )bnldev(prob_V_death,V[k],&(idum))
         declaration: reed3.c:74: int jj,i,k,numdays,stepsize,day_of_year,samplefreq,total_years,year_vaccination_starts,total_days;
    uno: in fct main, array index can exceed upper-bound (2>1), var 'k'
         statement  : reed3.c:290: num_V_death[k]=(int )bnldev(prob_V_death,V[k],&(idum))
         declaration: reed3.c:74: int jj,i,k,numdays,stepsize,day_of_year,samplefreq,total_years,year_vaccination_starts,total_days;
    uno: in fct main, array index can exceed upper-bound (2>1), var 'k'
         statement  : reed3.c:291: num_A_death[k]=(int )bnldev(prob_A_death,A[k],&(idum))
         declaration: reed3.c:74: int jj,i,k,numdays,stepsize,day_of_year,samplefreq,total_years,year_vaccination_starts,total_days;
    uno: in fct main, array index can exceed upper-bound (2>1), var 'k'
         statement  : reed3.c:291: num_A_death[k]=(int )bnldev(prob_A_death,A[k],&(idum))
         declaration: reed3.c:74: int jj,i,k,numdays,stepsize,day_of_year,samplefreq,total_years,year_vaccination_starts,total_days;
    uno: in fct main, array index can exceed upper-bound (2>1), var 'k'
         statement  : reed3.c:292: num_Ainv_death[k]=(int )bnldev(prob_Ainv_death,Ainv[k],&(idum))
         declaration: reed3.c:74: int jj,i,k,numdays,stepsize,day_of_year,samplefreq,total_years,year_vaccination_starts,total_days;
    uno: in fct main, array index can exceed upper-bound (2>1), var 'k'
         statement  : reed3.c:292: num_Ainv_death[k]=(int )bnldev(prob_Ainv_death,Ainv[k],&(idum))
         declaration: reed3.c:74: int jj,i,k,numdays,stepsize,day_of_year,samplefreq,total_years,year_vaccination_starts,total_days;
    uno: in fct main, array index can exceed upper-bound (2>1), var 'k'
         statement  : reed3.c:279: num_S_to_V[k]=0
         declaration: reed3.c:74: int jj,i,k,numdays,stepsize,day_of_year,samplefreq,total_years,year_vaccination_starts,total_days;
    uno: warning, ran1.c:36: condition has side effect on 'temp'
    uno:global: nr.h:18 possibly redundant field 'nch'
    uno:global: nr.h:18 possibly redundant field 'ncod'
    uno:global: nr.h:18 possibly redundant field 'nodemax'
    uno:global: nr.h:18 possibly redundant field 'left'
    uno:global: nr.h:18 possibly redundant field 'right'
    uno:global: nr.h:18 possibly redundant field 'icod'
    uno:global: nr.h:11 possibly redundant field 'iupb'
    uno:global: nr.h:11 possibly redundant field 'ncum'
    uno:global: nr.h:11 possibly redundant field 'minint'
    uno:global: nr.h:11 possibly redundant field 'nch'
    uno:global: nr.h:11 possibly redundant field 'ncumfq'
    uno:global: nr.h:11 possibly redundant field 'jdif'
    uno:global: nr.h:11 possibly redundant field 'ilob'
    uno:global: nr.h:11 possibly redundant field 'nrad'
    uno:global: nr.h:11 possibly redundant field 'nc'
    uno:global: nr.h:5 possibly redundant field 'r'
    uno:global: nr.h:5 possibly redundant field 'i'
    Another tool to consider, in terms of static code checking, is some kind of lint program. splint works nicely under *nix type systems.
    Last edited by kermit; 08-16-2010 at 10:05 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiling C in Visual Studio 2005
    By emanresu in forum C Programming
    Replies: 3
    Last Post: 11-16-2009, 03:25 AM
  2. Undefined Reference Compiling Error
    By AlakaAlaki in forum C++ Programming
    Replies: 1
    Last Post: 06-27-2008, 11:45 AM
  3. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  4. <Gulp>
    By kryptkat in forum Windows Programming
    Replies: 7
    Last Post: 01-14-2006, 12:03 PM
  5. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM

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