Title: 0xC0000005: Access violation reading location 0xccccccd0. C++
Tags: c++;access-violation
Question: I'm having the above issue when I attempt to set a string (stored in a class) equal to another string. I have combed and combed trying to find if I did not initialize any variables, but I can't find such a situation. In debug mod, I get the above error. In release mode it hangs and Win7 looks for a problem, no major abort or retry window. Here's the relevant code, there is another header file with my main program if you feel it should be included, I'll include the line that causes errors. Language is C++ obviously.
//Error occurs in this area:
Car one;
one = two;
one.addExtra ("Windows");
log << "Car one: " << one << endl;
two = Car(one); // call copy constructor.
//I realize when I call the first one = two, there are no extras
//stored int Car one, which is what differs between the two. Remaining
//code. Extras header:
#include <iostream&gt;
#include <string&gt;
#include <string.h&gt;
using namespace std;
class Extras
public:
friend class Car;
friend int main();
friend ostream&amp; operator << (ostream&amp; os, const Extras&amp; in);
friend class CarLot;
Extras(const Extras&amp; other);
Extras&amp; operator=(Extras  &amp;rhs);
Extras(string in);
Extras();
~Extras();
void modify_ext(string* in);
//string ex_list;
private:
int place;
string *ex_list;
//Extras.cpp:
#include "Extras.h"
Extras75b5:f2c1:f5d7:b954:8dc3:cee9:baf:a8c0xtras(string in)
delete ex_list;
ex_list = new string;
place = 0;
//ex_list = new string[4];
(*ex_list) = in;
place++;
Extras75b5:f2c1:f5d7:b954:8dc3:cee9:baf:a8c0xtras()
//ex_list = new string[4];
place = 0;
//for(int i = 0; i < 4; i++)
ex_list = new string;
*ex_list = "0";
//Overloaded << operator for Extras class to
//easily output array contents
ostream&amp; operator<< (ostream&amp; os, Extras const &amp;in)
os << *(in.ex_list);
return os;
Extras&amp; Extras75b5:f2c1:f5d7:b954:8dc3:cee9:baf:a8c0operator=(Extras &amp;rhs)
if(this != &amp;rhs)
//string temp;
//temp = rhs.ex_list;
modify_ext(rhs.ex_list);
cout << endl << endl << ex_list << endl << endl;
place = rhs.place;
return *this;
Extras75b5:f2c1:f5d7:b954:8dc3:cee9:baf:a8c0xtras(const Extras&amp; other) : place(other.place), ex_list(other.ex_list)
//for(int i = 0; i < 4; i++)
//ex_list = other.ex_list;
void Extras75b5:f2c1:f5d7:b954:8dc3:cee9:baf:a8c0modify_ext(string* in)
delete ex_list;
ex_list = new string;
(*ex_list).resize((*in).size());
for(unsigned int i = 0; i < (*in).size(); i++)
ex_list[i] = in[i];
Extras75b5:f2c1:f5d7:b954:8dc3:cee9:baf:a8c0~Extras()
delete ex_list;
place = 0;
//Car Header:
#include "Extras.h"
class Car
public:
friend class Extras;
friend Extras&amp; Extras75b5:f2c1:f5d7:b954:8dc3:cee9:baf:a8c0operator=(Extras &amp;rhs);
friend int main();
friend ostream&amp; operator<< (ostream&amp; os, const Car&amp; in);
friend class CarLot;
friend void add_extra();
~Car();
Car();
Car(Car&amp; other);
Car(string in_name, int in_year, string in_color, float in_cost);
Car&amp; operator=(Car const &amp;rhs);
void edit_extr(int in);
void addExtra(string in);
private:
string name, color;
int year, extr_num;
float cost;
Extras  *extr;
//Car.cpp:
#include "car.h"
//Constructor
Car75b5:f2c1:f5d7:b954:8dc3:cee9:baf:a8c0r(string in_name, int in_year, string in_color, float in_cost)
name = in_name;
color = in_color;
year = in_year;
cost = in_cost;
extr = new Extras[3];
extr_num = 0;
//Overloaded = operator
Car&amp; Car75b5:f2c1:f5d7:b954:8dc3:cee9:baf:a8c0operator=(Car const &amp;rhs)
if(this != &amp;rhs)
name = rhs.name;
color = rhs.color;
year = rhs.year;
cost = rhs.cost;
//delete extr;
extr = rhs.extr;
extr_num = rhs.extr_num;
return *this;
//Default Constructor
Car75b5:f2c1:f5d7:b954:8dc3:cee9:baf:a8c0r()
name = "TEMP";
color = "BLUE";
year = 0;
cost = 0;
extr = new Extras[3];
extr_num = 0;
//Destructor
Car75b5:f2c1:f5d7:b954:8dc3:cee9:baf:a8c0~Car()
delete extr;
extr = NULL;
//Copy constructor
Car75b5:f2c1:f5d7:b954:8dc3:cee9:baf:a8c0r(Car&amp; other) : name(other.name), color(other.color), year(other.year),
cost(other.cost), extr_num(other.extr_num)
//delete extr;
for(int i = 0; i < extr_num; i++)
extr[i].modify_ext(other.extr[i].ex_list);
extr[i].place = other.extr[i].place;
//Overloaded << operator for Car class
ostream&amp; operator<< (ostream&amp; os, const Car&amp; in)
os.precision(2);
os << in.name << ", " << in.year << ", "
<< in.color << ", $"<< in.cost << ", ";
os << "extras include: ";
for(int k = 0; k < in.extr_num; k++)
os << in.extr[k] << ", ";
os << endl;
return os;
void Car75b5:f2c1:f5d7:b954:8dc3:cee9:baf:a8c0it_extr(int in)
Extras* temp;
temp = new Extras[in];
for(int i = 0; i < in; i++)
temp[i] = extr[i];
extr_num = in;
delete extr;
extr = temp;
void Car75b5:f2c1:f5d7:b954:8dc3:cee9:baf:a8c0xtra(string in)
if(extr_num == 3)
//log << "Car has too many extras.";
return;
//edit_extr(extr_num + 1);
*(extr[extr_num].ex_list) = in;
extr[extr_num].place++;
extr_num++;
As I said, I have one more additional header, another class, and a main program if those need to be included, but I figured this was more than enough code (sorry!) for anyone to look through. Any help would be incredibly appreciated.
Comment: I don't think this question deserves any answer...

Comment: Piotr, I did both of those. I said in my OP that I ran it in debug and thats how I got the error that is the title of the question, and I have tried to test every function alone and got no results. I started re-adding things after that. Sorry.

Comment: You should always try to *minimise* code required to reproduce the problem. And at least try to run it under debugger.

Here is the accepted answer: What I'm seeing that's broken:
two = Car(one); // call copy constructor.
No, it creates a temporary object with the copy constructor, passes this to operator=() on two, then destroys the temporary.
Extras&amp; operator=(Extras  &amp;rhs);
should be:
Extras&amp; operator=(const Extras &amp;rhs);
Extras75b5:f2c1:f5d7:b954:8dc3:cee9:baf:a8c0xtras(string in)
delete ex_list;
ex_list = new string;
place = 0;
//ex_list = new string[4];
(*ex_list) = in;
place++;
Better:
Extras75b5:f2c1:f5d7:b954:8dc3:cee9:baf:a8c0xtras(const string&amp; in): place(1), ex_list(new string(in))
Extras75b5:f2c1:f5d7:b954:8dc3:cee9:baf:a8c0xtras(const Extras&amp; other) : place(other.place), ex_list(other.ex_list)
//for(int i = 0; i < 4; i++)
//ex_list = other.ex_list;
Looking at your default constructor, it's clear that the Extras object owns the string in ex_list.  However, this copy constructor claims ownership of the original object's ex_list.  It should make its own copy:
Extras75b5:f2c1:f5d7:b954:8dc3:cee9:baf:a8c0xtras(const Extras&amp; other): place(other.place),
ex_list(new string(other.ex_list))
void Extras75b5:f2c1:f5d7:b954:8dc3:cee9:baf:a8c0modify_ext(string* in)
delete ex_list;
ex_list = new string;
(*ex_list).resize((*in).size());
for(unsigned int i = 0; i < (*in).size(); i++)
ex_list[i] = in[i];
You're copying the string.  All you need is:
void Extras75b5:f2c1:f5d7:b954:8dc3:cee9:baf:a8c0modify_ext(const string* in)
*ex_list = *in;
Moving on to the Car...
friend class Extras;
friend Extras&amp; Extras75b5:f2c1:f5d7:b954:8dc3:cee9:baf:a8c0operator=(Extras &amp;rhs);
friend int main();
friend ostream&amp; operator<< (ostream&amp; os, const Car&amp; in);
friend class CarLot;
friend void add_extra();
You should consider refactoring the code to get rid of these.
Car(Car&amp; other);
Car(string in_name, int in_year, string in_color, float in_cost);
Should be:
Car(const Car&amp; other);
Car(const string&amp; in_name, int in_year, const string&amp; in_color, float in_cost);
References are your friends when passing objects to functions.
I'm going to stop here.
Comment for this answer: I should explain, since that code is embarrassingly bad. I ended up having a problem setting one string = to another in the copy constructor, so I just started changing const's and references since it was an access issue, a lot of the stuff you told me to change was there originally and has been reverted. I changed all of that and get the same error. Any further ideas?
Also,the first comment that says calls copy constructor was my teacher's, after I had modified it.
Comment for this answer: Ran debug again, freezes on:
*ex_list = *in;
Which is in the modify_ext function, which is a member function of Extras, the class containing ex_list (both string pointers).
Comment for this answer: Thanks for your help Mike. I debugged it and in was perfectly set to what it was supposed to be, "Windows". This' ex_list is undefined. It didn't work. I copied my Extras files over to a completely new project and they all compiled and ran perfectly fine, so I went in and changed up a few things, made it so I do not use the code in question, and use the tested overloaded = operator for Extras, which should work. Now I get the error: "Access violation writing location." And then it crashes. Any idea why changing solutions changes behavior?
Comment for this answer: Thanks Mike. Figured it all out, was a pointer error. Appreciate the time and effort.
Comment for this answer: Well, at the risk of beating on the heap more than necessary, you could try `{ delete ex_list; ex_list = new string(*in); }`.  Or you could use the debugger to set a breakpoint before `*ex_list = *in;` and look at `ex_list`, `in`, and what they point to.  Freeze => infinite loop => trying to copy something and never hitting the "done" condition => corrupt input => `in == 0`?
Comment for this answer: "This' ex_list is undefined." ... There's your real problem.  Backtrack and find out why it's not set.  Maybe `this` isn't valid?  As for the changed solution, you lost me.
Comment for this answer: @Mike: +1, you had more patience to actually list the issues out.  I gave up after I saw more than 4 :)

Here is another answer: In your constructor you are deleting ex_list.  It hasn't even been allocated yet, so this is wrong.  Remove that and do this instead when allocating your new string:
ex_list = new string(in);
This way you use the string copy constructor.  You can get rid of the rest of whatever you were trying to do below in the constructor since this will do it for you.
Actually, there are a ton of problems all throughout this code.  Is there any reason you want your string to be a pointer internally?  You aren't using pointers correctly in a bunch of different places.  I only noticed the first one as I scrolled down.
Comment for this answer: Pointers in the Car class is there to set the Extras arrays equal. Pointer in Extras class was a desperation move to fix a problem that was already occurring, that continues to occur, even after applying all mentioned updates here and previously adding pointers.

Here is another answer: The allocator in debug builds with VC++ uses some magic values to fill allocated areas.  In particular, 0xCC is memory that had been allocated but it now freed.  So the address 0xCCCCCCD0, looks suspiciously like a small offset (e.g., to a struct or class member) from a pointer in memory that was freed.  Given that, Mark Loeser's answer looks promising.

{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 3, 'CommentCount': 3, 'ContentLicense': 'CC BY-SA 2.5', 'CreationDate': '2011:00:02/14/11', 'FavoriteCount': None, 'PostTypeId': 1, 'Tags': '<c++><access-violation>', 'Title': '0xC0000005: Access violation reading location 0xccccccd0. C++', 'ViewCount': 9493, 'id': 4996235, 'user_AboutMe': '', 'user_AccountId': 306120.0, 'user_DisplayName': 'zmarine', 'user_Reputation': 23.0, 'user_Views': 10.0, 'user_id': 616683.0}}