Sorry for posting a program, but this is a sort of minimal test for a
problem that is puzzling me all day.
More precisely, the problem is that std::sort() accesses the element
pointed to by the end() of a vector. I think my comparison of clauses
operator<() is indeed a strict order with the appropriate transitivity
properties, but that should not affect the behavior of std::sort with
respect to which elements it accesses, am I wrong?
I have tested this code with gcc 4.1, 4.2, IBM's xlc, and others, and
they all broke at the same point. The size of the collection
influenced whether the abort() test was called or not!
I would appreciate some help.
Juanjo
/* -*- mode: c++; c-basic-offset: 4 -*- */
#include <stdlib.h>
#include <iostream>
#include <algorithm>
#include <vector>
class Clause {
unsigned int size;
unsigned int bits;
std::vector<unsigned int> bit_indices;
public:
typedef std::vector<unsigned int> uivector;
Clause();
Clause(unsigned int a_size, unsigned int n_bits);
Clause(const Clause &c);
unsigned int bit_index(unsigned int position) const { return
bit_indices.at(position); }
unsigned int clause_size() const { return size; }
unsigned int clause_bits() const { return bits; }
bool operator<(const Clause &other) const;
};
Clause::Clause()
: size(0), bits(0), bit_indices()
{
/* Uninitialized elements */
}
Clause::Clause(unsigned int a_size, unsigned int n_bits)
: size(a_size), bits(n_bits), bit_indices(size, 0)
{
/* Initialized with fixed values */
size_t i = 0;
for (uivector::iterator a = bit_indices.begin(); a !=
bit_indices.end(); a++) {
*a = i++;
}
}
Clause::Clause(const Clause &s)
: size(s.size), bits(s.bits), bit_indices(s.bit_indices)
{
}
bool
Clause::operator<(const Clause &other) const
{
if (clause_size() != other.clause_size()) {
/* We are comparing with an uninitialized element or with an element
of different size */
/* When linked with a buggy c++ library it breaks here */
abort();
}
for (uivector::const_iterator a = bit_indices.begin(), b =
other.bit_indices.begin();
a != bit_indices.end();
a++) {
if (*a > *b)
return true;
if (*a < *b)
return false;
}
return false;
}
int
main(int argc, char **argv)
{
unsigned int n = 63;
std::vector<Clause> c(n, Clause(3, 8));
std::sort(c.begin(), c.end());
std::cout << c.size();
exit(0);
}
--
[ See http://www.gotw.ca/resources/clcm.htm
for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


|