I am getting arbitrarily changing variables, I have a warning for "6011 dereferencing null pointer"

0

My variables seem to change at random. My only lead is "6011 dereferencing null pointer". I am using C++ with allegro on MSVS.

I've tried going through the code line by line and placing break points at common problem areas. I actually might have made it worse at one point where I changed the parameters of grabframe from taking a width and height to taking a sprite and using its width and height. But even before the change the variables were changing randomly

in my header:

//define the sprite structure
typedef struct SPRITE{
    int dir, alive;
    int x, y;
    int width, height;
    int xspeed, yspeed;
    int xdelay, ydelay;
    int xcount, ycount;
    int curframe, maxframe, animdir;
    int framecount, framedelay;
}SPRITE;
SPRITE* player1spr;
BITMAP* player1[121];

in my C++ file that includes the main:

//grabframe function
//takes sprites out of a sprite sheet and stores it in bitmaps
//parameters are:
//BITMAP* source: a bitmap that holds the sprite sheet
//SPRITE* sprite: the sprite that is going to use the frame
//int startx: the start x coordinate of the sprite sheet
//int starty: the start y coordinate of the sprite sheet
//int columns: the amount of columns the sprite sheet has
//int frame the frame thats being grabbed
BITMAP* grabframe(BITMAP* source, SPRITE* sprite, int startx, int starty, int columns, int frame) {
    //create temporary bitmap to hold the sprite
    BITMAP* temp = create_bitmap(sprite->width, sprite->height);
    //find the dimensions of a sprite given the properties of the sprite      sheet
    int tempx = startx + (frame % columns) * sprite->width;
    int tempy = starty + (frame / columns) * sprite->height;
    //blit the sprite onto the sheet
    blit(source, temp, tempx, tempy, 0, 0, sprite->width, sprite->height);
    return temp;
}

in my main:

player1spr = (struct SPRITE*)malloc(sizeof(SPRITE*));
player1spr->maxframe = 121;
player1spr->curframe = 0;
player1spr->width = 600;
player1spr->height = player1spr->width;

// create a bitmap for the player one character
temp1 = (BITMAP*)data[PLAYER1_BMP].dat;
for (player1spr->curframe = 0; player1spr->curframe < player1spr->maxframe; player1spr->curframe++) {
    player1[player1spr->curframe] = grabframe(temp1, player1spr, 0, 0, 11, player1spr->curframe);
}
destroy_bitmap(temp1);

Errrors like this:

Exception thrown at 0x0F81B605 (allegro-4.4.2-monolith-md.dll) in Assignment2 v2.exe: 0xC0000005: Access violation reading location 0x00000010.

I expect my sprite variables to stay the same but they change.

c++
visual-studio
null-pointer
asked on Stack Overflow Jul 15, 2019 by user11788131 • edited Jul 15, 2019 by Geoffrey

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0