I have a stash class, which will hold an array of characters and will be dynamically expanded or shrink to fit the newly added characters each time (something like STL's string class - only primitive), and I am stuck at a problem I'm having... This is my stash.h code:
In main(), I am giving the following code:
But I am getting this in the console
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.
Code:
//********Created by Sotiriou Christos
//********(A.K.A.) Soulstorm
//******** http://users.forthnet.gr/ath/jonmecos
//Personnal Stash beta1
//This will hold an array of characters and will return it as a string
//Not completed yet. Modify at your own risk.
#ifndef STASH_H
#define STASH_H
#include <iostream>
using std::cout;
using std::cin;
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();
char returnCharacterAtPosition(int i);
int returnCurrentSize(){return currentSize;}
void operator+(char *p);
void operator=(char *p);
stash operator=(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);
}
//--Return a character at a specified position
//--Return NULL if a character doesn't exist at the
//--requested position
char stash::returnCharacterAtPosition(int i){
if(i>currentSize)
return NULL;
return ch[i];
}
//--clear the stash and hold a new string
void stash::operator=(char *p){
clearStash();
insertString(p);
}
//--THIS HAS PROBLEMS WITH MEMORY ALLOCATION!!!
stash stash::operator=(stash s){
clearStash();
insertString(s.returnAsString());
return *this;
}
#endif
Code:
int main(){
stash o = "hello world";
stash b;
b = o;
o.show();
b.show();
return 0;
}
Code:
[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
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.