繁体   English   中英

在结构中传递指针到指针时EXC_BAD_ACCESS?

[英]EXC_BAD_ACCESS when passing a pointer-to-pointer in a struct?

我在结构中有一个CGPoints的c数组。 添加另一个CGPoint时,我需要替换此数组。 我发誓我做对了,它似乎工作了好几次,但最终我会得到一个EXC_BAD_ACCESS 我想念什么?

这是结构,我已将其删除,以删除许多不相关的项目。

typedef struct{
    CGPoint **focalPoints;
    NSUInteger focalPointCount;
    CGRect boundingRect;
}FocalPoints;

这是我如何初始化它:

CGPoint *fPoints = (CGPoint *)malloc(sizeof(CGPoint));
FocalPoints focalInfo = {&fPoints, 0, rect};

请注意,通过引用将focalInfo传递给另一个函数,例如: anotherFunction(&focalInfo)

现在,这里是将Points数组替换为新数组的函数:

void AddFocalPoint (CGPoint focalPoint, FocalPoints *focal){
    if (focalPoint.x == CGFLOAT_MAX) return;
    if (!CGRectContainsPoint(focal->boundingRect, focalPoint)) return;
    int origCount = focal->focalPointCount;
    int newCount = origCount + 1;
    CGPoint *newPoints = (CGPoint *) malloc((newCount) * sizeof(CGPoint));
    for (int i = 0; i < newCount; i++)
        newPoints[i] = (i < origCount) ? *focal->focalPoints[i] : focalPoint; //error occurs here
    free(*focal->focalPoints);
    *focal->focalPoints = newPoints;
    focal->focalPointCount = newCount;
}

EXC_BAD_ACCESS错误在上面的代码在第8行上发生: newPoints[i] = (i < origCount) ? *focal->focalPoints[i] : focalPoint; newPoints[i] = (i < origCount) ? *focal->focalPoints[i] : focalPoint; 那我到底在做什么错?

这有点远,但是*focal->focalPoints[i]可能存在运算符优先级的问题。 您是否尝试根据要达到的目的添加括号?

认为问题出在哪里,将GCPoint *fPoints分配为&fPoints评估&fPoints地址的地址...一旦函数退出,该地址就不再有效。

(使用malloc很好地分配它指向的数据。)

除了我在评论中提出的使用链接列表/ NSMutableArray建议之外,我的另一建议是使用realloc()而不是不断使用malloc() ,手动复制,然后free()进行旧分配。

void * realloc(void *ptr, size_t size);
realloc()函数尝试将ptr指向的分配大小更改为size ,然后返回ptr 如果没有足够的空间来扩大ptr指向的内存分配,则realloc()创建一个新分配,复制ptr指向的旧数据以适应新分配的数量,释放旧分配,然后返回指向已分配内存的指针。

这几乎就是您正在执行的操作,但是您可以让该库为您处理。

(我还谦恭地建议用的是“焦点”略少来命名变量的函数?)(另外还有,我不是真正的原因明确focalPoints在你的结构是一个指针到指针。你只是想要一个结构数组-单个指针应该没问题。)

考虑以下(有点广泛)重写: 希望它对您有所帮助。

typedef struct{
    CGPoint *points;    // Single pointer
    NSUInteger count;
    CGRect boundingRect;
} FocalPoints;

// Renamed to match Apple's style, like e.g. CGRectIntersectsRect()
void FocalPointsAddPoint (FocalPoints *, CGPoint);

void FocalPointsAddPoint (FocalPoints *f, CGPoint thePoint){
    if (thePoint.x == CGFLOAT_MAX) return;
    if (!CGRectContainsPoint(f->boundingRect, thePoint)) return;
    NSUInteger origCount = f->count;    // |count| is typed as NSUInteger; |origCount|
    NSUInteger newCount = origCount + 1;    // and |newCount| should be consistent
    // Greatly simplified by using realloc()
    f->points = (CGPoint *) realloc(f->points, newCount * sizeof(CGPoint));
    (f->points)[newCount-1] = thePoint;
    f->count = newCount;
}

int main(int argc, const char * argv[])
{

    @autoreleasepool {
        // Just for testing; any point should be inside this rect
        CGRect maxRect = CGRectMake(0, 0, CGFLOAT_MAX, CGFLOAT_MAX);
        // Can initialize |points| to NULL; both realloc() and free() know what to do
        FocalPoints fp = (FocalPoints){NULL, 0, maxRect};
        int i;
        for( i = 0; i < 10; i++ ){
            FocalPointsAddPoint(&fp, CGPointMake(arc4random() % 100, arc4random() % 100));
            NSLog(@"%@", NSStringFromPoint(fp.points[i]));
        }

    }
    return 0;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM