Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New Coder
    Join Date
    Jan 2005
    Posts
    74
    Thanks
    0
    Thanked 1 Time in 1 Post

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

    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 by Shaitan00; 06-01-2009 at 03:01 AM.

  • #2
    Rockstar Coder
    Join Date
    Jun 2002
    Location
    USA
    Posts
    9,074
    Thanks
    1
    Thanked 328 Times in 324 Posts
    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?
    OracleGuy

  • #3
    Master Coder
    Join Date
    Dec 2007
    Posts
    6,682
    Thanks
    436
    Thanked 890 Times in 879 Posts
    try:
    Code:
    A::~A(){
       if(integer != NULL)
    	free (integer);
    }
    best regards

  • #4
    Rockstar Coder
    Join Date
    Jun 2002
    Location
    USA
    Posts
    9,074
    Thanks
    1
    Thanked 328 Times in 324 Posts
    Quote Originally Posted by oesxyl View Post
    try:
    Code:
    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.
    OracleGuy


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •