Loop crash...

Hi everybody and, I got a trouble about implementing a reversed loop.
It looks like :
1
2
3
4
unsigned char size = Getsize(); 
for (; size >= 0;size--){
Class[size]->free(); delete Class[size];
}

I tried to fix it many times but I couldn't... :(
Does anybody know?
Last edited on
Are the objects in the Class container dynamically allocated pointers? If you're using . to access members, then their not even pointers to begin with. How does that code even compile?
The type of size is unsigned char, that means it can never be less than zero.
The loop continues as long as size >= 0.

g++ even mentions this:
test.cc:5:16: warning: comparison is always true due to limited range of data type [-Wtype-limits]


The contents of the loop appear very questionable as well.
This is a demonstration example. Actually, the loop causes crash. Why?
Thanks Cubbi! The problem now has been solved. Actually It gives me a headache...
Usually a function like Getsize() provides the size not the last index of an array. I'd expect it like so:
1
2
3
4
unsigned char size = Getsize(); 
for (; size > 0;size--){
Class[size - 1]->free(); delete Class[size - 1];
}
Hmm...Currently I'm still using the code :
 
for (unsigned char i = 0; i < size; i++){[...]}

The code (improved) :
for(--size;;){[...](!--size)break;}
Output debug size example :
Size : 8
Size : 255
Size : .... // Crash!!!!

?????????????????????????????????????????????????????????????????????????????????????????????????????????????????????

Perhaps this is a core error C++ itself???
Last edited on
why do you use unsigned char? int is usually faster and better

The code (improved) :
That's not improved that will horribly obfuscate your code

Size : .... // Crash!!!!
What is ....? Why crash?

You know that i will never be >= 255?
Topic archived. No new replies allowed.