Jkee
Jkee

Reputation: 1

Unable to read memory. Can't find what's wrong

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.

Upvotes: 0

Views: 2693

Answers (2)

Jkee
Jkee

Reputation: 1

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.

Upvotes: 0

pagdot
pagdot

Reputation: 155

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));

Upvotes: 2

Related Questions