Compiler:
$ g++ -v
Using built-in specs.
Configured with: FreeBSD/i386 system compiler
Thread model: posix
gcc version 3.4.2 [FreeBSD] 20040728


I have a really strange error in the following program.  I feed it the
following input:
$ cat DupWords.txt 
Cat Cat Meow Fur Fur Fur Saur Plesiosaur Mososaur Mososaur Ted Ted

like so:
$ ./COUNT < DupWords.txt             
Cat is repeated 2 times
Fur is repeated 3 times
Mososaur is repeated 2 times
Ted is repeated 2 times

and the program works. However, If I change 'k' to 'j' in:
for(k=j; (j < size) && (Input[k] == Input[j]); k++)
             ^changed from k
            
then I get the following error:
$ gdb COUNT                          
GNU gdb 6.1.1 [FreeBSD]
Copyright 2004 Free Software Foundation, Inc.
GDB is free software, covered by the GNU General Public License, and you are
welcome to change it and/or distribute copies of it under certain conditions.
Type "show copying" to see the conditions.
There is absolutely no warranty for GDB.  Type "show warranty" for details.
This GDB was configured as "i386-marcel-freebsd"...
(gdb) run < DupWords.txt 
Starting program: /usr/home/xfce/code/koenig-moo/ch04/COUNT < DupWords.txt
Cat is repeated 2 times
Fur is repeated 3 times
Mososaur is repeated 2 times

Program received signal SIGBUS, Bus error.
0x280d4dc1 in std::string::compare () from /usr/lib/libstdc++.so.4

If I do a backtrace:
(gdb) bt
#0  0x280d4dc1 in std::string::compare () from /usr/lib/libstdc++.so.4
#1  0x080491a8 in std::operator==<char, std::char_traits<char>,
std::allocator<char> > ([EMAIL PROTECTED], [EMAIL PROTECTED]) at
basic_string.h:2054
#2  0x08048e5e in main () at ex45Main2.cc:25

then I see the error is happening at line 25, namely:
for(k=j; (j < size) && (Input[k] == Input[j]); k++)
             ^changed from k


Below is the program that works (using k < size). My expectation is
that the for loop should bail whether I use (j < size) or (k < size).
Any insights would be appreciated.

//Counts how many times each word occurred.
#include "ex45Func.h"
#include <iostream>

using std::cout;
using std::endl;

int main()
{
        vector<string> Input;
        vector<string>::size_type veclen=0, j, k, size=0;
        readWord(Input);

        //Sort the list
        sort(Input.begin(),Input.end());

        size = Input.size();

        //Now devise a for loop to compare the elements
        j=0, k=0;
        while (j < size)
        {
                //Count ahead to see if there are duplicate words in
the sorted list
                int count=0;
                for(k=j; (k < size) && (Input[k] == Input[j]); k++)
                        count++;
                if (count > 1)
                        cout << Input[j] << " is repeated " << count <<
                                " times" << endl;
                j = count + j;
        }


        return 0;
}


-- 
Kind regards,
Jonathan

_______________________________________________
CWE-LUG mailing list
[email protected]
http://www.cwelug.org/
http://www.cwelug.org/archives/
http://www.cwelug.org/mailinglist/

Reply via email to