Thread: multiple indirection...

  1. #1
    Linguistic Engineer... doubleanti's Avatar
    Join Date
    Aug 2001
    Location
    CA
    Posts
    2,459

    multiple indirection...

    i stay away from using arrays as much as possible... i use pointers for everything if i can... but i believe a while back, back in the old board, i came across the problem of setting up an array of pointers. i wouldn't want to use something like...

    char * ptr [num_ptrs];

    because that uses arrays... which, as i've already stated, i don't like...

    how might i use calloc, for example... to allocated a pointer array... something like...

    ptr = (char **) calloc (huh, what?, when?, where?!?);

    thanks!
    hasafraggin shizigishin oppashigger...

  2. #2
    Has a Masters in B.S.
    Join Date
    Aug 2001
    Posts
    2,263
    ptr = (char **) calloc (huh, what?, when?, where?!?);

    that might not work and you might get some nasty errors
    i cant remember the c way but heres the C++ way to hold you over till i recall... if i can that is...

    C++ way:

    char** ptr;

    ptr = new char*[num_ptrs];
    Last edited by no-one; 08-29-2001 at 11:55 PM.

  3. #3
    Registered User
    Join Date
    Aug 2001
    Posts
    72
    ptr = (char **) calloc (num_ptrs, sizeof(char*));

    is it didn't work?

  4. #4
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    ptr=(char **)calloc(NumPtrs,sizeof(char))

    This is just a two dimensional array. Why do you need to declare and allocate this way?

  5. #5
    Linguistic Engineer... doubleanti's Avatar
    Join Date
    Aug 2001
    Location
    CA
    Posts
    2,459

    reason being...

    firstly i'm very curious... and secondly, suppose i want to have a log of allocated memory, which i want to free if a runtime error occurs that has nested allocations... and i did not wish to use a linked list or some other sort of convention. i think, too, that using this type of setup is easier [for me] to understand. thanks. i'll try those out... and yes, damyan, that would be only a 2 dimensional array since the bytes are still allocated, not pointers to them... sizeof still returns but an integral value...

    i'm sure there must be some way to do this! thanks!
    hasafraggin shizigishin oppashigger...

  6. #6
    Unregistered
    Guest
    Bubba said that it would just be a two-dimensional array.
    • To keep a log of allocated memory, track each memory pointer by a handle.
    • To de-allocate call your own type of Free() function and mark the area as free.
    • To allocate, check the memory for availability against space and other handles and then return a unique handle to that memory.
    • Call another function which requires a handle and returns a void pointer to the memory area. The pointer then can be typecasted to another data type by the programmer.
    • You should have functions that will lock and unlock memory areas.


    Managing memory this way can be tedious, very confusing, and very complex especially if you want to code all the functions yourself.

    If a run-time error happens or an exception is thrown then iterate through the array or linked list of handles and call your version of free() for each.

    Another implementation

    You could also implement some type of binary or quad-tree that would allow you to know which handles need to be freed. Parent nodes would be the first memory object. Subsequent nodes would be memory that was allocated after the parent node, but still belong to the parent object.

    This would allow you to free up memory within the parent object tree and would also let you know which subsequent memory handles needed to be freed. Anything that was a child node of the node that you wish to free, would need to be freed. If you wish to delete the parent node, or the entire object, then all subsequent nodes would need to be freed, thus freeing all memory taken up by the object. Nodes would be indentified by their handle and would remain transparent to you the programmer. The programmer would just be accessing the memory handles w/o really knowing/caring how everything worked, just that it did.

    Example:
    Object - Handle

    ParentObject - 0
    ..Child1 - 1
    ....Child11 -(First Child of Child1) - 2
    ..Child2 -3
    ....Child21 -(First Child of Child2) -4


    Let's say to want to delete memory handle 4:
    • Does Child21 have children: No - delete/free child21


    To delete memory handle 1:
    • Does Child1 have children: Yes-next
    • Does Child11 have children: No - delete/free child11,child1


    To delete the ParentObject:
    • Does ParentObject have children: Yes - next
    • Does Child1 have children: Yes - next
    • Does Child11 have children: No - delete/free child11, child1
    • Does Child2 have children: Yes - next
    • Does Child21 have children: No - delete/free child21,child2
    • Delete/free ParentObject


    Delete/Free means delete memory object and free handle.

    Unfortunately to implement this, you will use a linked list of sorts, but it will guarantee that all memory objects are freed properly. This could be easily implemented recursively or if you do not want the function call overhead or do not have enough stack space, you could do it in a do:while.

  7. #7
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    All that work and I was not even logged in.

    The long post was from Bubba. I was disconnected while responding to the post and I guess when it reconnected it did not log me in.

    Anyways, doubleanti, I put a lot of thought into my response so I hope it helps if not just a little.

  8. #8
    Blank
    Join Date
    Aug 2001
    Posts
    1,034
    Why don't you like arrays?
    In c++ allocate your dynamic arrays with new.

    char* a = new char[180];

    delete[] a;

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 06-08-2009, 03:03 PM
  2. Replies: 8
    Last Post: 06-04-2009, 02:03 PM
  3. When and why to use multiple indirection ?
    By marcossoft in forum C Programming
    Replies: 1
    Last Post: 10-10-2007, 12:59 PM
  4. NAQ: Everything you never wanted to know about CPP
    By evildave in forum C Programming
    Replies: 21
    Last Post: 12-12-2005, 10:56 AM
  5. Linker errors - Multiple Source files
    By nkhambal in forum C Programming
    Replies: 3
    Last Post: 04-24-2005, 02:41 AM