Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old May 26th, 2006, 4:32 PM   #11
Soulstorm
Hobbyist Programmer
 
Soulstorm's Avatar
 
Join Date: Jan 2006
Location: Menidi, Athens, Greece
Posts: 239
Rep Power: 3 Soulstorm is on a distinguished road
Quote:
I'd also use C++ strings, but I don't know if you are prohibited to do that or anything. C++ strings have their own memory management, which is ideal.
I am not prohibited to anything, this thing is for my satisfaction only. I like C++ strings, I use them extensively, but I wanted to create a class that 'simulates' a string, just for the sake of it.

As for the header files, I have already made all these things, and had created separate files, but until my problem was fixed, I thought it would be good to put all those things into one file, for the users in here to read it all-in-one. Thanks for the advice anyway.
__________________
Project::Soulstorm (personal homepage)
Soulstorm is offline   Reply With Quote
Old May 26th, 2006, 4:42 PM   #12
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
Quote:
So then the following would function correctly:
Nopey.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Old May 26th, 2006, 4:46 PM   #13
Soulstorm
Hobbyist Programmer
 
Soulstorm's Avatar
 
Join Date: Jan 2006
Location: Menidi, Athens, Greece
Posts: 239
Rep Power: 3 Soulstorm is on a distinguished road
Quote:
Originally Posted by nnxion
A side question: I see Soulstorm doing:
void stash::clearStash(){
	currentSize = 0;
	currentStorage = 0;
	ch = new char [0];
	next = currentStorage - currentSize;
}
So he doesn't delete [] anything, will new do a resize next time it's called (like realloc)?
That is exactly what I have noticed right now... you beat me to it. The problem is, that when I try to do this:
//--reset the stash
void stash::clearStash(){
	delete [] ch;
	ch = new char [gIncrement];
	currentSize = 0;
	currentStorage = gIncrement;
	next =  gIncrement - currentSize;
}
and this main:
int main(){
	stash o("hello world");
	
	
	stash b;
	b = o;
	
	o.show();
	b.show();
	return 0;
}
I get this error:
Quote:
C++ tool 3(1885) malloc: *** Deallocation of a pointer not malloced: 0x8fe067a8; 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
currentStorage: 12
current size: 11
Next: 1
hello world
currentStorage: 12
current size: 11
Next: 1
hello world
I thought this method (deleting and reallocatind 'ch') worked out quite well in inflate()...

What went wrong?
__________________
Project::Soulstorm (personal homepage)
Soulstorm is offline   Reply With Quote
Old May 26th, 2006, 6:07 PM   #14
Soulstorm
Hobbyist Programmer
 
Soulstorm's Avatar
 
Join Date: Jan 2006
Location: Menidi, Athens, Greece
Posts: 239
Rep Power: 3 Soulstorm is on a distinguished road
edit: I found it. It was stupid. I think I will turn off the computer now...
__________________
Project::Soulstorm (personal homepage)
Soulstorm is offline   Reply With Quote
Old May 26th, 2006, 6:15 PM   #15
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
Ouzo time, eh?
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Old May 26th, 2006, 6:24 PM   #16
Soulstorm
Hobbyist Programmer
 
Soulstorm's Avatar
 
Join Date: Jan 2006
Location: Menidi, Athens, Greece
Posts: 239
Rep Power: 3 Soulstorm is on a distinguished road
Quote:
Ouzo time, eh?
seems you know a bit about greek customs...
__________________
Project::Soulstorm (personal homepage)
Soulstorm is offline   Reply With Quote
Old May 26th, 2006, 6:43 PM   #17
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
Beats the hell out of Miller!
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Old May 26th, 2006, 6:51 PM   #18
The Dark
Expert Programmer
 
Join Date: Jun 2005
Posts: 824
Rep Power: 4 The Dark is on a distinguished road
Quote:
Originally Posted by Soulstorm
edit: I found it. It was stupid. I think I will turn off the computer now...
Can you post what it was so that other people searching the forum can see the answer?
The Dark is offline   Reply With Quote
Old May 27th, 2006, 2:09 AM   #19
Soulstorm
Hobbyist Programmer
 
Soulstorm's Avatar
 
Join Date: Jan 2006
Location: Menidi, Athens, Greece
Posts: 239
Rep Power: 3 Soulstorm is on a distinguished road
Of course. I don't have the code in front of me, but i remember that the problem resided in the clearstash() function. When I was deleting the 'ch' pointer, I didn't make sure wether that pointer was allocated or not. The result was in some occasions the compiler to give me warnings and errors (when the 'clearstash()' function was called before a string allocation was made).

The solution was to pass the 'ch' pointer a zero value at the beginning (in my constructors), then with an 'if' statement, make sure every time I call 'clearstash()' the 'ch' pointer is allocated before deallocation. That could be done like this, if I remember correctly:

void stash::clearStash(){
	if(ch)
		delete [] ch;
	ch = new char [gIncrement];
	currentSize = 0;
	currentStorage = gIncrement;
	next =  gIncrement - currentSize;
}
...supposing we have set the 'ch' as 0 at the constructors.
__________________
Project::Soulstorm (personal homepage)

Last edited by Soulstorm; May 27th, 2006 at 2:23 AM.
Soulstorm is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 4:10 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC