stdmap - c++ inseting User Defined class into map -
i've been looking around quite time , think have of pieces code still won't work...
i have map,
map<number, entry> chainlist;
and class number , entry, entry wont worry i'm pretty sure half works correct
in number.h class number { public: //public functions //constructor/destructor number(int len); number(string copy); number(const unsigned char *copy, int len); number(const number& in); ~number(); ....... ....... friend void swap(number& first, number& second); bool operator<(const number& rhs) const; number& operator=(number &rhs); private: //our binary number array unsigned char *num; //hold length used, , maxsize of array int length; };
then,
//in number.cpp number::~number() { delete [] num; } number::number(const number& in) { length = in.length; num = (unsigned char *) calloc(length, sizeof(unsigned char)); (int = 0; < length; i++) { num[i] = in.num[i]; } } bool number::operator<(const number& rhs) const { if (this -> length > rhs.length) { return false; } (int = 0; < -> length; i++) { if (this -> num[i] > rhs.num[i]) { return false; } else if (this -> num[i] < rhs.num[i]) { return true; } } return false; } void swap(number& first, number& second) { // enable adl (not necessary in our case, practice) using std::swap; // swapping members of 2 classes, // 2 classes swapped swap(first.length, second.length); swap(first.num, second.num); } number& number::operator=(number &rhs) { swap (*this, rhs); return *this; }
however when try , insert item map seg fault....
in database.cpp .... chainlist.insert(pair<number, entry>(*(tempentry -> msghash), *tempentry)); .....
where tempentry -> msghash number* - dynamically allocated
what issue be? option have function typecasts , returns c++ string, question std::less_than function work null characters in middle of statement, know works in lexigraphical order first null?
i think problem in operator<()
:
if (this->length > rhs.length) return false; (int = 0; < rhs.length; i++) ....
see? if rhs.length
greater this->length
go on , compare bytes. compare rhs.length
bytes, , might overflow this->num
, this->length
less or equal rhs.length
.
i'm not sure if need specific sorting order, like:
if (this->length > rhs.length) return false; if (this->length < rhs.length) return true; (int = 0; < rhs.length; i++) ....
now, when reach loop sure both arrays same length.
update:
you have important issue in operator=(number &rhs)
. operator should never modify right-hand-side operator. should operator=(const number &rhs)
or operator=(number rhs)
, never non-const reference, yours is.
you trying implement copy-and-swap idiom. got right, proper way is:
number& number::operator=(number rhs) { swap (*this, rhs); return *this; }
update #2:
you allocating array calloc()
freeing delete[]
. undefined behaviour. memory allocated calloc()
freed free()
, , memory allocated new[]
freed delete[]
.
my advice use std::vector<unsigned char>
hold dynamic arrays, , avoid this->length
, delete[]
, etc. std::swap()
on vectors , it's done.
Comments
Post a Comment