View Single Post
Old May 26th, 2006, 10:20 AM   #1
Soulstorm
Hobbyist Programmer
 
Soulstorm's Avatar
 
Join Date: Jan 2006
Location: Menidi, Athens, Greece
Posts: 243
Rep Power: 3 Soulstorm is on a distinguished road
Stash returns... Malloc problem?

I am expanding my stash class, and I am stuck at a problem I'm having... This is my stash.h code:

//Personnal Stash beta1
//This will hold an array of characters and will return it as a string
//Not completed yet.
#ifndef STASH_H
#define STASH_H
#include <iostream>
#include <fstream>
using namespace std;

int gIncrement = 4;

class stash{
	char *ch;
	int currentSize; //position of the last character
	int currentStorage; //size of the entire array
	int next;
public:
	stash();
	stash(int startSize);
	stash(char *p);
	
	~stash();
	
	void show(){
		cout << "currentStorage: " << currentStorage << "\n";
		cout << "current size: " << currentSize << "\n";
		cout << "Next: " << next << "\n";
		for(int i=0; i<currentSize; i++)
			cout << ch[i];
		cout << "\n";
	}
	
	void inflate();
	void add(char p);
	void insertString(char *p);
	char* returnAsString();
	void clearStash();
	
	void operator+(char *p);
	void operator=(char *p);
	stash operator=(stash s);
	
	friend istream& operator>>(istream& stream, stash s);
	
	
};

//**constructors and destructors
stash::stash(){
	ch = new char [0];
	currentSize = 0;
	currentStorage = 0;
	next = 0;
}

stash::stash(int startSize){
	ch = new char [startSize];
	currentSize = 0;
	currentStorage = startSize;
	next = currentStorage - currentSize;
}

stash::~stash(){
	delete [] ch;
	cout << "Freeing storage\n";
}

stash::stash(char *p){
	clearStash();
	insertString(p);
}
//**

//--Increase the stash's size to hold more chars.
void stash::inflate(){
	int i;
	char *temp = new char [currentSize + gIncrement];
	for(i=0; i<currentSize; i++)
		temp[i] = ch[i];
	delete [] ch;
	ch = temp;
	currentStorage = currentStorage + gIncrement;
}

//--add a character to the stash and resize it according
//--to space needed
void stash::add(char p){
	if(next == 0)
		inflate();
	ch[currentSize] = p;
	++currentSize;
	next = currentStorage - currentSize;
}

//--reset the stash
void stash::clearStash(){
	currentSize = 0;
	currentStorage = 0;
	ch = new char [0];
	next = currentStorage - currentSize;
}

//--insert an entire string into the stash
void stash::insertString(char *p){
	int i;
	for(i=0; p[i]; i++){
		add(p[i]);
	}
}

//--make the chars a string
char* stash::returnAsString(){
	add('\0');
	next--; //to overwrite the zero byte next time we add something
	return ch;
}

//--add more chars into the string;
void stash::operator+(char *p){
	insertString(p);
}

//--clear the stash and hold a new string
void stash::operator=(char *p){
	clearStash();
	insertString(p);
}

//--THIS HAS PROBLEMS!!!
stash stash::operator=(stash s){
	clearStash();
	insertString(s.returnAsString());
	return *this;
}

//-- Doesn't work! (NO NEED TO CHECK THIS NOW)
istream& operator>>(istream& stream, stash s){
	for(char ch = stream.get(); ch != '\n' && !stream.fail(); ch = stream.get())
		s.add(ch);
	return stream;
}
#endif

In main(), I am giving the following code:
int main(){
	stash o = "hello world";
	
	
	stash b;
	b = o;
	
	o.show();
	b.show();
	return 0;
}
But I am getting this in the console
[Session started at 2006-05-26 17:56:43 +0300.]
Freeing storage
Freeing storage
currentStorage: 12
current size: 11
Next: 1
hello world
currentStorage: 12
current size: 11
Next: 1
hello world
Freeing storage
C++ tool 3(1296) malloc: ***  Deallocation of a pointer not malloced: 0x3003c0; This could be a double free(), or free() called with the middle of an allocated block; Try setting environment variable MallocHelp to see tools to help debug
Freeing storage
1)Why does this happen? What can I do to fix it?

2)Please if you have recommendations about my code, or any other mistakes I have done, please tell me so, I want to make this class as efficient as I can.

Thank you in advance.
__________________
Project::Soulstorm (personal homepage)
Soulstorm is offline   Reply With Quote