简体   繁体   English

随机存储器访问冲突

[英]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) MAP_SIZE = sizeof(地图)

I use Visual Studio, when i run debug without breakpoints, I've always got a memory access violation in the first loop turns. 我使用Visual Studio,当我在没有断点的情况下运行调试时,总是在第一次循环中遇到内存访问冲突。

with breakpoints and slowly/constant F5 push, no errors, the loop finnish as well... 具有断点和缓慢/恒定的F5推入,没有错误,循环也完成了...

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... 我不明白为什么会出现此错误,在此示例中,两个循环没有100转,违反是完全随机的,有时在第5轮,有时在第90轮...

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 更新1

Now, i use as well my d variable and exit if malloc() return NULL . 现在,我也使用我的d变量, 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 GetAreaPos()返回正确的值,但在测试中第二个FOR循环的第2轮到第6轮之间随机发生的第一个if条件下,总是会发生内存冲突

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; 您首先声明一个指针map *d; and allocate memory for it d = malloc(MAP_SIZE); 并为其分配内存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]; if (d == NULL)意味着malloc不起作用,并且这(Area->landType == DESTRUCTIBLE_BRICK)将导致UB [因为Area = Map[k]; ] but your code does not protect against that and that could crash ],但是您的代码无法避免这种情况,并且可能导致崩溃

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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