Memory Management

This is a discussion on Memory Management within the C Programming forums, part of the General Programming Boards category; G'Day, Just a quick query on memory management. I have a dynamic array of a particular struct. Code: struct myStruct ...

  1. #1
    rasdfasfd
    Guest

    Cool Memory Management

    G'Day,
    Just a quick query on memory management.
    I have a dynamic array of a particular struct.
    Code:
    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.
    Code:
    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:
    ds.am.memPtrs = malloc(2 * sizeof(void *) );
    ds.am.memPtrs[0] = ds.ptr1;
    ds.am.memPtrs[1] = ds.ptr2;
    ds.am.numElements = 2;
    Then when it comes time to cleanup it is as easy as:
    Code:
    //loop through array of my structs
    struct allocatedMemory *t = (myStruct[index]).anotherStruct
    for (int x = 0;x< t->numElements;t++) {
      free(t->memPtrs[x])
    }
    Does anyone have comments on this approach? Is it portable? Is there other ways?

  2. #2
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    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.

    Code:
    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
    rafdggsfs
    Guest
    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, 11:27 AM
  3. Valgrind says I suck at memory management :)
    By carrotcake1029 in forum C Programming
    Replies: 6
    Last Post: 02-01-2009, 07:10 PM
  4. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 08:32 AM
  5. Managing shared memory lookups
    By clancyPC in forum Linux Programming
    Replies: 0
    Last Post: 10-08-2003, 04:44 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21