Thread: Memory Management

  1. #1

    Cool Memory Management

    Just a quick query on memory management.
    I have a dynamic array of a particular struct.
    struct myStruct
      int stuff;
      void * anotherStruct;
    As the code suggests the field anotherStruct can point to a number of different structs. Each of these structs may contain zero or more pointers to allocated memory. Rather than having a complex cleanup routine I though this might be a better way.
    struct allocatedMemory {
      //array of pointers
      void ** memPtrs[];
      int numElements;
    struct diffStruct1 {
      struct allocatedMemory am;
      char * ptr1;
      unsigned char * ptr2;
    and more diffStructs something like above.
    Then, whenever I fill out one of the diffStructs I also fill out the allocatedMemory struct contained within it with an array of all the pointers to allocated memory in the diffStruct.
    Code: = malloc(2 * sizeof(void *) );[0] = ds.ptr1;[1] = ds.ptr2; = 2;
    Then when it comes time to cleanup it is as easy as:
    //loop through array of my structs
    struct allocatedMemory *t = (myStruct[index]).anotherStruct
    for (int x = 0;x< t->numElements;t++) {
    Does anyone have comments on this approach? Is it portable? Is there other ways?

  2. #2
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Visalia, CA, USA
    Like anything else there are sacrifices. It looks like you are sacrificing speed for ease of use (for this applicatation it isn't a bad trade-off). I also noticed that you aren't actually allocating memory for each struct. I am confident that you just didn't post this part of the code but if you aren't be sure to do something like.

    ds.ptr1 = malloc(buffer_size);
    Although it is a good trade-off between easy/safe code and speed it is not a good to have large objects. Your structs are almost larger than they are worth. Link lists are pretty strait-foward already. They don't really need an extra layer simply for the act of deletion. You idea is a good one, but about 75% of the time it will be a little much.

  3. #3
    Thanks mate.
    I have thought of using linked lists but I just mastered dynamic arrays in my last project and I'm sure you know how it is: You always use the solution you found in your last project in your next project!

    Anyway thanks for your help.

    P.S The project is a simple and portable DNS resolver and once I put the finishing touches to it I hope to post it somewhere.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. tools for finding memory leaks
    By stanlvw in forum C++ Programming
    Replies: 4
    Last Post: 04-03-2009, 11:41 AM
  2. Replies: 7
    Last Post: 02-06-2009, 12:27 PM
  3. Valgrind says I suck at memory management :)
    By carrotcake1029 in forum C Programming
    Replies: 6
    Last Post: 02-01-2009, 08:10 PM
  4. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 09:32 AM
  5. Managing shared memory lookups
    By clancyPC in forum Linux Programming
    Replies: 0
    Last Post: 10-08-2003, 04:44 AM