Loop crash...
Dec 13, 2012 at 11:23pm UTC
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 Dec 13, 2012 at 11:36pm UTC
Dec 13, 2012 at 11:32pm UTC
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?
Dec 13, 2012 at 11:33pm UTC
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.
Dec 13, 2012 at 11:35pm UTC
This is a demonstration example. Actually, the loop causes crash. Why?
Dec 13, 2012 at 11:39pm UTC
Thanks Cubbi! The problem now has been solved. Actually It gives me a headache...
Dec 14, 2012 at 8:08am UTC
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 ];
}
Dec 14, 2012 at 10:21am UTC
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 Dec 14, 2012 at 10:21am UTC
Dec 17, 2012 at 3:12pm UTC
why do you use
unsigned char
?
int
is usually faster and better
That's not improved that will horribly obfuscate your code
What is
....
? Why crash?
You know that
i
will never be >= 255?
Topic archived. No new replies allowed.