我有一个函数,我传递一个指针然后在函数内部分配内存。
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;
}
我是否在功能内部分配的数据上放弃了范围?如果是这样我该如何完成,在这个函数中分配一个数组而不会失去范围?
答案 0 :(得分: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);
之后代码shoudl正常工作