动态多维数组,指针的释放不是malloced ..

编程入门 行业动态 更新时间:2024-10-28 11:21:53
本文介绍了动态多维数组,指针的释放不是malloced ..的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

大家好, 解决大量内存问题。我在这里有一段简单的代码,用于在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

解决方案

我们** ******@gmail 写道:

大家好, 遇到问题解决大量内存问题。我在这里有一段简单的代码,用于在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错误。

看起来很可疑,机器是否接近6GB或虚拟内存 可用?通过尝试 分配比机器提供的更多的方式,确保您的操作员新行为正确。 - Ian柯林斯。

5月12日凌晨2:02,welch.r ... @ gmail写道:

大家好, 解决大量内存问题。我在这里有一段简单的代码,用于在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,则会在重新分配时给出 以下错误:

那里有很多分配。 在我看来,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

5月11日晚上8:24,Ian Collins< ian-n ... @ hotmailwrote: welch.r ... @ gmail写道:

大家好,

解决大量内存时遇到问题。我在这里有一段简单的代码,用于在ppc64机器上分配一大块 内存。代码是:

/ * 测试看看当我们尝试分配一个巨大的 内存。 * /

#include< iostream> #include< string> #include< stdexcept> using namespace 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; string blank; getline(cin,blank);

// Deallocate。 for(int k = 0; k< ROWS; k ++){ delete [] test [k]; } delete [] test;

cout<< 释放完成! << endl; cout<< 按一个键终止.. <<结束; getline(cin,空白); } catch(bad_alloc& e){ cout << 分配失败.. << endl; }

返回0; }

如果我将ROWS和COLS设置为5000和5000,它就可以正常工作。但是, 如果我将ROWS设置为635000并且COLS设置为2350,则会在重新分配时给出 以下错误:

HugeMemory.exe(29468)malloc:***指针的释放不是 malloced:0x20afd2000;这可能是一个double free()或free(),它被称为 ,其中包含已分配的块;尝试设置环境 变量MallocHelp看到帮助调试的工具

注意分配步骤成功,而且我只允许代码取消分配数组后收到此 错误。

看起来很可疑,机器是否接近6GB或虚拟内存 可用?通过尝试 分配比机器提供的更多的方式,确保您的操作员新行为正确。 - Ian科林斯。

嗯..机器有8 GB的RAM,所以这可能不是问题。 我会尝试最大限度地了解会发生什么。

Hi all, Having a problem with addressing large amounts of memory. I have a simple piece of code here that is meant to allocate a large piece of memory on a ppc64 machine. The code is: /* Test to see what happens when we try to allocate a massively huge piece 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 the following error upon deallocation: HugeMemory.exe(29468) malloc: *** Deallocation of a pointer not malloced: 0x20afd2000; This could be a double free(), or free() called with the middle of an allocated block; Try setting environment variable MallocHelp to see tools to help debug Note that the allocation step succeeds, and that I only receive this error after allowing the code to deallocate the array. Any ideas? Thanks, Ryan

解决方案

we********@gmail wrote:

Hi all, Having a problem with addressing large amounts of memory. I have a simple piece of code here that is meant to allocate a large piece of memory on a ppc64 machine. The code is: /* Test to see what happens when we try to allocate a massively huge piece 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 the following error upon deallocation: HugeMemory.exe(29468) malloc: *** Deallocation of a pointer not malloced: 0x20afd2000; This could be a double free(), or free() called with the middle of an allocated block; Try setting environment variable MallocHelp to see tools to help debug Note that the allocation step succeeds, and that I only receive this error after allowing the code to deallocate the array.

Looks suspect, has the machine got the nigh on 6GB or virtual memory available? Make sure your operator new behaves correctly by attempting to allocate way more than the machine can provide. -- Ian Collins.

On May 12, 2:02 am, welch.r...@gmail wrote:

Hi all, Having a problem with addressing large amounts of memory. I have a simple piece of code here that is meant to allocate a large piece of memory on a ppc64 machine. The code is: /* Test to see what happens when we try to allocate a massively huge piece 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 the following error upon deallocation:

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, Branimir

On May 11, 8:24 pm, Ian Collins <ian-n...@hotmailwrote:

welch.r...@gmail wrote:

Hi all,

Having a problem with addressing large amounts of memory. I have a simple piece of code here that is meant to allocate a large piece of memory on a ppc64 machine. The code is:

/* Test to see what happens when we try to allocate a massively huge piece 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 the following error upon deallocation:

HugeMemory.exe(29468) malloc: *** Deallocation of a pointer not malloced: 0x20afd2000; This could be a double free(), or free() called with the middle of an allocated block; Try setting environment variable MallocHelp to see tools to help debug

Note that the allocation step succeeds, and that I only receive this error after allowing the code to deallocate the array.

Looks suspect, has the machine got the nigh on 6GB or virtual memory available? Make sure your operator new behaves correctly by attempting to 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 ..

本文发布于:2023-07-30 11:04:05,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1248825.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:多维   数组   指针   动态   malloced

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!