This is the mail archive of the
gcc-help@gcc.gnu.org
mailing list for the GCC project.
Re: funny output from g++
- From: Claudio Bley <Claudio dot Bley at Student dot Uni-Magdeburg dot DE>
- To: gcc-help at gcc dot gnu dot org
- Date: Wed, 5 May 2004 13:13:39 +0200
- Subject: Re: funny output from g++
- References: <20040505193940.1141132b.john.habermann@jcu.edu.au>
On Wed, May 05, 2004 at 07:39:40PM +1000, John Habermann wrote:
Hello John,
> I have just been trying to write a program to learn how to use arrays
> for my c++ class and I get different output from the program
> when it is compiled with g++-2.95, g++-3.2 and g++-3.3.
>
> I have included the program below but what it does is just take the
> following data from a file data.txt:
>
> Bob 17
> Sally 23
> Bill 13
> Wendy 87
> Jack 33
> Karen 64
> Wilbur 79
> Betty 42
>
> put that in 2 arrays names[] and ages[] and then I need to find the
> maximum age and output the name and age to the screen.
>
> g++-3.2 returns the correct value 87
> g++-2.95 returns 805464728
> g++-3.3 returns 268439248
The problem with your program is, that your data file only has 8 entries but
you use all the 10 elements of your array to compute the maximum value.
You have to remember that with local storage usually the memory is not
zero'ed and hence where not explicitely initialised contains random garbage.
G++ 3.2 seems to initialise the arrays to 0 (which it is allowed to I'd say BUT
it is *not* *required*) or it just happened that the 2 last elements of the
array were smaller than the maximum value of your data. So, luckily
it returned the expected value.
int i = 0;
while (i < MAX_ENTRIES && fi >> names[i] && fi >> ages[i]) {
++i;
}
maxAge = 0;
for(; i <= 0; --i) {
// ...
}
--
Claudio