本文介绍了动态多维数组,指针的释放不是malloced ..的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 大家好, 解决大量内存问题。我在这里有一段简单的代码,用于在ppc64机器上分配一大块 内存。代码是: / * 测试看看当我们尝试分配一块巨大的内存时会发生什么。 * / #include< iostream> #include< string> #include< stdexcept> 使用命名空间std; int main(int argc,char ** argv){ cout<< 试图分配...... << endl; const int ROWS = 635000; const int COLS = 2350; //分配。 尝试{ int ** test = new int * [ROWS]; for(int i = 0; i< ROWS; i ++){ test [i] = new int [COLS]; for(int j = 0; j< COLS; j ++){ test [i] [j] = 0; } } cout<< 分配成功! << endl; cout<< 按一个键取消分配并继续...... << endl; 字符串空白; getline(cin,空白); // Deallocate。 for(int k = 0; k< ROWS; k ++){ delete [] test [k]; } 删除[]测试; cout<< 释放完成! << endl; cout<< 按一个键终止.. <<结束; getline(cin,空白); } catch(bad_alloc& e){ cout << 分配失败.. <<结束; } 返回0; } 如果我设置ROWS和COLS达到5000和5000,它工作得很好。但是, 如果我将ROWS设置为635000并将COLS设置为2350,则会在重新分配时给出 以下错误: HugeMemory.exe(29468)malloc:***指针的释放不是 malloced:0x20afd2000;这可能是一个double free()或free(),它被称为 ,其中包含已分配的块;尝试设置环境 变量MallocHelp看看帮助调试的工具 注意分配步骤成功,我只收到这个 $ b允许代码取消分配数组后出现$ b错误。 有什么想法吗? 谢谢, Ryan 解决方案 看起来很可疑,机器是否接近6GB或虚拟内存 可用?通过尝试 分配比机器提供的更多的方式,确保您的操作员新行为正确。 - Ian柯林斯。 那里有很多分配。 在我看来,malloc internaly在指针算术中有溢出。 也许你在64位设置上使用32位malloc不知怎的? 试试下面看看是否有效: const unsigned rows = 635000; const unsigned cols = 2350; int(* p)[cols] = new int [rows] [cols]; for(unsigned i = 0; i< rows; ++ i) { for(unsigned j = 0; j< cols; ++ j) p [i] [j] = 0 ; } cin.get(); 删除[] p; 问候,Branimir 看起来很可疑,机器是否接近6GB或虚拟内存 可用?通过尝试 分配比机器提供的更多的方式,确保您的操作员新行为正确。 - Ian科林斯。 嗯..机器有8 GB的RAM,所以这可能不是问题。 我会尝试最大限度地了解会发生什么。 Hi all,Having a problem with addressing large amounts of memory. I have asimple piece of code here that is meant to allocate a large piece ofmemory on a ppc64 machine. The code is:/*Test to see what happens when we try to allocate a massively hugepiece of memory.*/#include <iostream>#include <string>#include <stdexcept>using namespace std;int main(int argc, char** argv) {cout << "Attemping to allocate.." << endl;const int ROWS = 635000;const int COLS = 2350;// Allocate.try {int** test = new int*[ROWS];for (int i = 0; i < ROWS; i++) {test[i] = new int[COLS];for (int j = 0; j < COLS; j++) {test[i][j] = 0;}}cout << "Allocation succeeded!" << endl;cout << "Press a key to deallocate and continue.." << endl;string blank;getline(cin,blank);// Deallocate.for (int k = 0; k < ROWS; k++) {delete[] test[k];}delete[] test;cout << "Deallocation completed!" << endl;cout << "Press a key to terminate.." << endl;getline(cin,blank);}catch(bad_alloc& e) {cout << "Allocation failed.." << endl;}return 0;}If I set ROWS and COLS to 5000 and 5000, it works just fine. However,if I set ROWS to 635000 and COLS to 2350, it will give me thefollowing error upon deallocation:HugeMemory.exe(29468) malloc: *** Deallocation of a pointer notmalloced: 0x20afd2000; This could be a double free(), or free() calledwith the middle of an allocated block; Try setting environmentvariable MallocHelp to see tools to help debugNote that the allocation step succeeds, and that I only receive thiserror after allowing the code to deallocate the array.Any ideas?Thanks,Ryan 解决方案Looks suspect, has the machine got the nigh on 6GB or virtual memoryavailable? Make sure your operator new behaves correctly by attemptingto allocate way more than the machine can provide.--Ian Collins.There are lot of allocations there.Looks to me that malloc internaly has overflow in pointer arithmetic.Perhaps you use 32 bit malloc on 64 bit setup somehow?Try following and see if works:const unsigned rows = 635000;const unsigned cols = 2350;int (*p)[cols] = new int[rows][cols];for(unsigned i = 0; i<rows;++i){for(unsigned j=0;j<cols;++j)p[i][j] = 0;}cin.get();delete[] p;Greetings, BranimirLooks suspect, has the machine got the nigh on 6GB or virtual memoryavailable? Make sure your operator new behaves correctly by attemptingto allocate way more than the machine can provide.--Ian Collins.Hmm.. the machine has 8 GB of RAM, so that probably isn''t the issue.I''ll try maxing it out to see what happens. 这篇关于动态多维数组,指针的释放不是malloced ..的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 08-22 14:49