c - c 函数结束后数组被释放?

标签 c scope postgis

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

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;
    }

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

最佳答案

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

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);

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

关于c - c 函数结束后数组被释放?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14170625/

相关文章:

javascript - 如何在 .then() 链中访问之前的 promise 结果?

postgresql - 输入几何具有未知(0)几何(尽管使用了 st_transform)

java - 使用 JFileChooser - 访问所选文件

c - 如何在 C 中关闭 stdout 和 stderr?

c - 为什么 fread 有时会遇到 "Bad file descriptor"?

c - 定义 (1 << 31) 或使用 0x80000000?结果不同

c++ - 我在哪里可以为我的 C++ 项目找到一个好的 Scope Guard 实现?

sql - 如何在postgis中从另一个包含field_ID和lat long point的表中创建一个带有几何多边形和field_ID的表

postgresql - PostGIS 错误 : type "geography" does not exist

转换和字节顺序