【发布时间】:2015-06-15 02:24:14
【问题描述】:
我需要一系列 unsigned long long 类型的缓冲区,这些是我在分配中使用的代码行。
FAC_op_Buffer = static_cast<uint_64 *>( calloc(static_cast<uint_32>(ceil(static_cast<float>(2*FAC_N) / 64.0) ), sizeof(uint_64)) );
SDC_op_Buffer = static_cast<uint_64 *>( calloc(static_cast<uint_32>(ceil(static_cast<float>(2*SDC_n) / 64.0) ), sizeof(uint_64)) );
MSC_coder_h_lvl_1 = static_cast<uint_64 *>( calloc( static_cast<uint_32>(ceil(static_cast<float>(2*(MSC_n1_n2[0] + MSC_n1_n2[1])) / 64.0) ), sizeof(uint_64) ) );
MSC_coder_h_lvl_2 = static_cast<uint_64 *>( calloc( static_cast<uint_32>(ceil(static_cast<float>(2*(MSC_n1_n2[0] + MSC_n1_n2[1])) / 64.0) ), sizeof(uint_64) ) );
MSC_coder_h_lvl_3 = static_cast<uint_64 *>( calloc( static_cast<uint_32>(ceil(static_cast<float>(2*(MSC_n1_n2[0] + MSC_n1_n2[1])) / 64.0) ), sizeof(uint_64) ) );
MSC_coder_l_lvl_1 = static_cast<uint_64 *>( calloc( static_cast<uint_32>(ceil(static_cast<float>(2*(MSC_n1_n2[0] + MSC_n1_n2[1])) / 64.0) ), sizeof(uint_64) ) );
MSC_coder_l_lvl_2 = static_cast<uint_64 *>( calloc( static_cast<uint_32>(ceil(static_cast<float>(2*(MSC_n1_n2[0] + MSC_n1_n2[1])) / 64.0) ), sizeof(uint_64) ) );
MSC_coder_l_lvl_3 = static_cast<uint_64 *>( calloc( static_cast<uint_32>(ceil(static_cast<float>(2*(MSC_n1_n2[0] + MSC_n1_n2[1])) / 64.0) ), sizeof(uint_64) ) );
变量“MSC_n1_n2”、“FAC_N”、“SDC_N”包含要分配的位数。 而 uint_64 只是 stdint 中标准 int 的 typedef。
typedef uint64_t uint_64;
现在的问题是当它进入第二行分配“MSC_coder_h_lvl_2”时。我得到一个错误。 - Windows 触发断点.....
HEAP:释放后在 YYY 修改的空闲堆块 XXX
内存位置 XXX 和 YYY 每次都在变化。但它总是指向我之前分配但未释放的另一个缓冲区。
如果我点击继续,其余的分配就会毫无问题地发生。当我查看内存窗口时,所有其他分配的位置都被初始化为零,就像它应该的那样。只有第二条语句失败,这使得“MSC_coder_h_lvl_2”指向 0x0000000。
我使用 calloc 而不是 new 因为我想要调整这个缓冲区大小的能力。
谁能帮我解决这个问题。
【问题讨论】:
-
不要在C++中使用
malloc之类的,甚至不要使用new[]。请改用std::vector。 -
如果您在调试代码方面需要帮助,请发帖SSCCE。
-
问题是,如果我把这个分配代码剪下来单独写成SSCCE,它就可以正常工作。我不知道我的项目中还有什么在干扰这个。
-
好吧,找出答案!我们也没有一个能告诉我们的魔法水晶球。 (而且你真的应该使用
std::vector,而真的真的应该不 使用calloc。) -
I am using calloc instead of new because I want the capability of re sizing this buffer.然后使用std::vector。Can somebody help me to resolve this issue.那个“帮助”叫做debugging。你想以这种方式使用动态分配的内存 --- 一旦你这样做了,由于你的代码管理不善的指针、动态分配的内存等,预计会出现这些错误。一旦你把动态内存的责任放在了 C++ 中,这就是你所做的交易分配在你的肩膀上。
标签: c++ visual-c++ memory memory-management