On 2007-05-12 02:02, welch.r...@gmail.com 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.
I have absolutely no idea, but you could try to make the code a bit more
simple by allocating everything in one large block instead:
#include <iostream>
#include <string>
#include <stdexcept>
using namespace std;
int main() {
cout << "Attemping to allocate.." << endl;
const int ROWS = 635000;
const int COLS = 2350;
// Allocate.
try {
int* test = new int[ROWS * COLS];
for (int i = 0; i < ROWS * COLS; i++) {
test[i] = 0;
}
cout << "Allocation succeeded!" << endl;
cout << "Press a key to deallocate and continue.." << endl;
string blank;
getline(cin,blank);
// Deallocate.
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;
}
Do you still get the same error (or some other)? If you do there's
probably something wrong with your standard library.
--
Erik Wikstr=F6m
Nope! That code succeeds. However, that code should require around 6
So it's not even coming close.. or am I reading that incorrectly?