Thread: The occupation of memory

  1. #1
    Registered User
    Join Date
    Jun 2010
    Posts
    14

    The occupation of memory

    Hi,
    I need an explanation.
    I have written various softwares and procedures in C. I can't understand the usage of memory. When I use a list of structs and initialize a lot of elements, sometimes I would to empty this structure to erase the memory I used.
    The function FREE seems to be the only function used to deallocate memory but I believe it doesn't works correctly because I have everytime problems with memory usage.
    What happens using the free function?

    Is it possible that the only manner to erase a list of pointers used can be only using a procedure that for each element of the list erase all its field?

    Thanks to anyone can answer

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Why nor provide a concrete example to demonstrate what you are talking about?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Jun 2010
    Posts
    14
    Quote Originally Posted by laserlight View Post
    Why nor provide a concrete example to demonstrate what you are talking about?
    I would do it but is impossible. The programs are too much long.
    I can do an example for my question:

    I create a structure like this for a list of clusters:
    Code:
    struct cluster_type{
    	int index;//indice del cluster
    	int totElements;//number of elements in the cluster
    	elementType *elements;//pointer to the elements of the cluster
    	clusterType *nextCluster;//point to next Cluster
    	int centroid;//the centroid 
    };
    I use this in a while loop:
    Code:
    while (..){
    clusters=(clusterType *)malloc(k*sizeof(clusterType));
    //....procedures to create elements in the list
    free(rowClusters);
    }
    at a certain point seems that structures can't be allocate anymore.
    The question is: What happen when I use the free function?

  4. #4
    Registered User
    Join Date
    Aug 2007
    Posts
    3
    what is rowClusters. You need to visit each cluster in the list and free it manually. You just cant free the head and get away with that.

  5. #5
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    What happens using the free function?
    When you pass free() a pointer, the operating system knows how much memory was allocated when that pointer was returned from malloc(). Accordingly, it marks the corresponding memory as free so that it can be reallocated to someone else later. So even if memory that you've allocated through multiple calls to malloc() is contiguous or related through pointers in your struct, etc... malloc() and free() simply can't communicate about that kind of thing. Every call to malloc() must have a matching call to free(). A good practice is to malloc() and free() at the begginning and end, respectively, of the same function.

    This is why what faxo said is probably your problem. Also, you need to free() your linked list in reverse. If you start at the root and work to the end, you're deallocating your pointer to the next node, and there's a chance it could be overwritten. Start at the end and work back to the root. A good way to do this might be recursively, where the node is free()'d AFTER the recursive call.

    Also, if your malloc() and free() calls aren't nicely lined up and you have real leaks somewhere, debugging tools like valgrind and other debuggers can help you verify what is really going on in the program and track down leaks.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Memory Fragmentation with Dynamic FIFO Queue
    By fguy817817 in forum Linux Programming
    Replies: 17
    Last Post: 10-31-2009, 04:17 AM
  2. Replies: 4
    Last Post: 01-13-2008, 02:14 AM
  3. Question regarding Memory Leak
    By clegs in forum C++ Programming
    Replies: 29
    Last Post: 12-07-2007, 01:57 AM
  4. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 11:22 AM
  5. Shared Memory - shmget questions
    By hendler in forum C Programming
    Replies: 1
    Last Post: 11-29-2005, 02:15 AM