简体   繁体   中英

Random memory acces violation

I've this sample code :

map *d;

i = MAP_SIZE;
j = sizeof(map);
d = malloc(MAP_SIZE);

if (d == NULL) {
    exit(EXIT_FAILURE);
}

dest.x = dest.y = 0;

for (i = 0; i < WINDOW_HEIGHT / AREA_RESOLUTION; i++)
{
    for (j = 0; j < WINDOW_WIDTH / AREA_RESOLUTION; j++)
    {
        k = GetAreaPos(j, i);
        Area = d[k];
        dest.x = j*AREA_RESOLUTION;
        dest.y = i*AREA_RESOLUTION;
        if (Area->landType == DESTRUCTIBLE_BRICK) {      //GOT ERROR HERE
            SDL_QueryTexture(Game_Texture->Explodable, NULL, NULL, &dest.w, &dest.h);
            SDL_RenderCopy(Renderer, Game_Texture->Explodable, NULL, &dest);
        }
        if (Area->landType == INDESTRUCTIBLE_BRICK) {
            SDL_QueryTexture(Game_Texture->Solidblock, NULL, NULL, &dest.w, &dest.h);
            SDL_RenderCopy(Renderer, Game_Texture->Solidblock, NULL, &dest);
        }
    }
}
free(d);

MAP_SIZE = sizeof(map)

I use Visual Studio, when i run debug without breakpoints, I've always got a memory access violation in the first loop turns.

with breakpoints and slowly/constant F5 push, no errors, the loop finnish as well...

I don't understand why this error appears, the 2 loops havent 100 turns in this example, the violation is totally random, sometimes in the 5th turn, sometimes in the 90th...

This sample of code is running in another external loop, and when the first execution works fine, the others never has any violation error.

UPDATE 1

Now, i use as well my d variable and exit if malloc() return NULL . The GetAreaPos() return correct value, but the memory violation always appaers on the first if condition at randomly between 2nd and 6th turn of second FOR loop in tests

Header definitions :

#define MAP_SIZE sizeof(map)
#define AREA_SIZE sizeof(union area)
#define AREA_RESOLUTION 64
#define MAP_WIDTH 10//28
#define MAP_HEIGHT 10//14

//#pragma pack(1)
typedef enum {
    EMPTY = 00,
    INDESTRUCTIBLE_BRICK = 10,
    DESTRUCTIBLE_BRICK = 11
} landType;

typedef enum {
    BONUS_BOMB_SCOPE = 000,
    MALUS_BOMB_SCOPE = 001,
    BONUS_BOMB_AMOUNT = 010,
    MALUS_BOMB_AMOUNT = 011,
    BONUS_PLAYER_SPEED = 100,
    MALUS_PLAYER_SPEED = 101,
    NO_BONUS_MALUS = 110,
    NO_MALUS_BONUS = 111,
} bonusType;

union area {
    struct {
        bool inFire :4;
        landType landType :8;
        bool presenceBomb :4;
        bool presenceBonus :4;
        bonusType typeBonus :12;
    };
    char c;
};
//#pragma pack(0)

typedef union area map[MAP_WIDTH * MAP_HEIGHT];

you first declare a pointer map *d; and allocate memory for it d = malloc(MAP_SIZE); but you ignore the return value...

if (d == NULL) means the malloc didn't work and this (Area->landType == DESTRUCTIBLE_BRICK) would cause UB [because Area = Map[k]; ] but your code does not protect against that and that could crash

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM