A
Arthur J. O'Dwyer
I'm seeing a bug at the moment that I can't track down. It's
part of a moderately large program, but here is a small program
that exhibits the bug on gcc. (The program code follows at the
bottom of the message.)
The symptom is this:
% g++ --version
g++ (GCC) 3.2.1
[...]
% g++ -W -Wall -pedantic test.cpp
% ./a.out
push_back(3402,tian).
push_back(3401,tian).
push_back(3405,wu)Segmentation fault
%
So it appears something is going wrong in multiset::insert.
I'm not very familiar with the STL, so I suppose I'm abusing
multiset in some way, but for the life of me I can't figure
out how, or how to fix it.
(The goal of the TerminalList class is to be a container
of Terminals, but to allow very fast access to the list of
Terminals containing a certain 'syllable' or 'unino'. I'm
using 'lower_bound' and 'upper_bound' to extract a vector
of Terminals with a given syllable from myStrHash; please
tell me if you can see a better way, but this is unrelated
to the segfault bug, which is why I snipped the code that
does all that from the program below.)
Thanks much,
-Arthur
==code begins==
#include <cstdio>
#include <cctype>
#include <vector>
#include <set>
#include <string>
using namespace std;
struct Terminal
{
int unino;
string syllable;
int d;
Terminal(int u, const string& y, int c):
unino(u), syllable(y), d(c)
{}
};
struct myStrPred {
bool operator() (Terminal* a, Terminal* b)
{ return (a->syllable < b->syllable); }
};
struct TerminalList
{
void push_back(const Terminal& t);
typedef multiset<Terminal*, myStrPred> StrHashT;
vector<Terminal> myVector;
StrHashT myStrHash;
};
void TerminalList:
ush_back(const Terminal& t)
{
myVector.push_back(t);
Terminal* pt = &myVector.back();
printf("push_back(%04x,%s)",
pt->unino, pt->syllable.c_str());
fflush(stdout);
myStrHash.insert(pt);
printf(".\n");
}
int main()
{
TerminalList characters;
characters.push_back(Terminal(0x3402, "tian", 1));
characters.push_back(Terminal(0x3401, "tian", 1));
characters.push_back(Terminal(0x3405, "wu", 3));
return 0;
}
part of a moderately large program, but here is a small program
that exhibits the bug on gcc. (The program code follows at the
bottom of the message.)
The symptom is this:
% g++ --version
g++ (GCC) 3.2.1
[...]
% g++ -W -Wall -pedantic test.cpp
% ./a.out
push_back(3402,tian).
push_back(3401,tian).
push_back(3405,wu)Segmentation fault
%
So it appears something is going wrong in multiset::insert.
I'm not very familiar with the STL, so I suppose I'm abusing
multiset in some way, but for the life of me I can't figure
out how, or how to fix it.
(The goal of the TerminalList class is to be a container
of Terminals, but to allow very fast access to the list of
Terminals containing a certain 'syllable' or 'unino'. I'm
using 'lower_bound' and 'upper_bound' to extract a vector
of Terminals with a given syllable from myStrHash; please
tell me if you can see a better way, but this is unrelated
to the segfault bug, which is why I snipped the code that
does all that from the program below.)
Thanks much,
-Arthur
==code begins==
#include <cstdio>
#include <cctype>
#include <vector>
#include <set>
#include <string>
using namespace std;
struct Terminal
{
int unino;
string syllable;
int d;
Terminal(int u, const string& y, int c):
unino(u), syllable(y), d(c)
{}
};
struct myStrPred {
bool operator() (Terminal* a, Terminal* b)
{ return (a->syllable < b->syllable); }
};
struct TerminalList
{
void push_back(const Terminal& t);
typedef multiset<Terminal*, myStrPred> StrHashT;
vector<Terminal> myVector;
StrHashT myStrHash;
};
void TerminalList:
{
myVector.push_back(t);
Terminal* pt = &myVector.back();
printf("push_back(%04x,%s)",
pt->unino, pt->syllable.c_str());
fflush(stdout);
myStrHash.insert(pt);
printf(".\n");
}
int main()
{
TerminalList characters;
characters.push_back(Terminal(0x3402, "tian", 1));
characters.push_back(Terminal(0x3401, "tian", 1));
characters.push_back(Terminal(0x3405, "wu", 3));
return 0;
}