Heap Corruption issues using calloc/realloc [C++]

Shaitan00

Junior Contributor
Joined
Aug 11, 2003
Messages
358
Location
Hell
I've been basing my head on this problem for 2 days now and give up ... hoping someone out there has a good idea to help me either fix the issue or pinpoint the actual problem...

I have code to perform calculations of big numbers using (int* integer) which uses calloc and realloc to manage dynamic memory, calloc is called to create the initial memory and realloc there after to resize it when needed - take a look at how I am trying to do it...


This is the HEADER FILE for my class A:
Code:
class A
{
private:
	int* integer;
public:
	A(const int = 1);
	A(const string&);
	A(const A&);
	~A();
	const A& operator= (const A&);
	void DeepCopy(const A&);
	void StringCopy(const string&);
};

The is the CPP FILE for my class A:
Code:
A::A(int nSize) :
		size(nSize < 1 ? 1 : nSize),
		integer((int*) calloc (nSize, sizeof(int)))
		{}

A::A(const A& a)
{
	integer = NULL;
	DeepCopy(a);
}

A::~A()
{
	free (integer);
}

const A& A::operator= (const A& a)
{
	if (this != &a)
		DeepCopy(a);
	return *this;
}

void A::DeepCopy(const A& a)
{
	int nCurrentSize = size;
	size = a.size;

	if (integer == NULL)
		integer = (int*) calloc (size, sizeof(int));
	else if (nCurrentSize != size)
		integer = (int*) realloc (integer, size * sizeof(int));

	for (int i = 0; i < size; i++)
		integer[i] = a.integer[i];
}

A::A(const string& sNumber)
{
	integer = NULL;
	StringCopy(sNumber);
}

void A::StringCopy(const string& sNumber)
{
	int nIndex = 0;
	nIndex = sNumber.find_first_not_of("0", nIndex);
	if (nIndex == -1) nIndex = sNumber.size() - 1;
	int nCurrentSize = size;
	size = sNumber.size() - nIndex;

	if (integer == NULL)
		integer = (int*) calloc (size, sizeof(int));
	else if (nCurrentSize != size)
		integer = (int*) realloc (integer, size * sizeof(int));

	for(int i = nIndex, j = 0; i < sNumber.size(); i++, j++)
	        integer[j] = sNumber[i] - '0';
}


So, the key things to see (I assume) are my calls to CALLOC, REALLOC, and FREE - as you can see in the base constructors initalization list I allocate (CALLOC) some memory for my int* integer, this creates a 0 (integer). Then in either my copy constructor or overloaded "=" I call DEEPCOPY which is where I do my magic...

I check to the see INTEGER==NULL (if so I assume memory has not yet been allocated), in this case I do a CALLOC, otherwise if the new/old sizes are different I do a REALLOC to fit the new one ... then do the actual deep copying... this not good?

Now, after running the program at some points I get the follow error:
Windows has triggered a breakpoint in A.exe.
This may be due to a corruption of the heap, which indicates a bug in Calculator.exe or any of the DLLs it has loaded.
HEAP[A.exe]: Invalid address specified to RtlValidateHeap( 00020000, 00025FB0 )
What can I do with this?

If you have any advice, clues, ideas, or ways to help me track down the issue it would be much appreciated ...
Thanks,
 
Last edited:
Why not use the array creation syntax rather than memory allocation functions? I'm far from a C++ expert, but it seems more natural to me to write
Code:
integer = new int[size];
than
Code:
integer = (int*) calloc (size, sizeof(int));
As far as realloc() goes, does it deallocate the memory the poiner points to then reallocate new memory? Does it preserve data?
 
Back
Top