0

I've got a piece of code, it's purpose is to draw a background image on one of the game levels. For this purpose I create this structure.

typedef struct crate_t {
            int x = 0;
            int y = 0;
            int h = 0;
            int w = 0;
            int type = BACKGROUND;
        }crate;
    

Then in the main function I create a 2D array

crate **Crates = (crate**)malloc(sizeof(crate)*(SCREEN_WIDTH / GrassBlock->w));
for (int i = 0; i <= SCREEN_HEIGHT/GrassBlock->h; i++) {
    Crates[i] = (crate*)malloc(sizeof(crate)*(SCREEN_HEIGHT / GrassBlock->h));
}

and I pass it to the function counter = DrawLevelBG(screen, GrassBlock, Border, Crates);. The problem is that the function causes error. "Access violation writing location." at Obstacles[i][j].x = x;

int DrawLevelBG(SDL_Surface *screen, SDL_Surface *sprite, SDL_Surface *border, crate **Obstacles) {
        int x = 0;
        int y = 0;
        int i = 0;
        int j = 0;
        bool condition = 0;
        while (y < SCREEN_HEIGHT + sprite->h) {
            DrawSurface(screen, sprite, x + (sprite->w / 2), y + (sprite->h / 2));
            if (x >= SCREEN_WIDTH - sprite->w || x == 0 || y == 0 || y >= SCREEN_HEIGHT - sprite->h) {
                DrawSurface(screen, border, x + (sprite->w / 2), y + (sprite->h / 2));
                Obstacles[i][j].x = x;
                Obstacles[i][j].y = y;
                Obstacles[i][j].h = border->h;
                Obstacles[i][j].w = border->w;
                Obstacles[i][j].type = WALL;
                i++;
                if (x >= SCREEN_WIDTH - sprite->w) {
                    y += sprite->h;
                    x = 0;
                    j++;
                    condition = 1;
                }
            }
            if (!condition) {
                x += sprite->w;
            }
            condition = 0;
        }
        return i;
    }

I know that these ones are caused by pointers not pointing actually to anything but I can't understand what's wrong here. Any help would be greatly appreciated. Thanks.

EDIT

I've changed my memory allocation piece of code so it looks like that now:

crate **Crates = (crate**)malloc(sizeof(crate*)*(SCREEN_WIDTH / GrassBlock->w)*(SCREEN_HEIGHT / GrassBlock->h));
for (int i = 0; i <= SCREEN_WIDTH/GrassBlock->w; i++) {
    Crates[i] = (crate*)malloc(sizeof(crate)*(SCREEN_HEIGHT / GrassBlock->h));
}

According to all your replies guys. Unfortunately this doesnt solve the problem. +Important info, the function DrawLevelBG causes ERROR on the first iteration of loop.

Community
  • 1
  • 1
Jkee
  • 1
  • 2
  • 1
    Tag only one language, C is not C++ – Stargateur Jan 09 '18 at 14:56
  • You don't see anything wrong with you using `sizeof(crate)` for *both* allocations? – Some programmer dude Jan 09 '18 at 14:57
  • Have you tried using a debugger? That's the first thing to do when you get errors like this. – underscore_d Jan 09 '18 at 14:59
  • 1
    There's a smell in your for loop for initialization. You allocate `SCREEN_WIDTH / GrassBlock->w` pointers, but to initialize them you iterate `SCREEN_HEIGHT/GrassBlock->h` times... these numbers may not be the same (also you're not allocating pointers, and also this looks like a 2D array, not 3D) – AndyG Jan 09 '18 at 14:59
  • As for your problem, this is the perfect time to [learn how to debug your programs](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/). What are the valid indexes into `Obstacles`? What indexes are you using? – Some programmer dude Jan 09 '18 at 14:59
  • @underscore_d I've got a problem with a debugger, can't use it because I've got "sdl_windows_main.c not found" I'll fix that ASAP and try to debug it. – Jkee Jan 09 '18 at 15:06
  • 1
    By the way, the reason you need to cast those `malloc` calls, and the reason you can use inline initialization of the `crate_t` structure members is because you're programming in *C++!* Either stop doing that and program in C properly, or [get a good beginners C++ book (or two)](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) and learn C++ properly. – Some programmer dude Jan 09 '18 at 15:09
  • 1
    For an array of N elements, what are the valid indexes? Check those loop conditions! – Some programmer dude Jan 09 '18 at 15:26
  • I notice you allocate `SCREEN_WIDTH / GrassBlock->w` pointers, but then initialize `1 + SCREEN_HEIGHT/GrassBlock->h` of them. Is this intentional? – Davislor Jan 09 '18 at 16:20

2 Answers2

2

In the first allocation you create an array from pointers. So you need to allocate memory for pointers:

crate **Crates = (crate**)malloc(sizeof(crate*)*(SCREEN_WIDTH / GrassBlock->w));
0

Thanks for all the help guys. The problem was iterators, not only did I make my 2D array SCREEN_HEIGHT wide and SCREEN_WIDTH high which was the opposite of what I wanted but aswell the iteration in DrawLevelBG was wrong as pointed out. I had to swap my "i" and "j" and make some corrections, so thanks alot Some programmer dude for pointing that out. Thanks alot.

Jkee
  • 1
  • 2