Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 返回对动态生成的二维数组的引用 您应该分配指针大小,而不是结构大小 CGPointMake(x,y)在堆栈而不是堆中创建结构。感谢@BaVariable指出这不是事实。见下面的评论。:)_C++_Objective C_C_Multidimensional Array_Malloc - Fatal编程技术网

C++ 返回对动态生成的二维数组的引用 您应该分配指针大小,而不是结构大小 CGPointMake(x,y)在堆栈而不是堆中创建结构。感谢@BaVariable指出这不是事实。见下面的评论。:)

C++ 返回对动态生成的二维数组的引用 您应该分配指针大小,而不是结构大小 CGPointMake(x,y)在堆栈而不是堆中创建结构。感谢@BaVariable指出这不是事实。见下面的评论。:),c++,objective-c,c,multidimensional-array,malloc,C++,Objective C,C,Multidimensional Array,Malloc,执行所需操作的代码: - (CGPoint *)point { return positions[arc4random() % rows]; } 无符号整数行=10; 无符号整数列=10; CGPoint**points=malloc(行*sizeof(CGPoint*)); 对于(int r=0;r

执行所需操作的代码:

- (CGPoint *)point {
    return positions[arc4random() % rows];
}
无符号整数行=10;
无符号整数列=10;
CGPoint**points=malloc(行*sizeof(CGPoint*));
对于(int r=0;r

警告:当您不再需要2D阵列时,必须记住释放它。一种方法是将其包装在Obj-C包装器中,并在
init
dealloc
中进行包装,使用
CGPointMake()
没有问题,因为结构实例被分配到了适当的(堆)位置。事实上,
CGPointMake()
是一个内联函数,除非程序是用
-O0
构建的,否则根本不会有堆栈分配。我不知道这一点,谢谢你指出。我做了必要的调整。:)
- (CGPoint *)point {
    return positions[arc4random() % rows];
}
unsigned int row = 10;
unsigned int column = 10;
CGPoint **points = malloc(row * sizeof(CGPoint *));
for (int r = 0; r < row; ++r) {
    points[r] = malloc(column * sizeof(CGPoint));
    for (int c = 0; c < column; ++c) {
        points[r][c].x = 0.0;
        points[r][c].y = 0.0;
    }
}