SDL question / maybe to do with array subscripting
Hey guys, happy Friday!
I'm working on an animation function using SDL, and I'm experiencing a bizarre problem I don't understand. Here are the details:
Code:
typedef struct {
int index;
unsigned char speed;
unsigned char counter;
SDL_Rect **tiles;
unsigned char totalTiles;
} Animation;
Animation CreateAnimation(SDL_Rect **tiles, unsigned char totalTiles, unsigned char speed) {
Animation a;
a.index = 0;
a.counter = 0;
a.speed = speed;
a.tiles = tiles;
a.totalTiles = totalTiles;
return a;
}
void DrawAnimation(SDL_Surface *image, Animation *a, int x, int y) {
a->counter++;
SDL_Rect *tile; // would be used later, struggling with debuggling. :P lol
for (unsigned char i = 0; i<a->totalTiles; i++) {
if (a->counter == a->speed * i) {
if (i > 0) a->index++;
a->counter = 0;
if (a->index == a->totalTiles) a->index = 0;
tile = a->tiles[i];
if (tile == NULL) printf("BINGO on %d\n", i);
break;
}
}
}
I've confirmed that:
1. a isn't NULL
2. a->index isn't NULL and is always less than a->totalTiles
3. a->tiles isn't NULL
4. nothing afaik in a is NULL
And yet, when I try to do this:
a->tiles[a->index]
The program crashes with code 0xC0000000005 (I may be off a zero or 60). I've confirmed this by checking if it's NULL, and it is. And yet, mysteriously, if I do this:
a->tiles[3] // The max value of a->index
It's fine. while(true) hair--; // :P
I've fought hard against 0xC0000000000000005 many times over, and it's one of those lovely ultra-vague goobers. Its official title is "access violation" and its real meaning is "You goofed with a pointer" (lol). But based on the info above, this scenario defies any kind of deductive reasoning or other human logic. Whatever it is that the computer thinks went NULL, it's obviously correct, but to us humans it's a real hair-jerker of a mystery. Something obviously broke, but I have no idea what/where/how/why. Any thoughts?
Thanks in advance. :)
Funny, I was gonna ask about C debuggers in a new thread later! :D
I'm currently using the CodeBlocks IDE (on Windows 10 x64) and whenever I go to Debug > Start Debugging, it asks me to select a debugger. So I guess it doesn't come with one, which kinda stinks, but I'm honestly more interested in learning how C debuggers work - most of the videos I've seen on the subject and pages I've read talk about debugging C in terms of memory addresses - which is awesome, and I'd love to know how to do, but I'm not there yet. I've dabbled in 6502 Assembly language, and I understand how memory addresses work (and In C/C++ you kinda have to, because "pointers" lol). But even if I found and installed a good debugger, I don't think I'd even know what I'm looking at. Any suggestions in that area?
PS: Sorry for the code tags - the error-checker is buggin' out too (lol) - guess it thinks there's code in this paragraph someplace (??)