How To Detect Pointers In Structure

This is a discussion on How To Detect Pointers In Structure within the C Programming forums, part of the General Programming Boards category; Hi! I want to write a function that will be able to detect pointers in a structure and if necessary, ...

  1. #1
    Code Warrior
    Join Date
    Nov 2001
    Posts
    669

    Question How To Detect Pointers In Structure

    Hi!

    I want to write a function that will be able to detect pointers in a structure and if necessary, free them. It will work like this. If the pointer is found in a structure than check if it was freed. If it was not freed, than free it.

    Example:
    Code:
    ...
    typedef struct _TEMP
    {
        short *Number;
        char **Text, Temp;
        int i;
    } TEMP;
    ...
    CheckStruct (TEMP);
    ...
    Is this possible? If anyone of you guys/girls have any idea, please post it.

    Thanks!
    Current projects:
    1) User Interface Development Kit (C++)
    2) HTML SDK (C++)
    3) Classes (C++)
    4) INI Editor (Delphi)

  2. #2
    Code Warrior
    Join Date
    Nov 2001
    Posts
    669
    >Then there's the problem of what to do with say a linked list ...

    I don't need such a complex function. I'll skip that .

    >Then for something like this
    char **Text,
    How do you know what the value of 'n' was in this call
    Text = malloc( n * sizeof(char*) );
    which allocated it in the first place

    That's the biggest problem.

    How can I distinct pointers from other types?
    Current projects:
    1) User Interface Development Kit (C++)
    2) HTML SDK (C++)
    3) Classes (C++)
    4) INI Editor (Delphi)

  3. #3
    eee2
    Guest
    This is what a garbage collector does. Do a search:c "garbage collector code"

  4. #4
    Code Warrior
    Join Date
    Nov 2001
    Posts
    669
    >Only the compiler can do that
    >You can't for example tell simply by looking at an anonymous array of bytes, and decide whether any consecutive sequence of them are a valid pointer to malloc'ed memory

    Damn!

    >This is what a garbage collector does. Do a search:c "garbage collector code"

    I don't need that. Thanks anyway.
    Current projects:
    1) User Interface Development Kit (C++)
    2) HTML SDK (C++)
    3) Classes (C++)
    4) INI Editor (Delphi)

  5. #5
    Code Warrior
    Join Date
    Nov 2001
    Posts
    669
    Instead that I write the code for freeing the pointers in a structure I wanted that the function will do that instead of me.

    Positive sides:
    - less writing
    - the function will never forget to free a pointer

    Negative sides:
    - I don't know how to write it
    Current projects:
    1) User Interface Development Kit (C++)
    2) HTML SDK (C++)
    3) Classes (C++)
    4) INI Editor (Delphi)

  6. #6
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Mad idea: switch to C++ and use an auto_ptr
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  7. #7
    Code Warrior
    Join Date
    Nov 2001
    Posts
    669
    That's a good one. I'll switch to C++ as soon as I can. I'm learning it now for about a week.
    Current projects:
    1) User Interface Development Kit (C++)
    2) HTML SDK (C++)
    3) Classes (C++)
    4) INI Editor (Delphi)

  8. #8
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    What if you don't want to free the pointer? Can you ensure that the function gets called? If so, the function probably does something useful aside from silently killing the structure variable. I've found that you'll usually have more reliable code if you leave memory management to the highest reasonable level. That way your other functions don't have to worry about problematic memory management and you save yourself the trouble of making sure that memory management spanning multiple functions/nesting levels works properly. For example:
    Code:
    main()
    {
        allocate memory A
        call f1(A)
        release memory A
        call f2()
    }
    
    f1(A)
    {
        use A
    }
    
    f2()
    {
        allocate memory B
        use B
        release memory B
    }
    Notice that the allocation and release is performed in the same function. You don't have to make sure that it was silently released elsewhere, or hunt for the point of release...It's in the same function, so finding it is easier.

    The same goes for specialized memory routines:
    Code:
    alloc()
    release(mem)
    
    main()
    {
        A = alloc()
        call f1(A)
        release(A)
        call f2()
    }
    
    f1(A)
    {
        use A
    }
    
    f2()
    {
        B = alloc()
        use B
        release(B)
    }
    My best code is written with the delete key.

  9. #9
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Standard garbage collector:

    Code:
    /* this code has not been compiled. use at your own risk */
    struct _memory {
    void ** marshal, * pointer;
    } memory; 
    void init(memory * m)
    {
     m->pointer = 0;
     m->marshal = &m->pointer;
    }
    int track(memory * m, void ** pointer)
    /* returns 1 on success, 0 if the memory is still tied up */
    {
     if(collect(m)) {
     m->marhal = pointer;
     m->pointer = *pointer;
     } else return 0;
     return 1;
    }
    int collect(memory * m)
    /* returns 1 on success, even if no collection was necessary, 
       0 if the memory is still tied up */
    {
     if(collectable(m))free(m->pointer), m->marshal = &m->pointer;
     else return 0;
     return 1;
    }
    int collectable(memory * m)
    /* returns 1 if the memory is invalid, 0 if still good  */
    {
     if(m->pointer != *m->marshal  // trash
     || m->marshal == &m->pointer) // neutral
      return 1;
     return 0;
    }
    int alloc(memory * m, void ** pointer, unsigned amount)
    /* returns number of bytes allocated on success, 
       0 on bad alloc, -1 if the memory is still tied up */ 
    {
     *pointer = malloc(amount);
     if(!*pointer)
      return 0; 
     if(!track(m, pointer)) 
      return -1;
     return amount;
    }
    
    
    int main()
    {
    const int size = 10240;
    memory monitor, * m = &monitor;
    char * buffer = 0;
    
    init(m);
    
    alloc(m, &buffer, size); // ignore result for now.
    
    buffer = "- memory leak?"; 
    
    collect(m);
    }

    Store the structures in a linked list or similar.
    Launching the process in its own, low priority thread would be a good idea, too.

    Naturally, C++ would better suited for this task, IMHO;
    Last edited by Sebastiani; 08-01-2003 at 02:31 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. pthread question how would I init this data structure?
    By mr_coffee in forum C Programming
    Replies: 2
    Last Post: 02-23-2009, 11:42 AM
  2. Generic Pointers to structures
    By dunxton in forum C Programming
    Replies: 8
    Last Post: 02-20-2009, 09:23 AM
  3. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  4. sorting structure members using pointers
    By robstr12 in forum C Programming
    Replies: 5
    Last Post: 07-25-2005, 05:50 PM
  5. Structure Problem
    By Generator in forum C Programming
    Replies: 5
    Last Post: 07-28-2003, 11:54 PM

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