Click here to Skip to main content
15,885,757 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,all.
I have made = operator and use in VC++.

The definition is as follows:
C++
class CMain
{
public:
  CMain(int nDim)
  {
    dim = nDim;
    pK = new int[nDim];
    memset(pK, 0, sizeof(int) * nDim);
  }
  ~CMain()
  {
    if(pK != NULL)
    {
      delete[] pK;
      pK = NULL;
    }
  }
  CMain& operator(CMain& other)
  {
    dim = other.dim;
    delete[] pK;
    pK = new int[dim];
    memcpy(pK, other.pK, sizeof(int) * dim);
    return *this;
  }
  int dim;
  int* pK;
};

and in other cpp file, my code is as follow:
C++
void main()
{
std::vector<CMain> pTemp(5, CMain(3));

for(int i = 0; i < 5; i++)
{
  CMain m(3);
  m.pK[0] = i;
  m.pK[1] = i;
  m.pK[2] = i;
  
  pTemp[i] = m;
}
CMain mm(3);
for(int i = 0; i < 5; i++)
{
  if(pTemp[i].pK[1] == 2)
    mm = pTemp[i];
}
}

Why does my code memory error?
Is my = operator mistake?
Thanks.
Posted
Updated 22-May-15 0:32am
v3

You didn't (re-)define the assignment operator for your class (your code doesn't even compile). You have to (re-)define both the assignment operator and the copy constructor in order to avoid catastrophic failures, e.g.
C++
CMain (const CMain & other) // copy ctor
{
  clone(other);
}
CMain & operator = (const CMain & other) // assignment operator
{
  clone(other);
  return *this;
}

void clone( const CMain & other )
{
  dim = other.dim;
  if ( pK )
    delete[] pK;
  pK = new int[dim];
  memcpy(pK, other.pK, sizeof(int) * dim);
}


In any case, please note, it would be better use a vector<int></int> instead of the handcrafted array inside your CMain class.
 
Share this answer
 
Comments
forest-321 25-May-15 7:52am    
Thank you for your advice.
Firstly you have to forget the C-prefix - its ugly and its not part of the ungary notation how the most of people think.

Now to your problem. You only make a shallow copy here: pTemp[i] = m; like memcpy. So when m gets out of scope the destructor of CMain gets called. The destructor now deletes the array you allocated before in the consturctor, the result of that is, that pK gets invalid in pTemp[i].

EDIT:
This isn't enought:
Replacing pTemp[i] = m with pTemp[i] = std::move(m) should solve it.

You have to define your own move assignment operator, the full working program:
C++
#include <vector>
#include <cstring>

class CMain
{
public:
	CMain(int nDim)
	{
		dim = nDim;
		pK = new int[nDim];
		memset(pK, 0, sizeof(int) * nDim);
	}
	~CMain()
	{
		if (pK != NULL)
		{
			delete[] pK;
			pK = NULL;
		}
	}
	CMain& operator() (CMain& other)
	{
		dim = other.dim;
		delete[] pK;
		pK = new int[dim];
		memcpy(pK, other.pK, sizeof(int) * dim);
		return *this;
	}
	CMain& operator=(CMain&& other)
	{
		dim = other.dim;
		pK = other.pK;
		other.pK = nullptr;
		return *this;
	}
	int dim;
	int* pK;
};

int main(int argc, char* argv[])
{
	std::vector<CMain> pTemp(3, CMain(3));
	for (int i = 0; i < 3; i++)
	{
		CMain m(1);
		m.pK[0] = i;
		m.pK[1] = i;
		m.pK[2] = i;
		pTemp[i] = std::move(m);
	}
	CMain mm(3);
	for (int i = 0; i < 3; i++)
	{
		if (pTemp[i].pK[1] == 2)
			mm = std::move(pTemp[0]);
	}
	return 0;
}

Or if you need multyple pointer to the same object either your store the ptr in a globaly object preferably instanced as normaly object and point to them from your CMain-Objects or you use an std::shared_ptr<int[]> with an custom destructor.

C++ is hard to learn^^
 
Share this answer
 
v10
Comments
forest-321 25-May-15 7:52am    
Thanks
I'm not complete sure ... have you tried := instead of =
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900