Quote:
|
When you copy that, you make a new variable, you make a new pointer to different dynamic memory, you duplicate the contents of the first dynamic memory into the second. A true, deep copy, not a copy of a reference. Then you can delete either, or both.
|
I wish I knew how to do that... I tried altering my stash.h this way:
#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;}
int returnCurrentStorage(){return currentStorage;}
int returnNext(){return next;}
void operator+(char *p);
void operator=(char *p);
void 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!!!
void stash::operator=(stash s){
int i;
delete [] ch;
char *temp = new char [s.returnCurrentSize()];
for(i=0; i<s.returnCurrentSize(); i++){
temp[i] = s.returnCharacterAtPosition(i);
}
next = s.returnNext();
currentStorage = s.returnCurrentStorage();
currentSize = s.returnCurrentSize();
ch = temp;
}
#endif I get this result:
Quote:
[Session started at 2006-05-26 19:55:16 +0300.]
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(1788) malloc: *** Deallocation of a pointer not malloced: 0x3003b0; 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
C++ tool 3 has exited with status 0.
|
which is just like before.
Dawei, could you (or anyone else) provide me with a simple code example of what should I do (not the whole code) because I really don't seem to be able to fix this annoying thing.