This is the mail archive of the cygwin@cygwin.com mailing list for the Cygwin project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: cygwin gcc and arrays -- Possible bug???


Cynthia,

On Wed, 28 Aug 2002, Cynthia Randles wrote:

> > Which libraries are you linking in?
> #include <stdio.h>
> #include <math.h>

Well, I actually meant the libraries specified to gcc on the command line,
with the -l flag, but it's probably irrelevant in light of the other bug...

> > How are you allocating/declaring rad_array and vol_array?
> #define NUMBINS (51 - 1)
>
> float rad_array[NUMBINS], vol_array[NUMBINS];
>
> > > I have the following lines of code:
> > >
> > >         for (i = 0; i <= NUMBINS; ++i)
>
> Please also check your loop limits - are you sure you wanted
> > a '<=' there, rather than a '<'?

Here's that other bug I promised... :-)
I deliberately put your loop limits next to your array declaration.
Notice that the way you declared the arrays, they will have a valid range
of indices from 0 to NUMBINS-1.  However, in the loop, the variable you
use to index the arrays (i) will get up to NUMBINS.  Since the arrays are
declared consecutively, the second (vol_array) is likely to follow the
first (rad_array).  Therefore, writing into rad_array[NUMBINS] will
likely write to vol_array[0].

>From your output, it looks like you tested your code by setting NUMBINS to
0.  Therefore, you allocate two consecutive 0-sized arrays, and they will
probably share the same space, which is why you get the output you get.

> Yes.  When I compile it with gcc on another machine, I get the right
> results.

This is probably due to your luck and the fact that gcc does different
alignment on different machines.  I'll venture to guess that "another
machine" is not a PC...  In any case, you should either change your loop
limits to avoid i==NUMBINS, or declare your arrays with size [NUMBINS+1],
because as it stands, the code is incorrect.
	Igor

On Wed, 28 Aug 2002, Cynthia Randles wrote:

> Dear Igor:
>
> Igor Pechtchanski wrote:
> >
> > Which version of gcc are you using?
>
> Well, since I just downloaded cygwin last week, I assume I have the
> latest version of gcc.  I think it is gcc-2.95.
>
> > Which libraries are you linking in?
> #include <stdio.h>
> #include <math.h>
>
>
> > How are you allocating/declaring rad_array and vol_array?
> #define NUMBINS (51 - 1)
>
>  float rad_array[NUMBINS], vol_array[NUMBINS];
>
> >Is this a C or  C++ program?
> C
>
> Please also check your loop limits - are you sure you wanted
> > a '<=' there, rather than a '<'?
>
> Yes.  When I compile it with gcc on another machine, I get the right
> results.
>
> If you have any ideas, please let me know.  Thanks for your help.
>
> Cynthia
>
>
> > On Tue, 27 Aug 2002, Cynthia Randles wrote:
> >
> > > I was wondering if anyone could help me.
> > > I have the following lines of code:
> > >
> > >         for (i = 0; i <=  NUMBINS; ++i)
> > >         {
> > >               printf("--------read in input files---------\n");
> > >               printf("The value if i is %d\n", i);
> > >               fscanf(ifp1, "%f\n", &radius_microns);
> > >               fscanf(ifp2, "%f\n", &volfraction);
> > >               printf("The value of radius is %f\n", radius_microns);
> > >               rad_array[i]= radius_microns;
> > >
> > >               printf("The radius is rad_array[%d]=%f\n\n",i, rad_array[i]);
> > >
> > >               printf("The value of volfraction is %f\n", volfraction);
> > >               vol_array[i] = volfraction;
> > >               printf("The volfraction is vol_array[%d]=%f\n", i, vol_array[i]);
> > >               printf("The value of rad_array[%i] is %f\n", i, rad_array[i]);
> > >               printf("----------end of read in input file------\n\n");
> > >         }
> > >
> > > which, in cygwin produces:
> > >
> > > --------read in input files---------
> > > The value of i is 0
> > > The value of radius is 0.200000
> > > The radius is rad_array[0] = 0.200000
> > >
> > > The value of volfraction is 0.330000
> > > The volfraction is vol_array[0]=0.330000
> > > The value of rad_array[0] is 0.330000
> > > ----------end of read in input file-----
> > >
> > > as you can see, for some reason rad_array[0] is being reassigned even
> > > though I never ask for that!!!
> > >
> > > my red-hat linux gcc produces:
> > >
> > > --------read in input files---------
> > > The value of i is 0
> > > The value of radius is 0.200000
> > > The radius is rad_array[0] = 0.200000
> > >
> > > The value of volfraction is 0.330000
> > > The volfraction is vol_array[0]=0.330000
> > > The value of rad_array[0] is 0.200000
> > > ----------end of read in input file-----
> > >
> > > which is the right answer.  I cannot figure out what is going
> > > on with cygwin, and I haven't found a post that can help me.  Any
> > > help is appreciated.
> > >
> > > Thanks,
> > > Cynthia

-- 
				http://cs.nyu.edu/~pechtcha/
      |\      _,,,---,,_		pechtcha@cs.nyu.edu
ZZZzz /,`.-'`'    -.  ;-;;,_		igor@watson.ibm.com
     |,4-  ) )-,_. ,\ (  `'-'		Igor Pechtchanski
    '---''(_/--'  `-'\_) fL	a.k.a JaguaR-R-R-r-r-r-.-.-.  Meow!

It took the computational power of three Commodore 64s to fly to the moon.
It takes a 486 to run Windows 95.  Something is wrong here. -- SC sig file


--
Unsubscribe info:      http://cygwin.com/ml/#unsubscribe-simple
Bug reporting:         http://cygwin.com/bugs.html
Documentation:         http://cygwin.com/docs.html
FAQ:                   http://cygwin.com/faq/


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]