0

我有一个函数,我传递一个指针,然后在函数内部分配内存。

count = rt_band_get_ray(band,x1,y1,x2,y2,&npixels);

这是函数体:

/**
* Processes an array of pixels that fall on the ray from x1,y1 to x2,y2.
 * @param band : band to be processed
 * @param x1 : starting x-cordinate ( 0 based )
 * @param y1 : starting y-cordinate ( 0 based )
 * @param x2 : ending x-crodinate ( 0 based ) 
 * @param y2 : ending y-cordinate ( 0 based )
 * @param npixels : pointer to array of pixels
 * 
 * @return -1 on error 0 for sucess.
 * http://en.wikipedia.org/wiki/Bresenham%27s_line_algorithm
 */
int 
rt_band_get_ray(rt_band band, int x1, int y1, int x2, int y2, rt_pixel *npixels )
{
  int x;
  int y;
  int xinc1;
  int yinc1;
  int xinc2;
  int yinc2;
  int dx;
  int dy;
  int den;
  int num;
  int numadd;
  int curpixel;
  double pixval;
  int isnodata = 0;
  rt_pixel pixel = NULL;
  int count;

  dx = abs( x2 - x1 );
  dy = abs( y2 - y1 );

  x = x1;
  y = y1;

  // considerations for all eight quads

  if ( x2 >= x1 )  // the x-values are increasing 
    {
      xinc1 = 1;
      xinc2 = 1;
    }
  else   // the x-values are decreasing
    {
    xinc1 = -1;
    xinc2 = -1;
    }

  if( y2 >= y1 ) // the y-values are increasing
    {
      yinc1 = 1;
      yinc2 = 1;
    }
  else  // the y-values are decreasing
    {
      yinc1 = -1;
      yinc2 = -1;
    }

  if ( dx >= dy )  // there is at least one x-value for every y-value
    {
      xinc1 = 0;   //don't change x when numerator >= denominator
      yinc2 = 0;   // don't change y for every iteration
      den = dx;
      num = dx / 2;
      numadd = dy;
      count = dx;  // there are more x-values than y-values
    }
  else
    {
      xinc2 = 0;
      yinc1 = 0;
      den = dy;
      num = dy / 2;
      numadd = dx;
      count = dy;
    }

  // allocate array
    if (npixels == NULL)
     *npixels = (rt_pixel) rtalloc(sizeof(struct rt_pixel_t) * count);
    else
     *npixels = (rt_pixel) rtrealloc(*npixels, sizeof(struct rt_pixel_t) * count);
    if (npixels == NULL) {
      rterror("rt_band_get_ray: Unable to allocate memory for ray pixel(s)");
      return -1;
    } 

  for ( curpixel = 0; curpixel < count; curpixel++)  //itterate through all pixels on the line
    {
      // set current pixel to array
      rt_band_get_pixel(band,x,y,&pixval,&isnodata);
      pixel = &((*npixels)[curpixel]);
      pixel->x = x;
      pixel->y = y;
      pixel->nodata = isnodata;
      pixel->value = pixval;

      num += numadd;  // increase the numerator by the top of the fraction
      if ( num >= den )  // check if numerator >= denominator
    {
      num -= den;  // calculate the new numerator value
      x += xinc1;  // change x as appropriate
      y += yinc1;  // change y as appropriate
    }
      x += xinc2;   // change x as appropriate
      y += yinc2;   // change y as appropriate
    }
  return count; // return number of npixel elements.

}

我想我正在失去函数内部分配的内存的范围。并遇到分段错误。在这些线上:

for ( i = 0; i < count; i++ ){
      pixel = &((*npixels)[i]);
      results[i] =  pixel->value;
    }

我是否失去了函数内部分配的数据的范围?如果是这样,我该如何完成,在这个函数内部分配一个数组而不丢失范围?

4

1 回答 1

0

您需要使用指向指针的指针,以便您可以从函数内部更改实际的指针值。你的 func 原型应该看起来像

int rt_band_get_ray(rt_band band, int x1, int y1, int x2, int y2,
                    rt_pixel **p_npixels)

当心 - 函数内对 npixels 的所有引用都应更改为 (*p_npixels),就像代码中的示例行一样:

if (npixels == NULL)
    *npixels = (rt_pixel) rtalloc(sizeof(struct rt_pixel_t) * count);

应该成为

if (*p_npixels == NULL)
    **p_npixels = (rt_pixel) rtalloc(sizeof(struct rt_pixel_t) * count);

并被称为

rt_pixel *npixels;

rt_band_get_ray(band, x1, y1, x2, y2, &npixels);

之后代码应该可以正常工作

于 2013-01-05T10:07:50.797 回答