Talk About Network

Google


Register and Login
Nick
Password
Register create new account Sign up is FREE and you can post replies, new topics, bookmark posts and more!
Recover lost password


Programming > C++ Moderated > More problems w...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 1 of 24 Topic 9766 of 9909
Post > Topic >>

More problems with sort

by Juanjo <juanjose.garciaripoll@[EMAIL PROTECTED] > Jul 3, 2008 at 03:46 PM

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! ]
 




 24 Posts in Topic:
More problems with sort
Juanjo <juanjose.garci  2008-07-03 15:46:25 
Re: More problems with sort
Hyman Rosen <hyrosen@[  2008-07-04 19:27:46 
Re: More problems with sort
Kai-Uwe Bux <jkherciue  2008-07-04 19:30:42 
Re: More problems with sort
Frank Birbacher <blood  2008-07-05 03:59:08 
Re: More problems with sort
Alex Shulgin <alex.shu  2008-07-05 04:00:59 
Re: More problems with sort
Oncaphillis <oncaphill  2008-07-05 04:58:02 
Re: More problems with sort
Martin Bonner <martinf  2008-07-05 07:24:00 
Re: More problems with sort
Lance Diduck <lancedid  2008-07-05 07:35:12 
Re: More problems with sort
j_richter@[EMAIL PROTECTE  2008-07-05 07:33:25 
Re: More problems with sort
Bart van Ingen Schenau &l  2008-07-05 08:58:22 
Re: More problems with sort
Thomas Maeder <maeder@  2008-07-05 08:58:14 
Re: More problems with sort
Juanjo <juanjose.garci  2008-07-05 16:53:45 
Re: More problems with sort
Hyman Rosen <hyrosen@[  2008-07-06 05:55:24 
Re: More problems with sort
Oncaphillis <oncaphill  2008-07-06 06:02:53 
Re: More problems with sort
Mathias Gaunard <loufo  2008-07-06 16:35:28 
Re: More problems with sort
Oncaphillis <oncaphill  2008-07-07 06:18:06 
Re: More problems with sort
Triple-DES <DenPlettfr  2008-07-07 06:28:04 
Re: More problems with sort
Mathias Gaunard <loufo  2008-07-07 17:36:03 
Re: More problems with sort
Frank Birbacher <blood  2008-07-08 05:27:49 
Re: More problems with sort
Thomas Maeder <maeder@  2008-07-08 15:24:42 
Re: More problems with sort
red floyd <redfloyd@[E  2008-07-08 15:25:48 
Re: More problems with sort
David Abrahams <dave@[  2008-07-09 01:28:10 
Re: More problems with sort
Martin Bonner <martinf  2008-07-09 13:28:05 
Re: More problems with sort
Mathias Gaunard <loufo  2008-07-09 14:58:07 

Post A Reply:
  Go here to Signup

AddThis Feed Button


About - Advertising - Contact - Frequently Asked Questions - Privacy Policy - Terms of Use - Signup

Contact
tan12V112 Wed Aug 20 4:36:08 CDT 2008.