c++ - 我正在任意更改变量,出现“6011 取消引用空指针”的警告
问题描述
我的变量似乎是随机变化的。我唯一的线索是“6011 取消引用空指针”。我在 MSVS 上使用 C++ 和 allegro。
我尝试逐行浏览代码并在常见问题区域放置断点。实际上,在我将grabframe的参数从采用宽度和高度更改为采用精灵并使用其宽度和高度时,实际上可能会变得更糟。但即使在变化之前,变量也在随机变化
在我的标题中:
//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];
在我的 C++ 文件中,其中包括主要内容:
//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;
}
在我的主要:
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);
像这样的错误:
Exception thrown at 0x0F81B605 (allegro-4.4.2-monolith-md.dll) in Assignment2 v2.exe: 0xC0000005: Access violation reading location 0x00000010.
我希望我的精灵变量保持不变,但它们会改变。
解决方案
推荐阅读
- css - 添加带有路径的SVG到css类不起作用
- javascript - 我如何将这些数据放在 html 中?
- typescript - React Native / typescript中的自定义警报
- plot - 如何在 Julia Plots Backend PGFPlotsX 中获得完整的黑色刻度线?
- python-3.x - 如何使用循环按部分拆分多行列表
- c++ - 如何将 std::is_convertible_v 应用于一组可变参数?
- java - 无法获得正确的手机内存大小(内部存储大小、总存储大小和总外部大小)
- json - 如何在兄弟对象上按键提取对象?
- javascript - 如何让代码在出错后继续执行 [JS]
- r - 如何从 PDF 中干净地提取多页表?