【发布时间】:2016-09-01 07:28:09
【问题描述】:
我正在编写一个程序来使用存储在源数组 (hist) 中的数据的计算来创建灰度图像 (image)。对图像调用 calloc 后,源数组中存储的数据重置为零。
func1(){
float * hist = (float *) calloc(256, sizeof(float));
// operation to populate 'hist'
for...{
for...{
hist.....
}
}
hist2img(hist);
free(hist);
return 0;
}
hist2img(hist){
cout << "-> " << hist [4 * 250] << endl;
unsigned char * image = (unsigned char *) calloc(256 * 256, sizeof(unsigned char));
cout << "-> " << hist [4 * 250] << endl;
free(image);
return 0;
}
输出是:
-> 0.997291
-> 0
数据会发生什么变化? hist 中的所有元素在 calloc 指令后均为 0。我需要将image 初始化为0。
--(~$)--> gcc --version
gcc (Ubuntu 5.4.0-6ubuntu1~16.04.2) 5.4.0 20160609
--(~$)--> uname
Linux 4.7.2-040702-generic x86_64 x86_64 x86_64 GNU/Linux
【问题讨论】:
-
hist [4 * 250]指针运算在这里出错
标签: c++ c malloc calloc data-management