【问题标题】:Incorrect values are being assigned - C分配了不正确的值 - C
【发布时间】:2020-04-17 15:14:15
【问题描述】:

这个问题很难在网站上找到。我试过但空手而归,如果之前有人问过类似的问题,我很抱歉。

基本上,我有一段带有两个 for 循环的代码。我相信它的目的对于我提出的问题是不必要的。无论如何,第二次在我的代码中调用此函数(第一次运行良好)分配给变量 x 和 y 的值不正确


void draw_sprite(Sprite *sp){

 printf("outside cicle\nx: %d\ny: %d\n", (sp->x), (sp->y));

 int y;
 int x;

 int p = 0;
 for(y = sp->y; y < (sp->y + sp->height); y++){
   for(x = sp->x; x < (sp->x + sp->width); x++){
     printf("inside cicle\nx: %d\ny: %d\n", x, y);
     vg_paint_pixel(x, y, sp->map[p]);
     p++;
   }
 }

 return;
}

程序打印出来:

outside cicle
x: 34
y: 30
inside cicle
x: 0
y: 136663040

如您所见,在分配之前,x 和 y 的值分别为 34 和 30。但赋值后,变量 x 和 y 变为 0 和 136663040。提前致谢。

sp的定义:

typedef struct {
  int x,y;             
  int width, height;   
  int xspeed, yspeed;  
  char *map;           
} Sprite;

这个函数的参数(draw_sprite)是一个通过以下方式创建的精灵:

Sprite *sp;

sp = create_sprite(xpm, xi, yi, 0, 0);

这些值是通过终端给出的,我使用的 xpm 地图可以正常工作,除非我必须移动精灵。 xi 和 yi 的值都是 30。这里是函数 create_sprite:

Sprite * create_sprite(xpm_map_t xpm, int x, int y, int xspeed, int yspeed){

  Sprite *sp = (Sprite *) malloc ( sizeof(Sprite));

  if(sp == NULL){
    return NULL;
  }

  xpm_image_t img;

  sp->map = (char*)xpm_load(xpm, XPM_INDEXED, &img);

  if(sp->map == NULL){
    free(sp);
    return NULL;
  }

  sp->width = img.width;
  sp->height = img.height;
  sp->x = x;
  sp->y = y;
  sp->xspeed = xspeed;
  sp->yspeed = yspeed;

  return sp;
}

还有,用于编译和生成上述错误:

int(video_test_move)(xpm_map_t xpm, uint16_t xi, uint16_t yi, uint16_t xf, uint16_t yf,
                     int16_t speed) {

  Sprite *sp;

  sp = create_sprite(xpm, xi, yi, 0, 0);

  if (sp == NULL) {
    printf("Error creating sprite.\n");
    return 1;
  }

  sp->xspeed = speed;

  draw_sprite(sp);

  if (speed > 0) {

            while(sp->x != xf || sp->y != yf)){
              destroy_sprite(sp);
              sp->x += sp->xspeed;
              sp->y += sp->yspeed;
              draw_sprite(sp);
            }      

  }

return 0;
}

最后,要让代码工作,还有destroy_sprite:

void destroy_sprite(Sprite *sp){
  if(sp == NULL){
    return;
  } 

  if(sp->map){
    free(sp->map);
  }

  free(sp);
  sp = NULL;
}

【问题讨论】:

  • 你能显示显示sp定义的代码吗?在不知道sp-&gt;widthsp-&gt;height 有什么类型或值的情况下,这有点难以帮助调试。因为这不是一个最低限度的工作示例:stackoverflow.com/help/minimal-reproducible-example
  • 感谢您的编辑,但如果您还可以显示您正在调用 draw_sprite 的具体参数,并展示手头的错误,那将不胜感激!
  • 没问题,谢谢你,很抱歉没有把它放在首位。
  • NoHoly,了解@kopecs 要求的代码的哪些部分非常简单:附加的代码可以编译吗?不,所以添加最少的代码以使其编译,并带有一个基本的 main 来显示导致问题发生的 Sprite 结构值。
  • @NoHoly 根据您迄今为止发布的内容,我推测您的错误是您在sp-&gt;heightsp-&gt;width 中拥有的值的结果@(因此,通过代理)@ 987654335@ 不是您所期望的,但我无法证实这一点。如果您可以在本质上为我们硬编码一个带有值(不是预期的,而是实际观察到的)的结构,那就太好了。您可以通过调试器(如gdb)或打印出来观察这些值。

标签: c for-loop int assign


【解决方案1】:

根据 cmets,具体问题听起来像是在

while(sp->x != xf || sp->y != yf)){
    destroy_sprite(sp);
    sp->x += sp->xspeed;
    sp->y += sp->yspeed;
    draw_sprite(sp);
}

也就是说,在 free 之后使用 sp 会导致您遇到未定义的行为。这导致(在您的情况下)您读取垃圾值,从而导致您的 for 循环中出现意外的迭代量。

【讨论】:

  • 没错!非常感谢!
猜你喜欢
  • 2017-06-06
  • 1970-01-01
  • 2019-12-26
  • 1970-01-01
  • 2012-11-06
  • 1970-01-01
  • 2014-11-15
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多