P
pieter.eendebak
Dear newsgroup,
I am trying to run the program below. When I use a STL vector the
program runs fine, but when using a deque I get a segmentation fault
at the line
sort ( hlist.begin(), hlist.end() );
I do not have the same problem when I use deque<int> instead of my own
class deque<array_link>. I checked the program with Valgrind, and
these are warnings about uninitialized values, but these are generated
within the C++ STL code.
Can sort be used with the deque iterations begin() and end()?
Have I defined all necessary assignment and copy constructor functions
for my class?
With kind regards,
Pieter Eendebak
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <vector>
#include <deque>
#include <algorithm>
using namespace std;
struct array_link
{
int size;
int index;
int * array;
array_link();
~array_link();
array_link(const array_link &);
array_link(int size, int index);
array_link &operator=(const array_link &rhs);
int operator==(const array_link &rhs) const;
int operator<(const array_link &rhs) const;
};
array_link::array_link(): size(-1), index(-1), array(0)
{
//printf("array_link::default constructor\n");
}
array_link::~array_link()
{
//printf("array_link:destructor() %d\n", index);
}
array_link::array_link(int s, int index_): size(s), index(index_)
{
this->array = new int;
for(int i=0;i<size;i++)
array=i*i;
}
array_link::array_link(const array_link &A): size(A.size), index
(A.index), array(A.array)
{
//printf("array_link::default copy constructor: count %d\n", count);
}
/**
* @brief Assignment operator
*/
array_link & array_link:perator=(const array_link& rhs)
{
this->size = rhs.size;
this->index = rhs.index;
this->array = rhs.array;
return *this;
}
int array_link:perator<(const array_link& b) const
{
if (this->size != b.size) {
printf("array_link:perator< comparing arrays (%d %d) with
different sizes: (%d) (%d)!\n", this->index, b.index, this->size,
b.size);
return 0;
}
return memcmp(this->array, b.array, sizeof(int)*this->size) <= 0;
}
// Comparision operator for the array link
int array_link:perator==(const array_link& b) const
{
if (this->size != b.size) {
printf("array_link:perator== comparing arrays (%d %d) with
different sizes: (%d) (%d)!\n", this->index, b.index, this->size,
b.size);
return 0;
}
return memcmp(this->array, b.array, sizeof(int)*this->size)==0;
}
int main ( int argc, char* argv[] ) {
deque<array_link> hlist;
//vector<array_link> hlist;
const int defaultsize=4;
for ( int hcol=0; hcol<120; hcol++ ) {
cout << "loop " << hcol << endl;
array_link link ( defaultsize, hcol+100 );
hlist.push_back ( link ); hlist.push_back ( link );
for ( unsigned int xx=0;xx<hlist.size();xx++ ) {
printf ( "hlist[%d]: index %d, size %d \n", xx, hlist[xx].index,
hlist[xx].size );
}
printf ( "sorting: %d values\n", hlist.end()-hlist.begin() );
sort ( hlist.begin(), hlist.end() );
}
}
I am trying to run the program below. When I use a STL vector the
program runs fine, but when using a deque I get a segmentation fault
at the line
sort ( hlist.begin(), hlist.end() );
I do not have the same problem when I use deque<int> instead of my own
class deque<array_link>. I checked the program with Valgrind, and
these are warnings about uninitialized values, but these are generated
within the C++ STL code.
Can sort be used with the deque iterations begin() and end()?
Have I defined all necessary assignment and copy constructor functions
for my class?
With kind regards,
Pieter Eendebak
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <vector>
#include <deque>
#include <algorithm>
using namespace std;
struct array_link
{
int size;
int index;
int * array;
array_link();
~array_link();
array_link(const array_link &);
array_link(int size, int index);
array_link &operator=(const array_link &rhs);
int operator==(const array_link &rhs) const;
int operator<(const array_link &rhs) const;
};
array_link::array_link(): size(-1), index(-1), array(0)
{
//printf("array_link::default constructor\n");
}
array_link::~array_link()
{
//printf("array_link:destructor() %d\n", index);
}
array_link::array_link(int s, int index_): size(s), index(index_)
{
this->array = new int
for(int i=0;i<size;i++)
array=i*i;
}
array_link::array_link(const array_link &A): size(A.size), index
(A.index), array(A.array)
{
//printf("array_link::default copy constructor: count %d\n", count);
}
/**
* @brief Assignment operator
*/
array_link & array_link:perator=(const array_link& rhs)
{
this->size = rhs.size;
this->index = rhs.index;
this->array = rhs.array;
return *this;
}
int array_link:perator<(const array_link& b) const
{
if (this->size != b.size) {
printf("array_link:perator< comparing arrays (%d %d) with
different sizes: (%d) (%d)!\n", this->index, b.index, this->size,
b.size);
return 0;
}
return memcmp(this->array, b.array, sizeof(int)*this->size) <= 0;
}
// Comparision operator for the array link
int array_link:perator==(const array_link& b) const
{
if (this->size != b.size) {
printf("array_link:perator== comparing arrays (%d %d) with
different sizes: (%d) (%d)!\n", this->index, b.index, this->size,
b.size);
return 0;
}
return memcmp(this->array, b.array, sizeof(int)*this->size)==0;
}
int main ( int argc, char* argv[] ) {
deque<array_link> hlist;
//vector<array_link> hlist;
const int defaultsize=4;
for ( int hcol=0; hcol<120; hcol++ ) {
cout << "loop " << hcol << endl;
array_link link ( defaultsize, hcol+100 );
hlist.push_back ( link ); hlist.push_back ( link );
for ( unsigned int xx=0;xx<hlist.size();xx++ ) {
printf ( "hlist[%d]: index %d, size %d \n", xx, hlist[xx].index,
hlist[xx].size );
}
printf ( "sorting: %d values\n", hlist.end()-hlist.begin() );
sort ( hlist.begin(), hlist.end() );
}
}