Thread: help with delete[]

  1. #1
    Registered User
    Join Date
    Aug 2008
    Posts
    3

    help with delete[]

    Hi, can anyone help with this error "heap corruption detected" ?
    Here is my program:

    Code:
    #include "stdafx.h"
    #include "iostream"
    #include "cassert"
    using namespace std;
    
    int capacity = 4; // default capacity
    double* billsAndCoins = new double[capacity]; // data storage for change
    int nBillsAndCoins = -1; // track the number of coins and bills
    // -1 means invalid, 0 means no change due, >0 is #of bills+coins changed
    
    bool set(double, double); // first parameter is price, last is amount paid
    double calc(double, double); // first parameter is denomination, last is change
    bool isValid(); // return false if the number of coins+bills 
    
    int i; // iteration tracker
    void myIterator(); // set index of array to 0
    bool hasNext(); // return the index number of coins+bills in the array
    double next(); // return a value from the array and advanced the index
    
    void release(); // delete the dynamic allocated array
    
    int main()
    {
    	set (14.57, 50);
    	if (!isValid()) // function checks if #of bills+coins is 0 or greater
    		cout << "Invalid input\n";
    	else
    	{
    		// print the change
    		for (myIterator(); hasNext();)
    			cout << '[' << next() << ']';
    	}
    	cout << endl;
    
    	set (63.68, 80);
    	if (!isValid()) // function checks if #of bills+coins is 0 or greater
    		cout << "Invalid input\n";
    	else
    	{
    		// print the change
    		for (myIterator(); hasNext();)
    			cout << '[' << next() << ']';
    	}
    	cout << endl;
    
    	release(); // contains the statement that deletes the dynamic array
    	cin.get();
    	return 0;
    }
    
    bool set(double price, double paid)
    {
    	double change = paid - price;
    	if(change == 0)
    	{
    		nBillsAndCoins = 0;
    		cout << "No Change";
    		return true;
    	}
    	else if(change > 0)
    	{
    		nBillsAndCoins = 0;
    		double money[11] = {100,50,20,10,5,1,0.5,0.25,0.1,0.05,0.01}; //denomination storage
    
    		for(int j = 0; j < 11; j++)
    			change = calc(money[j], change);
    
    		// if the change is still not 0 due to some errors (the change become very small), it rounds off to 0
    		if(change < 0.00001)
    			change = 0;
    
    		// if the change is still not 0 due to representational error, automatically allocate 1 penny to the array
    		if(change >= 0.009)
    		{
    			change = 0;
    			billsAndCoins[nBillsAndCoins] = 0.01;
    			nBillsAndCoins++;
    		}
    		assert(change == 0);
    
    		return true;
    	}
    	else
    		return false;
    }
    
    double calc(double Money, double Change)
    {
    	while(Change >= Money)
    	{
    		// check and doubling the size y for overloading arrays
    		if (nBillsAndCoins == capacity)
    		{
    			capacity *= 2;
    			double* temp = new double[capacity];
    			for (int i = 0; i < nBillsAndCoins; i++)
    				temp[i] = billsAndCoins[i];
    			delete [] billsAndCoins;
    			billsAndCoins = temp;
    		}
    
    		Change -= Money;
    		billsAndCoins[nBillsAndCoins] = Money;
    		nBillsAndCoins++;
    // but if I add " cout << Change << cin.get(); " the program has no error
    	}
    	return Change;
    }
    
    bool isValid()
    {
    	if(nBillsAndCoins < 0)
    		return false;
    	else
    		return true;
    }
    
    void myIterator()
    {
    	i=0;
    }
    
    bool hasNext()
    {
    	return i >= 0 && i < nBillsAndCoins;
    }
    
    double next()
    {
    	return billsAndCoins[i++];
    }
    
    void release()
    {
    	delete [] billsAndCoins;
    }
    Last edited by dicz.hack; 08-25-2008 at 01:05 AM.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    It would be much easier to debug if you used local variables instead and passed what is necessary as arguments to the functions.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Aug 2008
    Posts
    3

    reply to laserlight

    yeah, but my instructor said to me that I must use the global variable for this one.

  4. #4
    Registered User
    Join Date
    Aug 2008
    Posts
    3

    Solved

    Now I see where the problem is. I didnt realized that I need to add more space when adding 0.01 to the dynamically allocated array.

  5. #5
    Registered User
    Join Date
    May 2008
    Location
    Paris
    Posts
    248
    yeah, but my instructor said to me that I must use the global variable for this one.
    get a new instructor... ;-)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. delete[] problem with release config...
    By mikahell in forum C++ Programming
    Replies: 8
    Last Post: 08-21-2006, 10:37 AM
  2. delete[] without new[]?
    By ChadJohnson in forum C++ Programming
    Replies: 6
    Last Post: 04-04-2005, 11:39 AM
  3. delete[] or delete?
    By X PaYnE X in forum C++ Programming
    Replies: 7
    Last Post: 03-30-2005, 03:16 PM
  4. Memory issue with new[] and delete[]
    By Zarkhalar in forum C++ Programming
    Replies: 24
    Last Post: 08-07-2004, 07:45 AM
  5. delete and delete[]
    By Hunter2 in forum C++ Programming
    Replies: 13
    Last Post: 06-26-2003, 04:40 AM