Program only works in C::B debug mode...

I've been working on this string class for a while now, and I just tried to compile its test file again. My program crashed, so I said "Well, alright, let's look at the assignment operator overload." Looking over it, I see nothing wrong. Finally, I decided to put the code into a project and run the debugger. Once the debugger started, the program ran and exited normally! WTF? Here's my code:

http://pastebin.com/f1QqSRwm
What is the code you were running that crashed it, and where did it crash? And it sounds like memory corruption to me.

Also:
Lines 433-435...Don't do that.
The test code:

1
2
3
4
5
6
7
8
9
10
11
#include <cstdio>
#include "string.h"

int main(){
	string str;
	str="Merry Christmas to you!";
	//tests show that it crashed here vvv
	str.replace("Christmas","Hanukkah");
	printf("String: %s\nUpper: %s\nLower: %s\n",str.c_str(),str.upper().c_str(),str.lower().c_str());
	getchar();
	return 0;}

Why shouldn't I switch characters using an XOR swap?

EDIT: Strange thing, I added a few functions and it started working...
EDIT2: Scratch that, it was just because I used printf("DEBUG") before anything else... Weird stuff.
Last edited on
XOR: http://en.wikipedia.org/wiki/XOR_swap_algorithm#Reasons_for_avoidance_in_practice

This is also interesting: http://big-bad-al.livejournal.com/98093.html

Double check all your pointers, arrays, etc. If you add or remove random stuff that isn't related and suddenly the code works (or doesn't), then it is most likely memory corruption.
string::resizedata is quite confusing. the parameter newsize is not going to be the size of the string, but the number of cells added.
so you end with constructions like this: resizedata(s<<1-datasize). (that is funny, you are trying to optimize a multiplication but you do an unnecessary memcopy) resizedata(((size2-size1)*c)<<1);

I think that countstr could be incorrect (pos should be reset on a mismatch)

also
1
2
//const static unsigned maxint=~0;
maxint = std::numeric_limits<unsigned>::max() // #include <limits> 


Edit: The unnecessary copy occurs in operator=
Last edited on
In string::operator= resizedata(s<<1-datasize);
warning: suggest parentheses around '-' inside '<<'

the arithmetic has higher precedence than shift.

Edit:
Donald Knuth wrote:
We should forget about small efficiencies, say about 97% of the time: premature optimization is the root of all evil
Last edited on
Topic archived. No new replies allowed.