help with delete[]

This is a discussion on help with delete[] within the C++ Programming forums, part of the General Programming Boards category; Hi, can anyone help with this error "heap corruption detected" ? Here is my program: Code: #include "stdafx.h" #include "iostream" ...

  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 02:05 AM.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,159
    It would be much easier to debug if you used local variables instead and passed what is necessary as arguments to the functions.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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, 11:37 AM
  2. delete[] without new[]?
    By ChadJohnson in forum C++ Programming
    Replies: 6
    Last Post: 04-04-2005, 12:39 PM
  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, 08:45 AM
  5. delete and delete[]
    By Hunter2 in forum C++ Programming
    Replies: 13
    Last Post: 06-26-2003, 05:40 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21