Thread: Heap Corruption Error

  1. #1
    Registered User ralocorvette's Avatar
    Join Date
    Nov 2010
    Posts
    8

    Heap Corruption Error

    Code:
    Hello every one
    Im having a trouble with an Inventory Class
    Im getting a HEAP CORRUPTION  ERROR.
    After I compile a window appers saying:
    HEAP ERROR DETECTD: AFTER nORMAL BLOCK (#135) AT 0X00474860.
    CRT DETECTED THAT THE APPLICATION WROTE TO MEMORY AFTER END OF HEAP BUFFER
    Here is the code:
    
    //InventoryItem.h
    //This class has a private member function
    #ifndef INVENTORYITEM_H
    #define INVENTORYITEM_H
    
    #include<cstring> //needed for strlen and strcpy
    
    //Constant for the descrptions default size
    const int DEFAULT_SIZE = 51;
    
    class InventoryItem
    {
    private:
    	char *description; //The item description
    	double cost;	   //The item cost
    	int units;		   //Number of units on hand
    
    	//Private Member function.
    	void createDescription(int size, char *value)
    	{//Allocate the default ammount of memory fir description.
    		description = new char [size];
    
    		//Store a value in the memory.
    		strcpy(description,value);}
    public:
    	//Constructor #1
    	InventoryItem()
    	{//Stotre an empty string in the description of the
    		//atribute
    		createDescription(DEFAULT_SIZE, "");
    
    		//Initialize cost and units
    
    		cost = 0.0;
    		units = 0; }
    
    	//Contructor#2
    	InventoryItem(char *desc)
    	{//Allocate memory and store the description.
    		createDescription(strlen(desc),desc);
    
    		//Initialize cost and units
    		cost = 0.0;
    		units = 0;}
    	
    	//Constructor#3
    	InventoryItem(char *desc,double c,int u)
    	{//Allocate memory and store the description
    		createDescription(strlen(desc),desc);
    
    		//Asssign Values to the cost and units
    		cost = c;
    		units = u;}
    
    	//Destructor
    	~InventoryItem()
    	{delete [] description;}
    
    	//Mutator Functions
    	void setDescription(char *d)
    	{strcpy(description,d);}
    
    	void setCost(double c)
    	{cost = c;}
    
    	void setUnits(int u)
    	{units = u;}
    
    	//Accesor Functions
    	const char *getDescription()const
    	{return description;}
    
    	double getCost() const
    	{return cost;}
    
    	int getUnits () const
    	{return units;}
    
    };
    
    #endif
    
    
    
    
    
    //InventoryItem.cpp
    //This program demonstrates an array of class object
    #include<iostream>
    #include<iomanip>
    #include "InventoryItem.h"
    using namespace std;
    
    int main()
    {
    	const int NUM_ITEMS = 5;
    	InventoryItem inventory[NUM_ITEMS] = {
    		InventoryItem("Hammer",6.95, 12),
    		InventoryItem("Wrench",8.75, 20),
    		InventoryItem("Pliers",3.75, 10),
    		InventoryItem("Ratchet",7.95, 14),
    		InventoryItem("Screwdriver",2.50, 22) };
    
    		cout<<setw(14)<<"Inventory Item"
    			<<setw(8)<<"Cost"
    			<<setw(16)<<"Units on Hand\n";
    		cout<<"-------------------------------------\n";
    
    		for (int i = 0;i< NUM_ITEMS;i++)
    		{
    			cout<<setw(14)<<inventory[i].getDescription();
    			cout<<setw(8)<<inventory[i].getCost();
    			cout<<setw(7)<<inventory[i].getUnits();
    			
    		}
    		return 0;
    }
    		/*'Inventory Class.exe': Loaded 'C:\Users\RKLO\Documents\Visual Studio 2010\Projects\Inventory Class\Debug\Inventory Class.exe', Symbols loaded.
    'Inventory Class.exe': Loaded 'C:\Windows\SysWOW64\ntdll.dll', Symbols loaded (source information stripped).
    'Inventory Class.exe': Loaded 'C:\Windows\SysWOW64\kernel32.dll', Symbols loaded (source information stripped).
    'Inventory Class.exe': Loaded 'C:\Windows\SysWOW64\KernelBase.dll', Symbols loaded (source information stripped).
    'Inventory Class.exe': Loaded 'C:\Windows\SysWOW64\msvcp100d.dll', Symbols loaded.
    'Inventory Class.exe': Loaded 'C:\Windows\SysWOW64\msvcr100d.dll', Symbols loaded.
    'Inventory Class.exe': Loaded 'C:\Windows\SysWOW64\user32.dll', Symbols loaded (source information stripped).
    'Inventory Class.exe': Loaded 'C:\Windows\SysWOW64\gdi32.dll', Symbols loaded (source information stripped).
    'Inventory Class.exe': Loaded 'C:\Windows\SysWOW64\lpk.dll', Symbols loaded (source information stripped).
    'Inventory Class.exe': Loaded 'C:\Windows\SysWOW64\usp10.dll', Symbols loaded (source information stripped).
    'Inventory Class.exe': Loaded 'C:\Windows\SysWOW64\msvcrt.dll', Symbols loaded (source information stripped).
    'Inventory Class.exe': Loaded 'C:\Windows\SysWOW64\advapi32.dll', Symbols loaded (source information stripped).
    'Inventory Class.exe': Loaded 'C:\Windows\SysWOW64\sechost.dll', Symbols loaded (source information stripped).
    'Inventory Class.exe': Loaded 'C:\Windows\SysWOW64\rpcrt4.dll', Symbols loaded (source information stripped).
    'Inventory Class.exe': Loaded 'C:\Windows\SysWOW64\sspicli.dll', Symbols loaded (source information stripped).
    'Inventory Class.exe': Loaded 'C:\Windows\SysWOW64\cryptbase.dll', Symbols loaded (source information stripped).
    'Inventory Class.exe': Loaded 'C:\Windows\SysWOW64\imm32.dll', Symbols loaded (source information stripped).
    'Inventory Class.exe': Loaded 'C:\Windows\SysWOW64\msctf.dll', Symbols loaded (source information stripped).
    'Inventory Class.exe': Loaded 'C:\Windows\SysWOW64\uxtheme.dll', Symbols loaded (source information stripped).
    'Inventory Class.exe': Loaded 'C:\Windows\SysWOW64\dwmapi.dll', Symbols loaded (source information stripped).
    'Inventory Class.exe': Loaded 'C:\Windows\SysWOW64\ole32.dll', Symbols loaded (source information stripped).
    'Inventory Class.exe': Loaded 'C:\Windows\SysWOW64\oleaut32.dll', Symbols loaded (source information stripped).
    The thread 'Win32 Thread' (0x19bc) has exited with code -1073741510 (0xc000013a).
    The program '[5892] Inventory Class.exe: Native' has exited with code -1073741510 (0xc000013a).
    */
    //ANY HELP IS REALLY APPRECIATED
    // I UPLOADED THE CODE FOR YOUR BENEFIT

  2. #2
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    Code:
    description = new char [size];
    Every C-string requires a null-terminator character, which you have failed to provide room for as the passed size was from strlen. strlen does not include the null terminator.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    If you used std::string instead of char arrays, life would be a lot simpler.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered User ralocorvette's Avatar
    Join Date
    Nov 2010
    Posts
    8
    Hey guys you really are some of a kind of life savers I solved my problem yesterday and Im posting the Correct code today

  5. #5
    Registered User ralocorvette's Avatar
    Join Date
    Nov 2010
    Posts
    8
    Code:
    THIS IS THE CORRECT CODE
    
    
    //InventoryItem.h
    //This class has a private member function
    #ifndef INVENTORYITEM_H
    #define INVENTORYITEM_H
    
    #include<cstring> //needed for strlen and strcpy
    using namespace std;
    //Constant for the descrptions default size
    const int DEFAULT_SIZE = 51;
    
    class InventoryItem
    {
    private:
    	char *description; //The item description
    	double cost;	   //The item cost
    	int units;		   //Number of units on hand
    
    	//Private Member function.
    	void createDescription(int size, char *value)
    	{//Allocate the default ammount of memory fir description.
    		description = new char [size*'()'];
    		
    		//Store a value in the memory.
    		strcpy(description,value);}
    public:
    	//Constructor #1
    	InventoryItem()
    	{//Stotre an empty string in the description of the
    		//atribute
    		createDescription(DEFAULT_SIZE, "");
    
    		//Initialize cost and units
    
    		cost = 0.0;
    		units = 0; }
    
    	//Contructor#2
    	InventoryItem(char *desc)
    	{//Allocate memory and store the description.
    		createDescription(strlen(desc),desc);
    
    		//Initialize cost and units
    		cost = 0.0;
    		units = 0;}
    	
    	//Constructor#3
    	InventoryItem(char *desc,double c,int u)
    	{//Allocate memory and store the description
    		createDescription(strlen(desc),desc);
    
    		//Asssign Values to the cost and units
    		cost = c;
    		units = u;}
    
    	//Destructor
    	~InventoryItem()
    	{delete [] description;}
    
    	//Mutator Functions
    	void setDescription(char *d)
    	{strcpy(description,d);}
    
    	void setCost(double c)
    	{cost = c;}
    
    	void setUnits(int u)
    	{units = u;}
    
    	//Accesor Functions
    	const char *getDescription()const
    	{return description;}
    
    	double getCost() const
    	{return cost;}
    
    	int getUnits () const
    	{return units;}
    
    };
    
    #endif
    
    
    
    //InventoryItem.cpp
    //This program demonstrates an array of class object
    #include<iostream>
    #include<iomanip>
    #include<stdio.h>
    #include "InventoryItem.h"
    using namespace std;
    
    int main()
    {
    	const int NUM_ITEMS = 5;
    	InventoryItem inventory[NUM_ITEMS] = {
    		InventoryItem("Hammer",6.95, 12),
    		InventoryItem("Wrench",8.75, 20),
    		InventoryItem("Pliers",3.75, 10),
    		InventoryItem("Ratchet",7.95, 14),
    		InventoryItem("Screwdriver",2.50, 22) };
    
    		cout<<setw(14)<<"Inventory Item"
    			<<setw(8)<<"Cost"<<setw(8)
    			<<setw(16)<<"Units on Hand\n";
    		cout<<"-------------------------------------\n";
    
    		for (int i = 0;i< NUM_ITEMS;i++)
    		{
    			cout<<setw(14)<<inventory[i].getDescription();
    			cout<<setw(8)<<inventory[i].getCost();
    			cout<<setw(7)<<inventory[i].getUnits()<<endl;
    			
    		}
    		system("pause");
    		return 0;
    }
    /*Inventory Item    Cost  Units on Hand
    -------------------------------------
            Hammer    6.95     12
            Wrench    8.75     20
            Pliers    3.75     10
           Ratchet    7.95     14
       Screwdriver     2.5     22
    Press any key to continue . . .*/
    		
    
    // I UPLOADED DE CODE FOR THE BENEFIT OF THE USER

  6. #6
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    Code:
    description = new char [size*'()'];
    What the heck is THAT? Does it even compile? Sure doesn't for me:
    Code:
    InventoryItem.h:21:42: error: multi-character character constant

  7. #7
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Again, use std::string. Your program is a ticking time bomb, waiting to explode.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    For example, consider what would happen if you called setDescription() with a string longer than
    - the original string
    - longer than DEFAULT_SIZE
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  9. #9
    Registered User ralocorvette's Avatar
    Join Date
    Nov 2010
    Posts
    8
    Of course it compiles . Gees Im not using strings because the professor wants it that way, Take it easy guys. My code compiled really well, Im using Visual Studio 2010. That doesnt mean I dont get warnings from the compiler, but I get no errors. Its not my fault that the professor does not like it the easy safe way OK.
    Last edited by ralocorvette; 11-27-2010 at 02:44 PM.

  10. #10
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Just because it compiles doesn't mean it's right.
    It doesn't matter what the professor does or not. If you are going to learn C++, then I suggest you do it properly. The last we need is another "C+" user out there -_-
    It's up to you, but... if it were me, I'd "fix" the code and hand it in to the professor, and fix "the professor's" code, as well. No sense is teaching outdated crap.

    Also, eliminate any warnings you get. Only a professional programmer knows what warnings to skip and which to not, and even then, they rarely ignore warnings.
    Last edited by Elysia; 11-27-2010 at 02:53 PM.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  11. #11
    Registered User ralocorvette's Avatar
    Join Date
    Nov 2010
    Posts
    8
    You are right Im going to do it both ways

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Quantum Random Bit Generator
    By shawnt in forum C++ Programming
    Replies: 62
    Last Post: 06-18-2008, 10:17 AM
  2. Post...
    By maxorator in forum C++ Programming
    Replies: 12
    Last Post: 10-11-2005, 08:39 AM
  3. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 10:03 AM
  4. Stupid compiler errors
    By ChrisEacrett in forum C++ Programming
    Replies: 9
    Last Post: 11-30-2003, 05:44 PM
  5. Linking error
    By DockyD in forum C++ Programming
    Replies: 10
    Last Post: 01-20-2003, 05:27 AM

Tags for this Thread