【问题标题】:calloc overwrites another variable's memory?calloc 覆盖另一个变量的内存?
【发布时间】: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


【解决方案1】:

你分配了 256 个浮点数:

float * hist = (float *) calloc(256, sizeof(float));

然后您访问第 1000 个元素,即 UB

cout << "-> " << hist [4 * 250] << endl;

calloc 调用会将您错误指向的一些内存归零

要访问 hist 的第 250 个浮点元素,只需

cout << "-> " << hist [250] << endl;

(由于histfloat上的指针,编译器通过乘以float大小来计算地址,不需要自己做)

如果事先知道大小,最好还是静态分配数据

声明:

float hist[256]={0};

定义hist2img时:

hist2img(float hist[256]){

在这种情况下,当静态索引超出范围时您会收到警告(但如果某些变量索引超出范围,仍然会崩溃/UB:没有运行时检查)

【讨论】:

  • 你不需要memset 来清零数组——float hist[256] = {0}; 就足够了,而且不可能出错。 (memset(hist, 0, sizeof(hist)) 比显式计算大小更安全,但不如初始化安全。)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-06-06
  • 1970-01-01
  • 2023-03-21
  • 2019-04-05
  • 2021-11-28
  • 1970-01-01
相关资源
最近更新 更多