Reputation: 589
I have a doubly threaded linked list made up of winery
elements, winery
being a class with its own data fields. The problem that I appear to be having is with the cstring data fields in my winery element. Now I was receiving a "double free or corruption (out): 0x0000000000402940" error in Linux, so I disabled my winery element destructor:
winery::~winery()
{
delete[] name;
delete[] location;
}
and that fixed the error, in Linux. Although, I can't see anything wrong with my winery
deconstructor. My Linux environment will just output the values of name *
and location *
, while Windows Visual Studio outputs the values of name *
and location *
followed by: ²²²²½½½½½½½½ϵ▮ϵ▮ϵ▮.
In order to keep this post short, I'm just going to include the functions responsible for taking in data for name *
and the functions for printing name *
. Here's a dropbox link to a zip of my entire project if you really want to run it.
So, for example, I'm not actually making this call:
char name[] = "Winery\0\0\0\0\0\0";
winery item;
item.setName(name);
Just assume that name[]
is a character array with more memory slots than it needs.
Here's my setName()
function from my winery.cpp file:
void winery::setName(const char name[])
{
unsigned char count;
for (count = 0; name[count] != '\0'; count++);
this->name = new char[count];
for (unsigned char i = 0; name[i] != '\0'; i++)
this->name[i] = name[i];
}
Now I have an addWinery()
function in my list.cpp file which is part of my list class, but I'm not going to include that because I don't think it's causing the issue.
For cout
ing my values I overloaded the <<
operator:
std::ostream& operator<< (std::ostream& out, const char array[])
{
for (unsigned char i = 0; array[i] != '\0'; i++)
{
out << array[i];
}
return out;
}
std::cout << "Name: " << head->item.getName() << std::endl;
Prints this in Linux
Name: Winery
and this in Visual Studio:
Name: Winery²²²²½½½½½½½½ϵ▮ϵ▮ϵ▮
Now, I'm guessing this error is caused by my winery::setName()
& winery::setLocation()
functions although I can't see anything wrong with these two functions. I could be wrong about that.
Again, you guys may find this easier to test for yourselves, so here's a dropbox link.
If you could also explain what is wrong with my ~winery()
deconstructor or why I don't need it for these dynamically allocated arrays (C-Style Strings) I would be so grateful.
Upvotes: 1
Views: 127