Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

memory problem with calloc 1

Status
Not open for further replies.

julie55

Programmer
Oct 14, 2009
6
FR
Hi all,

I am developing a program where I generate tree-like generations and store resulting offspring for each generation in a 3D structure. As generation level increases the number of individuals gets higher and I end up with very large tables. At one point I get the following allocation error:

Unhandled exception at 0x004157e6 in offspring.exe: 0xC0000005: Access violation writing location 0x00000000.

I am allocating memory in a loop using calloc:

Code:
/* Allocate the full memory for the information */
// nucleotides[gen][seq][nuc] is the 3D table
  nucleotides[gen] = (int **)calloc(n_seq[gen], sizeof(int *));
  for (seq = 0; seq < n_seq[gen]; seq++)
  {
      nucleotides[gen][seq] = (int *)calloc(n_nuc, sizeof(int));
  }

and the program stops at this line:
Code:
nucleotides[gen][seq][nuc] = nucleotides[x][y][nuc];

and I really don't see what the problem can be.

Could anyone help me???

Many thanks in advance

Julie
 
Since the table can be large, maybe you can add a variable that counts the number of bytes allocated and print that variable from time to time to see just how much memory your program allocates. Or you can use some memory profiler.

Is it maybe possible that you allocate such a big amount of memory that the system cannot cope with it? Just a thought ...
 
Thanks for your quick answer.

The system does not crash when I do the allocation with caloc but it crashes later when I try to access the address to store a new value with error
"Access violation writing location 0x00000000".

How would count the number of bytes allocated?

Thanks again

Julie
 
I never made experiments with marginal allocations of memory, but it is my understanding of the malloc/calloc functions that they don't fail if there is no more memory to allocate, but just return NULL values (see reference manual). The program crashes upon attempting to access the respective memory area, which is exactly your case. So a quick fix would be to test that the return value of calloc is not NULL before you perform operations on the respective cells. But if you indeed need all that memory, then this fix will not solve your problem, you should perhaps work on a more efficient way of using your memory.

As for your other question, just increment a large-enough variable before each calloc. Watch out for overflowing integer types. Alternatively, you can increment that variable for each N bytes allocated (N = 100, N = 1000, ...), to prevent overflowing. You can then watch the value of that variable in the debugger after a crash of the program.
 
Many thanks for your answer, it helped a lot!

Julie
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top