Re: Dynamic multidimensional array, deallocation of pointer not malloced..
On May 11, 11:45 pm, Ian Collins <ian-n...@hotmail.com> wrote:
welch.r...@gmail.com wrote:
On May 11, 9:15 pm, Branimir Maksimovic <b...@hotmail.com> wrote:
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
I can't seem to get that code to compile, it complains about the line
before the for loop. I'm using gcc 4.0.1 for apple/darwin. Any ideas?
The code is fine, with the exception of an integer overflow warning from
gcc.
I think you're probably right, it has something to do with pointer
arithmetic. I'm just not sure what. The malloc() failure is happening
on the for loop where I'm deallocating each row of the array, I've
figured out that much. Beyond that, I'm not sure.
Update your code to scan the rows for duplicate addresses. If you find
one, something is wrong!
--
Ian Collins.
Okay, I wrote something that I *think* would detect duplicate
addresses. Don't laugh at the implementation..
#include <iostream>
#include <string>
#include <stdexcept>
#include <map>
using namespace std;
int main(int argc, char** argv) {
cout << "Attempting to allocate.." << endl; // I can spell correctly
now..
const int ROWS = 635000;
const int COLS = 2350;
// Keep track of all used addresses.
map<int*,int> addresses;
// Allocate.
try {
int** test = new int*[ROWS];
for (int i = 0; i < ROWS; i++) {
test[i] = new int[COLS];
addresses[ test[i] ] += 1;
for (int j = 0; j < COLS; j++) {
test[i][j] = 0;
}
}
// Check for duplicate addresses.
cout << "Checking for duplicate addresses.." << endl;
map<int*,int>::iterator iter = addresses.begin();
map<int*,int>::iterator end = addresses.end();
while (iter != end) {
if (iter->second > 1) {
cout << "--> Duplicate address detected: " << iter->first<<
endl;
}
iter++;
}
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++) {
int** ptr = test + k;
delete[] ptr;
}
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;
}
That code detects no duplicate addresses.
One additional thing I've noticed, I don't know if this helps: the
addresses in these error messages are about 8 apart from each other,
for example:
HugeMemory.exe(537) malloc: *** Deallocation of a pointer not
malloced: 0x2009350; 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
HugeMemory.exe(537) malloc: *** Deallocation of a pointer not
malloced: 0x2009358; 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
HugeMemory.exe(537) malloc: *** Deallocation of a pointer not
malloced: 0x2009360; 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