Segmentation fault with accessor function in C++ - c++

I am working on a homework assignment for a C++ class, and having some difficulties. My program is segfaulting due to "use of uninitialised value of size 8" and "invalid read of size 8" in item::name() which is called by item::operator=.
#ifndef ITEM_H
#define ITEM_H
const double WEIGHT_DEFAULT = 1.0;
const int ITEM_NAME_LENGTH = 30;
class item {
public:
// Constructors and deconstructor
item();
item(char* nme, double weight);
virtual ~item();
// Copy constructor
item(const item& itm);
// Overload assignment operator
const item& operator=(const item& itm);
// Overload inequality operator
bool operator!=(const item&) const;
// Mutators and accessors
const char* name(void) const;
double weight(void) const;
void weight(double wght);
void name(char* nme);
protected:
private:
char* m_name;
double m_weight;
};
#endif // ITEM_H
Here is the portion of the implementation that I think is having issues:
...
const item& item::operator=(const item& itm)
{
if (strcmp(this->name(), itm.name()) != 0)
{
this->weight(itm.weight());
strcpy(m_name, itm.name());
}
return *this;
}
const char* item::name(void) const {
return m_name;
}
...
As you can see, part of the assignment is to use c-strings, thus I'm stuck with the messy details. My understanding of pointers and classes is that when working with both, we need to implement destructors, copy constructors, and overload the assignment operator. I've done all of the above. I looked at some info on the copy-and-swap idiom here on Stack Overflow, and tried implementing that, but couldn't get that to work with my code either.
My brain is getting frazzled trying to puzzle out where I went wrong. Could someone please tell me what I'm missing?
Thanks!
EDIT
Here are the constructors, destructor, and such:
item::item() {
//ctor
m_name = new char[ITEM_NAME_LENGTH];
strncpy(m_name, " ", ITEM_NAME_LENGTH - 1);
this->weight(WEIGHT_DEFAULT);
return;
}
item::item(char* nme, double wght) {
m_name = new char[ITEM_NAME_LENGTH];
strncpy(m_name, nme, ITEM_NAME_LENGTH - 1);
// We want to check for a rational value being
// passed in to weight. In theory, an item has
// to have *some* kind of weight, so we check
// for a weight of 0. If a weight of 0 has been passed in,
// we instead initialize the item's weight to the
// default weight (set in the header file).
if (wght > 0.0) {
this->weight(wght);
} else {
this->weight(WEIGHT_DEFAULT);
}
return;
}
item::~item() {
//dtor
// TODO: We need to clean up any variables here.
delete[] m_name;
}
item::item(const item& itm) {
// copy ctor
this->weight(itm.weight());
strncpy(m_name, itm.name(), ITEM_NAME_LENGTH - 1);
}
const item& item::operator=(const item& itm)
{
if (strcmp(this->name(), itm.name()) != 0)
{
this->weight(itm.weight());
strcpy(m_name, itm.name());
}
return *this;
}
bool item::operator!=(const item& itm) const
{
return (strcmp(m_name, itm.name()) != 0);
}
EDIT 2
Now that I've stopped trying to dynamically allocate the name c-string, I'm getting memory errors on the weight function...
double item::weight(void) const {
return m_weight;
}
void item::weight(double wght)
{
m_weight = wght;
}

First off, you don't need to explicitly return; from void statements.
Second:
When creating a copy constructor, you need to allocate the space that will be used to hold the memory of that constructor, the same way you do for the regular constructor.
Try this in your copy constructor:
m_name = new char[ITEM_NAME_LENGTH];
Edit: for completeness this is what my "appears to be working" code is:
#include <stdio.h>
#include <string.h>
const double WEIGHT_DEFAULT = 1.0;
const int ITEM_NAME_LENGTH = 30;
class item {
public:
// Constructors and deconstructor
item();
item(char* nme, double weight);
virtual ~item();
// Copy constructor
item(const item& itm);
// Overload assignment operator
const item& operator=(const item& itm);
// Overload inequality operator
bool operator!=(const item&) const;
// Mutators and accessors
const char* name(void) const;
double weight(void) const;
void weight(double wght);
void name(char* nme);
protected:
private:
char* m_name;
double m_weight;
};
const item& item::operator=(const item& itm)
{
if (strcmp(this->name(), itm.name()) != 0)
{
m_name = new char[ITEM_NAME_LENGTH];
this->weight(itm.weight());
strcpy(m_name, itm.name());
}
return *this;
}
const char* item::name(void) const {
return m_name;
}
double item::weight(void) const {
return m_weight;
}
void item::weight(double wght) {
m_weight = wght;
}
item::item() {
//ctor
m_name = new char[ITEM_NAME_LENGTH];
strncpy(m_name, " ", ITEM_NAME_LENGTH - 1);
this->weight(WEIGHT_DEFAULT);
return;
}
item::item(char* nme, double wght) {
m_name = new char[ITEM_NAME_LENGTH];
strncpy(m_name, nme, ITEM_NAME_LENGTH - 1);
// We want to check for a rational value being
// passed in to weight. In theory, an item has
// to have *some* kind of weight, so we check
// for a weight of 0. If a weight of 0 has been passed in,
// we instead initialize the item's weight to the
// default weight (set in the header file).
if (wght > 0.0) {
this->weight(wght);
} else {
this->weight(WEIGHT_DEFAULT);
}
return;
}
item::~item() {
//dtor
// TODO: We need to clean up any variables here.
delete[] m_name;
}
item::item(const item& itm) {
// copy ctor
this->weight(itm.weight());
m_name = new char[ITEM_NAME_LENGTH];
strncpy(m_name, itm.name(), ITEM_NAME_LENGTH - 1);
}
bool item::operator!=(const item& itm) const
{
return (strcmp(m_name, itm.name()) != 0);
}
int main(int argc, char* argv[])
{
item i("test",2.0);
item b = i;
item c;
item d = c;
return 0;
}

I guess using std::string is out as part of the exercise, right? Otherwise, that's the way to go for string handling. Another thing you could try is to avoid the dynamic allocation of the string. Instead, just use a char m_name[ITEM_NAME_LENGTH];. BTW: Which length is that? If it's the maximum number of characters in the name, you need a +1 for the terminating NUL.
That said, one more thing: In assignment operators, you compare if(this != &other), i.e. you compare the address of the two objects. The point is to avoid self-assignment, which is error-prone. Your assignment checks if the names are different and only then copies the names and the weight. What if only the weight was different?

Related

Inserting new Object to Repository throws error

I've created an object and a Repository for it.
When I try inserting the object into the Repository (with the insert function i've created) I get compile error.
The Class I'm trying to insert into Repository
class Payment{
private:
int day;
int amount;
char *type;
public:
Payment();
Payment(int day, int amount, char *type);
Payment(const Payment &p);
~Payment();
//getters
int getDay()const;
int getAmount()const;
char* getType()const;
//setters
void setDay(int day);
void setAmount(int amount);
void setType(char* type);
//operator
Payment& operator=(const Payment& other);
friend ostream& operator<<(ostream &os,const Payment &obj);
};
//copy constructor
Payment::Payment(const Payment & p){
this->day = p.day;
this->amount = p.amount;
if(this->type!=NULL)
delete[] this->type;
this->type = new char[strlen(p.type)+1];
strcpy_s(this->type, strlen(p.type) + 1, p.type);
}
//assignment operator
Payment& Payment::operator=(const Payment &other) {
this->day = other.day;
this->amount = other.amount;
this->type = new char[strlen(other.type) + 1];
strcpy_s(this->type, strlen(other.type) + 1, other.type);
return *this;
}
//destructor
Payment::~Payment(){
this->day = 0;
this->amount = 0;
if (this->type != NULL) {
delete[]this -> type;
this->type = NULL;
}
}
//Repository header
class Repository{
private:
vector<Payment> list;
public:
Repository();
int getLength();
void insert(const Payment& obj);
void remove(int position);
};
//Repository cpp
Repository::Repository(){
this->list.reserve(10);
}
//return the size of the list
int Repository::getLength() {
return this->list.size();
}
//add payment to list
void Repository::insert(const Payment &obj) {
this->list.emplace_back(obj);
}
//remove payment from list
void Repository::remove(int position) {
this->list.erase(this->list.begin() + position);
}
In main function I have
char c[] = "some characters";
Payment pay = Payment(7,9,c);
Repository rep = Repository();
rep.insert(pay);
When I run the program I get the error "
Expression: _CrtlsValidHeapPointer(block) "
Since std::vector will make copies, a std::vector<Payment> requires that Payment has correct copy semantics. Your copy constructor and assignment operator are not implemented correctly. The assignment operator causes memory leaks, since you failed to delete [] the existing memory.
The easiest solution is to drop using the char *type; member and simply use std::string type;. Then the Payment class would have the correct copy semantics automatically.
Given that, the corrections to your Payment class is below:
#include <algorithm>
//...
Payment::Payment() : day(0), amount(0), type(nullptr) {}
Payment::Payment(const Payment & p) : day(p.day), amount(p.amount), type(nullptr)
{
if ( p.type )
{
type = new char[strlen(p.type) + 1];
strcpy_s(this->type, strlen(p.type) + 1, p.type);
}
}
// Use the copy/swap idiom
Payment& Payment::operator=(const Payment &other)
{
Payment temp(other); // make a temporary copy
// swap out contents of temporary with this object
std::swap(temp.day, day);
std::swap(temp.amount, amount);
std::swap(temp.type, type);
return *this;
} // when this brace has been reached, the temp copy dies off with the old data
Payment::~Payment()
{
delete [] type;
}
The above uses the copy/swap idiom within the assignment operator. The copy constructor uses a member initialization list.
The destructor need not check for a null pointer, since deleting a nullptr is perfectly valid.
Now adding to a std::vector is working well, without any runtime error (using the code #PaulMcKenzie posted). I've also found an example of code that's working, where only the assignment operator is kinda different.
Converted to my code would be (and it's working either):
Payment& Payment::operator=(const Payment &other) {
if (this != &other) {
this->setDay(other.day);
this->setAmount(other.amount);
this->setType(other.type);
}
return *this;
}
Thanks for the help! Now it's working perfectly! I didn't get to study very much from <algorithm> library, so I'll have to take a closer look. Wish you the best luck! ^_^

C++ initializing const

I need to be able to initialize a const member inside the constructor, which counts up every time I create a new object. I was shown in school how its suppose to work, but I'm getting errors all the time. It's something to do with the copy constructor.
Here's the code and the compiler errors:
class kunde {
public:
kunde(string name, int alter);
kunde(const kunde& orig);
~kunde();
int GetAlter() const;
string GetName() const;
const int GetKnr() const;
private:
string name;
int alter;
const int knr;
static int cnt;
static int MaxKnr;
};
int kunde::cnt = 0;
int kunde::MaxKnr = 1000;
kunde::kunde(string name, int alter):knr(MaxKnr++) {
this->name = name;
this->alter = alter;
}
kunde::kunde(const kunde& orig):knr(MaxKnr++){
this->name = orig.name;
this->alter = orig.alter;
}
kunde::~kunde() {
}
int kunde::GetAlter() const {
return alter;
}
string kunde::GetName() const {
return name;
}
const int kunde::GetKnr() const {
return knr;
}
main.cpp: In function 'int main(int, char**)':
main.cpp:35:15: error: use of deleted function 'kunde& kunde::operator=(const kunde&)'
v[0] = v[1];
^
In file included from main.cpp:17:0:
kunde.h:19:7: note: 'kunde& kunde::operator=(const kunde&)' is implicitly deleted because the default definition would be ill-formed:
class kunde {
^~~~~
kunde.h:19:7: error: non-static const member 'const int kunde::knr', can't use default assignment operator
knr is suppose to be an account number. each time you create an object it creates a new const account number which stays.
As stated in comments, since knr is const, the compiler cannot generate a default copy-assignment operator= for the class, which is exactly what the compiler is complaining about for the v[0] = v[1]; statement:
note: 'kunde& kunde::operator=(const kunde&)' is implicitly deleted because the default definition would be ill-formed
const members cannot be re-assigned once initialized, thus cannot be copied.
The elements in a vector must be CopyAssignable and CopyConstructible (at least until C++11), but your class does not have a viable copy-assignment operator=, so it is not CopyAssignable (and it is not MoveAssignable in C++11 either, since a viable move-assignment operator= can't be generated, either).
The solution is to implement a copy-assignment operator= (and optionally a move-assignment operator=) that ignores knr, eg:
class kunde {
public:
kunde(string name, int alter);
kunde(const kunde& orig);
kunde(kunde&& orig);
...
kunde& operator=(const kunde& rhs);
kunde& operator=(kunde&& rhs);
...
private:
string name;
int alter;
const int knr;
...
};
kunde::kunde(string name, int alter)
: knr(MaxKnr++), name(name), alter(alter)
{
}
kunde::kunde(const kunde& orig)
: knr(MaxKnr++), name(orig.name), alter(orig.alter)
{
}
kunde::kunde(kunde&& orig)
: knr(MaxKnr++), name(std::move(orig.name)), alter(orig.alter)
{
}
kunde& kunde::operator=(const kunde& rhs)
{
if (&rhs != this)
{
name = rhs.name;
alter = rhs.alter;
// CAN'T BE DONE, SO IGNORE IT
// knr = rhs.knr;
}
return *this;
}
kunde& kunde::operator=(kunde&& rhs)
{
name = std::move(rhs.name);
alter = rhs.alter;
// CAN'T BE DONE, SO IGNORE IT
// knr = rhs.knr;
return *this;
}

c++ destructor called before cout

#include <iostream>
#include <string.h>
using namespace std;
class Location
{
double lat, lon;
char *emi;
public:
Location(int =0, int=0, const char* =NULL);
~Location();
Location (const Location&);
void print () const;
friend ostream& operator<< (ostream&, const Location &);
void operator! ();
protected: ;
private:
};
Location::Location(int lat, int lon, const char *emi)
{
this->lat=lat;
this->lon=lon;
if (emi!=NULL)
{
this->emi=new char [strlen (emi)+1];
strcpy (this->emi, emi);
}
}
Location::~Location()
{
if (emi!=NULL)
delete []emi;
}
Location::Location(const Location &l)
{
lat=l.lat;
lon=l.lon;
if (l.emi!=NULL)
{
emi=new char [strlen (l.emi)+1];
strcpy (emi, l.emi);
}
}
void Location::operator! ()
{
if (!(strcmp(this->emi, "north")))
strcpy (this->emi, "south");
else strcpy (this->emi, "north");
}
void Location::print() const
{
cout<<this->emi<<endl;
cout<<this->lon<<endl;
cout<<this->lat<<endl;
cout<<endl;
}
class Adress
{
char *des;
Location l1;
char *country;
public:
Adress(char *,const Location &, char *);
virtual ~Adress();
friend ostream& operator<< (ostream&, const Adress &);
protected:
private:
};
Adress::Adress(char *des,const Location &l1, char *country)
{
if (des!=NULL)
{
this->des=new char [strlen (des)+1];
strcpy (this->des, des);
}
if (country!=NULL)
{
this->country=new char [strlen (country)+1];
strcpy (this->country, country);
}
this->l1=l1;
}
Adress::~Adress()
{
if (country!=NULL)
delete []country;
if (des!=NULL)
delete []des;
}
ostream& operator<< (ostream &os, const Adress& a){
os <<"Descrition: " << a.des<<endl;
os<<"Country: "<<a.country<<endl;
a.l1.print();
return os;
}
int main ()
{
Adress a1 ("dsad", Location (323, 34, "fdsf"), "fsdf");
cout<<a1;
}
The problem is that when I create an Adress object and display it, all the fields are correct , but the "emi" which is messed up, showing a random character. I think the destructor is called before I display it. If I remove the Location destructor it works. How should I resolve it? I'm sorry for my mistakes but I am a newbie.
First of all, it would be better to use std::string rather than char* but I will explain your problem for the education goal.
You must ensure after constructing an object, all of its member variables are initialized. In the case of Location class for example; you did not initialize the emi member variable if the third argument of the constructor is nullptr. so I changed it a little:
Location::Location(int _lat, int _lon, const char* _emi)
: lat(_lat)
, lon(_lon)
, emi(nullptr)
{
if (_emi != nullptr)
{
emi = new char[strlen(_emi) + 1];
strcpy(emi, _emi);
}
}
Next, you have a raw pointer in your class and you can not simply copy or assign it. You have to implement the assignment operator as well as the copy constructor.
Location& Location::operator=(const Location& other)
{
if (this != &other)
{
lat = other.lat;
lon = other.lon;
if (emi) delete[] emi;
emi = new char[strlen(other.emi) + 1];
strcpy(emi, other.emi);
}
return *this;
}

No viable overloaded '=' when assigning an object to another object?

I've tried looking for a solution but the answers in those questions are very specific. Here's what I have.
The header Code.
class InventoryItem {
public:
InventoryItem(char *desc = 0, double c = 0, int u = 0);
~InventoryItem();
char *getDescription() const;
double getCost() const;
int getUnits() const;
void operator= (const InventoryItem &right);
int setUnits;
private:
char *description;
double cost;
int units;
};
The .cpp file associated with the header file.
InventoryItem::InventoryItem(char *desc, double c, int u) {
description = desc;
cost = c;
units = u;
}
InventoryItem::~InventoryItem() {}
void InventoryItem::operator= (const InventoryItem &right) {
units = right.getUnits();
cost = right.getCost();
description = right.getDescription();
}
The CashRegister class.
class CashRegister {
public:
void getItemToPurchase(InventoryItem) const;
private:
InventoryItem item;
}
The error occurs in this code, which is the .cpp file to the CashRegister function.
void CashRegister::getItemToPurchase(InventoryItem item1) const {
item = item1;
}
It says "No viable operator"=" even though item and item1 are both objects of the same class.
I've compared my code to the one in the book but it still doesn't work.
The problem is with const member function in CashRegister. In CashRegister::getItemOnPurchase(), item is a const member. Hence
item = item1;
is not a viable function.
I can think of couple of ways to solve the problem.
Return the object
InventoryItem CashRegister::getItemToPurchase() const {
return item;
}
Return the object as an output argument.
void CashRegister::getItemToPurchase(InventoryItem& item1) const {
item1 = item;
}
Also, it is more idiomatic to return a reference to the object in the operator= function.
InventoryItem& operator= (const InventoryItem &right);

Can this Assignment and Copy cnstr be organized better

I am trying to improve my understanding of the copy constructor and copy assign. operator
Here is a simple class that I came up with
class Bar
{
char* name;
int zip;
std::string address;
public:
Bar(const Bar& that)
{
//Copy constructor
size_t len = strlen(that.name + 1);
name = new char[len];
strcpy(name, that.name);
//Copy the zip
this->zip = that.zip;
//Copy the address
this->address = that.address;
}
Bar& operator=(const Bar& that)
{
//Assignment operator
if(this != &that)
{
//Copy the name
size_t len = strlen(that.name + 1);
name = new char[len];
strcpy(name, that.name);
//Copy the zip
this->zip = that.zip;
//Copy the address
this->address = that.address;
}
return *this;
}
};
My question is since the code in the copy constructor and copy assignment operator are the same does it make more sense to unify that into a deep copy method so that incase I add another member variable I dont have to add another line to the copy cnstr and copy assign. section ? Any suggestions ?
The "normal" way of doing things where you manage your own resources is a little different:
char* cppstrdup(const char*s, int len=0);
class Bar
{
char* name;
int zip;
std::string address;
public:
Bar(const Bar& that)
:name(nullptr),
zip(that->zip),
address(that->address)
{
name = cppstrdup(that.name); //done here for exception safety reasons
}
Bar(Bar&& that) //if you have C++11 then you'll want this too
:name(nullptr)
{
swap(*this,that);
}
~Bar() //you forgot the destructor
{
delete [] name;
}
Bar& operator=(Bar that) //this is called copy and swap.
{ //"that" is a copy (notice, no & above), and we simply swap
swap(*this,that);
return *this;
}
friend void swap(Bar& left, Bar& right)
{
using std::swap;
swap(left.name, right.name);
swap(left.zip, right.zip);
swap(left.address, right.address);
}
};
//uses new instead of malloc
inline char* cppstrdup(const char* s, int len)
{
if (s==0) return nullptr;
if (len==0) len = strlen(s);
char* r = new char[len+1];
strncpy(r, len+1, s);
r[len] = 0;
return r;
}
The benefits of this pattern is that it is much easier to get exception safety, often with the strong exception guarantee.
Of course, even more normal is to not use char* name, and obey the "Rule of Zero" isntead. In which case, it becomes VERY different:
class Bar
{
std::string name;
int zip;
std::string address;
public:
Bar() = default; //well, that's easy
};
Check COPY & SWAP idiom. In short - your logic goes into copy constructor and swap method and your assignment operator looks like:
Bar& operator=(const Bar& that)
{
Bar temp(that);
swap(*this, temp);
return *this;
}