In my project, I have a structure Transaction
with the below interface:
/**
* Transaction interface
*/
struct Transaction
{
Transaction () : signature(new char[20]), otherAcc(new char[20]), amount(0)
{
}
Transaction (const Transaction &other)
: signature(new char[20]),
otherAcc(new char[20])
{
strcpy(signature, other.signature);
strcpy(otherAcc, other.otherAcc);
}
Transaction& operator = (const Transaction &other)
{
if (&other != this)
{
// out with the old
delete[] signature;
delete[] otherAcc;
// in with the new
signature = new char[20];
otherAcc = new char[20];
strcpy(signature, other.signature);
strcpy(otherAcc, other.otherAcc);
}
return *this;
}
~Transaction ()
{
delete[] signature;
delete[] otherAcc;
}
char *signature;
char *otherAcc;
int amount;
};
I initialize an (admittedly sloppy) implementation of a linked list with pointers to transactions on different occasions:
/**
* linked list interface
*/
template <class T>
struct node
{
/**
* constructs a node in of a linked list with the value provided
* and a NULL next node
*/
node (T initial_value) : value(initial_value), next(NULL), refs(1) {}
/**
* frees the memory
* allocated in the linked list
*/
void clear ()
{
clearList();
}
/**
* frees all nodes as well as encapsulated values in the
* linked list
*/
void clearWithValue ()
{
if (this)
{
next->clearWithValue();
if (value)
delete value;
delete this;
}
}
T value;
node<T> *next;
unsigned refs;
bool isFirst;
private:
/**
* clears the rest of the list starting from the node at
* which lear list was called
*/
void clearList ()
{
if (this)
{
next->clearList();
delete this;
}
}
};
The initialization would look something like this, where otherAcc and signature are const char * passed in by a function:
Transaction *new_trans = new Transaction;
strcpy(new_trans->otherAcc, otherAcc);
strcpy(new_trans->signature, signature);
new_trans->amount = amount;
data->transactions = new node<Transaction*>(new_trans);
I am getting a segmentation fault, which I have determined to be caused by a bad pointer allocation in one of the nodes in the linked list. One of the nodes has value = { 0x343536373839 }
, which cannot be accessed because it is out of bounds, so the program segfaults. Is this problem something obvious related to memory errors I'm not seeing? I'm pretty new to this stuff and debugging these memory errors can be a real pain, so general debugging tips are also welcome.
Edit: After I run this and it segfaults in gdb:
(gdb) bt
#3 0x00000000004015a0 in operator<< (os=..., acc=...) at tester.cpp:483
which is in my program
os << walker->value->otherAcc;
where walker is of type node *
Edit: I added the copy constructor and assignment operator as is now reflected in the Transaction interface, but it did not solve the segfault problem.