Thread: My compiler shuts down when I run this program...

  1. #1
    Registered User
    Join Date
    Jan 2003
    Posts
    118

    My compiler shuts down when I run this program...

    I am using Turbo c++ 4.5, and everytime I run this program weird errors happen and sometimes it shuts down. Im going to replace the compiler but in the meantime I wanted to know if this program works for anyone else.
    Code:
    #include <iostream.h>
    #include <string.h>
    
    #ifndef BUFFER_H
    #define BUFFER_H
    
    //class Buffer
    class Buffer {
    public:
    	Buffer();
    	~Buffer();
    
    	void Add(int Pos, char *b);
    	void ClearBuff();
    	void DisplayBuff();
    
    private:
    	char *a;
    };
    
    #endif
    
    Buffer::Buffer()
    {
    	a = new char[1999];
    }
    
    Buffer::~Buffer()
    {
    	delete[] a;
    }
    
    void Buffer::Add(int Pos, char *b)
    {
    	int i = 0;
    
    	for (i = 0; i < strlen(b); i++)
    	{
    		if (Pos+i > 1999 || Pos+i < 0)
    			return;
    		a[Pos+i] = b[i];
    	}
    }
    
    void Buffer::ClearBuff()
    {
    	int i = 0;
    
    	for (i = 0; i < 1999; i++)
    		a[i] = ' ';
    }
    
    void Buffer::DisplayBuff()
    {
    	int i = 0;
    
    	for (i = 0; i < 1999; i++)
    		cout << a[i];
    }
    
    /******************************************************************************/
    
    #ifndef ITEM_H
    #define ITEM_H
    
    // class Item
    class Item {
    public:
    	Item();
    	Item(char *a, float b);
    	~Item();
    
    	float GetPrice() { return Price; }
    	char *GetName() { return Name; }
    
    	void operator = (char *a);
    	void operator = (float a) { Price = a; }
    
    private:
    	float Price;
    	char *Name;
    };
    
    #endif
    
    Item::Item(): Price(0.0f), Name(NULL)
    {
    }
    
    Item::Item(char *a, float b): Price(b), Name(NULL)
    {
    	int i = 0;
    
    	if (Name != NULL)
    	{
    		if (strlen(Name) == 1)
    			delete Name;
    		else
    			delete[] Name;
    	}
    
    	Name = new char[ strlen(a) + 1 ];
    
    	for (i = 0; i < strlen(a); i++)
    		Name[i] = a[i];
    }
    
    Item::~Item()
    {
    	if (Name != NULL)
    	{
    		if (strlen(Name) == 1)
    			delete Name;
    		else
    			delete[] Name;
    	}
    }
    
    void Item::operator = (char *a)
    {
    	int i = 0;
    
    	if (Name != NULL)
    	{
    		if (strlen(Name) == 1)
    			delete Name;
    		else
    			delete[] Name;
    	}
    
    	Name = new char[ strlen(a) + 1];
    
    	for (i = 0; i < strlen(a); i++)
    		Name[i] = a[i];
    }
    
    /******************************************************************************/
    
    int xytobuff(int x, int y);
    void AddtoBuff(Buffer a, Item *b);
    
    int main()
    {
    	Buffer a;
    	Item b[5];
    
    	b[0] = "Zanahoria";
    	b[0] = 16.00;
    	b[1] = "Papa";
    	b[1] = 19.90;
    	b[2] = "Cebolla";
    	b[2] = 9.00;
    	b[3] = "Pepino";
    	b[3] = 12.50;
    	b[4] = "Papaya";
    	b[4] = 16.00;
    	b[5] = "Sandia";
    	b[5] = 26.90;
    
    	a.ClearBuff();
    	AddtoBuff(a, b);
    	a.DisplayBuff();
    
    	return 0;
    }
    
    int xytobuff(int x, int y)
    {
    	return x + y * 80;
    }
    
    void AddtoBuff(Buffer a, Item *b)
    {
    	int x = 5, y = 1;
    	int i = 0;
    
    	while (i < 5)
    	{
    		a.Add(xytobuff(x, y), b[i].GetName());
    		y++;
    		i++;
    	}
    }
    I know somethings look stupid but I was just messing around triying to fix it, maybe sometimes I dont free the memory or something like that.
    Why drink and drive when you can smoke and fly?

  2. #2
    Registered User
    Join Date
    Nov 2001
    Posts
    1,348
    Sounds like there is invalid memory access issue and the OS is set to reboot upon such an application error.

    Kuphryn

  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
    Your deletes are wrong for starters
    Even if you do new char[1], you still need to do delete[ ]

    Basically, you don't need all those if tests in front of all you deletes


    > b[5] = "Sandia";
    > b[5] = 26.90;
    You only have b[5], so these are off the end of the array.
    Delete them, or say Item b[6]
    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
    Join Date
    Jan 2003
    Posts
    118
    Thanks very much for the help, I corrected those mistakes and now it runs, but there still seems to be some kind of memory miss reading/writing because I get this kind of output:

    Papa(WeirdCharacters)
    Zanahoria(Weird Characters)

    and so on, The characters are totally random and sometimes they dont appear. Too bad I dont understand the debugger for this compiler.
    Why drink and drive when you can smoke and fly?

  5. #5
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    Code:
    void Buffer::Add(int Pos, char *b)
    {
    	int i = 0;
    
    	for (i = 0; i < strlen(b); i++)
    	{
    		if (Pos+i > 1999 || Pos+i < 0)
    			return;
    		a[Pos+i] = b[i];
    	}
    }
    You're off by one here. That should be:
    Code:
    if (Pos+i >= 1999 || Pos+i < 0)
    Worse, you are not null terminating the name string:
    Code:
    void Item::operator = (char *a)
    {
    	int i;
    
    	delete[] Name;
    
    	Name = new char[ strlen(a) + 1];
    
    	for (i = 0; i < strlen(a); i++)
    		Name[i] = a[i];
    }
    You never add a null terminator so the name string is not correctly null terminated. Therefore, the name string continues into random memory. This may crash your program or it may simply continue into random memory until it finds a null terminator.

  6. #6
    Registered User
    Join Date
    Jan 2003
    Posts
    118
    Oh man really thanks a lot, that corrected it all.
    Why drink and drive when you can smoke and fly?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help on making program run more efficiently
    By peeweearies in forum C Programming
    Replies: 2
    Last Post: 03-23-2009, 02:01 AM
  2. How to time how long a program takes to run?
    By advancedk in forum C Programming
    Replies: 2
    Last Post: 08-18-2008, 07:50 PM
  3. Help me with my basic program, nothing I create will run
    By Ravens'sWrath in forum C Programming
    Replies: 31
    Last Post: 05-13-2007, 02:35 AM
  4. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  5. Replies: 5
    Last Post: 04-17-2003, 07:07 AM