Thread: can't allocate memory even though have plenty

  1. #1
    Registered User
    Join Date
    Apr 2003
    Posts
    6

    can't allocate memory even though have plenty

    I have a program with not many structures that need memory allocated for, in my opinion, however, half way through code execution when I try to allocate memory for a two-dimensional array using malloc, malloc returns NULL telling me that it couldn't allocate the memory.
    I have 768 MB of RAM on my system and when I run this program, there are over 380MB of RAM available. I'm not sure if there is another issue other than actual memory allocation causing this error, but it's driving me nuts! THanks for any help anyone can give me!

  2. #2
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    Post the code, there might be a logical error.
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  3. #3
    Registered User
    Join Date
    Apr 2003
    Posts
    6
    Here's the allocation command I'm using:

    cmpds[i].conformations[j].feature_dist = (double **)malloc(numfeats2*numfeats2*sizeof(double *));
    if (cmpds[i].conformations[j].feature_dist == NULL)
    {
    printf("Error allocating memory\n");
    return;
    }
    for ( z = 0; z < numfeats2*numfeats2; z++)
    {
    cmpds[i].conformations[j].feature_dist[z] = (double *)malloc(2*sizeof(double));
    if (cmpds[i].conformations[j].feature_dist[z] == NULL)
    {
    printf("Error allocating memory\n");
    return;
    }
    }

  4. #4
    Registered User
    Join Date
    Apr 2003
    Posts
    6
    Well the numfeats variable is 8, so that's not real huge. The number of compounds(the i variable) is 13, and for each compound, there are potentially 100 conformation structures, each with its own two-dimensional array of size [numfeats*numfeats][2]. So, I guess that's 1300 possible two-dimensional arrays we're dealing with, but the thing is, I get the malloc error the first pass through for the first compound, the first conformation, so I've allocated the compounds and the conformation structures, but none of the ones we're talking about that error out.
    You know, it's funny, I'm primarily a Java programmer and this memory allocation issue with C/C++ drives me batty. I guess it was a benefit in the days when memory was godawful expensive, but today, it seems more hassle than it's worth. About 80% of my debugging time deals with memory allocation issues. But, enough ranting....

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problems with shared memory shmdt() shmctl()
    By Jcarroll in forum C Programming
    Replies: 1
    Last Post: 03-17-2009, 10:48 PM
  2. Memory allocation/reallocation
    By magda3227 in forum C Programming
    Replies: 10
    Last Post: 07-04-2008, 03:27 PM
  3. Memory Leak Help
    By (TNT) in forum Windows Programming
    Replies: 3
    Last Post: 06-19-2006, 11:22 AM
  4. allocate memory function
    By sunoflight77 in forum C++ Programming
    Replies: 1
    Last Post: 05-09-2005, 10:38 AM
  5. malloc returning NULL with plenty of memory
    By ... in forum C Programming
    Replies: 3
    Last Post: 03-15-2004, 01:28 AM