将 malloc 替换为数组
Replace malloc with array
我有一个图像处理程序(canny-edge-detection(,这是代码的一部分:
short int **magnitude;
int rows=320, cols=240;
//Allocate memory to store the image, warning if not successful
if((*magnitude = (short *) calloc(rows*cols, sizeof(short))) == NULL){
//some warning
}
我想使用数组来避免动态分配内存,因为这在我将要运行代码的系统中是不可行的。在这种情况下,数组的大小是多少?我假设
short int magnitude_arr[76800]
但是,输出图像被切成两半。
您的声明将为您提供一个具有正确大小的静态大小数组。 如果您的程序不再工作,则错误在其他地方。
如果您打算使用静态大小,您可能会考虑使用
std::array<short, 76800u> magnitude;
或
std::vector<short> magnitude(rows * cols);
如果相反,行和列可能会更改以使运行时大小动态。
如果需要指向存储数据的指针,这两个类都具有data()
成员函数。
这应该做得很好。
const int rows=320;
const int cols=240;
short int magnitud_arr[rows * cols];
相关文章:
- 按平均值替换数组中的元素
- 如何将字节数组元素替换为修改的十六进制 ASCII 符号?
- C++ 替换字节数组中项的顺序
- 使用基于数组和范围的 For 循环替换一些基本代码行
- 为什么数组中对象的析构函数在被另一个对象替换时不被调用?
- 替换 2D 数组中的字符
- 如何在给定文本中用字符数组替换符号
- 用现代C++STL数据结构替换旧的C风格数组
- 查找并替换数组的特定元素
- 如果在执行过程中替换二进制文件,"const"数组是否驻留在内存中?
- 我的函数不会用" "替换".",也不会按预期显示数组
- 用分层结构和类替换数组可以吗?
- 如何替换QT QML中的变体数组文本
- 为什么数据中的随机部分替换了曾经存储在我的角色数组中的其他数据
- C++ 在不使用 C 字符串库函数的情况下替换数组中的字符
- 用另一个1D数组替换2D数组的整个行
- 在函数的输入参数中将 char 数组替换为 std::string
- 用另一个字符数组替换一个字符数组
- 在哪里可以找到如何用函数数组替换"switch"运算符的示例?
- 将内置数组替换为std/tr1/boost::数组是否总是安全的?