...

View Full Version : Heap Corruption issues using calloc/realloc [C++]



Shaitan00
06-01-2009, 02:38 AM
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:


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:


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,

oracleguy
06-01-2009, 08:31 AM
Well test the different program paths. If you don't ever create a situation in which StringCopy is called, does it avoid the heap corruption?

oesxyl
06-01-2009, 09:05 AM
try:


A::~A(){
if(integer != NULL)
free (integer);
}


best regards

oracleguy
06-01-2009, 05:26 PM
try:


A::~A(){
if(integer != NULL)
free (integer);
}


best regards

That shouldn't matter, free like delete doesn't do anything if it gets a null pointer.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum