Thread: SDL question / maybe to do with array subscripting

  1. #1
    Registered User
    Join Date
    Dec 2018
    Posts
    8

    Question 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.

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    4,456
    Have you run the program with your debugger? Your debugger should be able to tell you exactly where it detects the problem and it should also allow you to view the contents of the variables at the time of the crash.

  3. #3
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,728
    a->tiles may not be NULL, but are you sure it was configured correctly? It may not have an a->index element, for some reason...
    Devoted my life to programming...

  4. #4
    Registered User
    Join Date
    Dec 2018
    Posts
    8

    Wink 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 (??)
    Last edited by Salem; 03-01-2019 at 10:29 PM. Reason: Fixed code tags

  5. #5
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,728
    If you downloaded "codeblocks with MinGW", it should have the debugger preinstalled. Just point codeblocks to the "gdb.exe" executable. If not, what system have you installed (MinGW, Cygwin, msys)?
    Devoted my life to programming...

  6. #6
    Registered User
    Join Date
    Dec 2018
    Posts
    8
    EDIT: Turns out there were a few problems: (1) I had set totalTiles to 1, cuz I got my parameters (total tiles not speed) backwards. And (2) The struct had an array of SDL_Rect POINTERS, which was wrong because I passed it an array of SDL_Rect structs! idk why I thought I needed to do that, but it's one of those "duh" moments. So the original problem is solved, and thanks for all your help!

    Now back to the debugger thing, I went to Debug, Active debuggers, then checked "CDB/GDB debugger: default (cuz yes I stuck with MinGW). But then when I try to run it, I get "ERROR: You need to specify a debugger program in the debuggers's settings.
    (For MinGW compilers, it's 'gdb.exe' (without the quotes))
    (For MSVC compilers, it's 'cdb.exe' (without the quotes))"
    This kinda suggests there's some kind of component disconnect, like the debugger has separate settings independent of CodeBlocks, or it's something under the hood of CodeBlocks that doesn't "see" the debugger/there's somewhere else they need to be reconnected besides just setting "active debuggers"... so...? Again, thanks for all the help on these questions.
    Last edited by Geek on Skates; 03-01-2019 at 07:05 PM. Reason: Problem #1 solved :)

  7. #7
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,728
    Goto Settings->Debugger...->GDB/CDB debugger->Default and set the executable path.
    Devoted my life to programming...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 01-10-2016, 01:23 AM
  2. Overloading Subscripting Operators?????
    By sveyda in forum C++ Programming
    Replies: 6
    Last Post: 11-10-2014, 10:20 AM
  3. Replies: 12
    Last Post: 11-21-2010, 09:46 AM
  4. clean array of blanks, array subscripting
    By kocika73 in forum C Programming
    Replies: 6
    Last Post: 02-19-2005, 03:18 PM
  5. Subscripting an array
    By RedZippo in forum C Programming
    Replies: 2
    Last Post: 01-06-2004, 06:34 PM

Tags for this Thread