NO

Author Topic: Memory Allocation Problems  (Read 1679 times)

Offline stecoop

  • Member
  • *
  • Posts: 5
Memory Allocation Problems
« on: May 29, 2019, 05:26:22 PM »
I'm not sure how to explain the problems I'm running into with the realloc() function and the small/big heap setup; I can't provide any 'simple' code that produces the problems. I'll try to explain what I think is happening. BTW, this problem did NOT occur in version 8 of Pelles C; it showed up in version 9.

If my program makes an initial allocation, using calloc(), of 125,000 bytes (500 records of 250 bytes), then later expands the allocation to 250,000 bytes (1,000 records of 250 bytes), using realloc(), everything works fine until I try to access records beyond about 750, I get an 'Access Violation' error. The call to realloc() did NOT return an error, it was successful. If I allocate memory, initially and incrementally, in smaller blocks of about 200 records (50,000 bytes), I don't get any access violation errors, but I do end up with corrupt data. On the otherhand, if I allocate memory in large blocks, 250,000 bytes or more, everything works just fine.

I've watched this program (and others) operate in the debugger (a tedious and time consuming process when dealing with hundreds of records), and noted that when realloc() is finally called, it does NOT move the memory block, it leaves it at the same address.

I think what is happening is that if the initial allocation is placed in the small heap, realloc() does not move it to the large heap when it expands; it leaves it in the small heap even if it's too big for the small heap. If the initial allocation is in the large heap, everything is fine.

At least, that's how it appears; if I allocate, and re-allocate, memory in large blocks, all goes well; if I allocate, and re-allocate, in small blocks, problems happen.

Again, none of this happened with the version 8 compiler; it all started with version 9.

Obviously, I can work around the problem by simply allocating memory way beyond my needs (I've got 16 gig of RAM in this beast), but that goes against my idea of proper programming and memory usage.

Any thoughts about this, anyone? Has anybody else encountered similar problems?

BTW: I'm running Windows 10, 64-bit, fully updated with 16 gigabytes of ram and 1 terabyte of hard drive space, Intel Core i7 processor.
I was born the same month and year FORTRAN was "born".