View Full Version : Templates :: Step through fine in Debug, but crash when run in anger...?

2005-12-17, 07:24
Hi there..

I am experimenting with some template stuff in Xcode..

I have a strange problem...

When I step through them using Debug everything is fine.. The IO log window outputs what I expect, and the trace of the code is faultless.. Variables and pointers all behave impeccably..

However, when I run it 'for real' in Xcode I get exceptions...

Anyone know of any quirks with templates and Xcode?

It's pretty basic stuff. Nothing fancy.

I have tried all the dumb stuff.. Cleaning, reloading everything etc. etc. :(

2005-12-17, 09:51
Er, do you mean project templates, or C++ templates?

2005-12-17, 10:04
Er, do you mean project templates, or C++ templates?

Hey buddy....

Actually I solved it about 10 minutes ago..

I was talking about C++ templates.
It was very wierd.. One of those 1 in a million bugs.. A pointer in a routine about half a page up was overwriting the end of some string space and randomly affecting the code, causing crashes... But it only manifested itself after I had included a whole load of templates for repetative search functions..

I must be using so many levels of indirection that the compiler didn't pick up the fact that I was writing wildy outside the boundaries of my data... Strange...

But that'll teach me to try and be clever...

2005-12-17, 10:27
The compiler won't. :)

C++ doesn't have any concept of writing past the end of an array, you're welcome to do it. Heck, it doesn't have the concept of writing before the beginning of the array either. Had a boss do the following once as a down and dirty 'struct' for a three dimensional array:

const int LEN = X * Y * Z;
int a[LEN + 3];
a = a +3;

a[0]...a[LEN] acted as expected.
a[-3]...a[-1] were storage for the lengths of the each axis. So you always had the proper sizes being carried around with it. In theory.

Personally, I thought he was bonkers, but it worked pretty well... until the day a student misread the documentation... oy.

The point is, you can have an array that's N long, and access [N*10] if you want, or [-N]. C/C++ won't care, and won't stop you.

Glad you got it figured out. :)