-
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!
-
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];
-
ptr = (char **) calloc (num_ptrs, sizeof(char*));
is it didn't work?
-
ptr=(char **)calloc(NumPtrs,sizeof(char))
This is just a two dimensional array. Why do you need to declare and allocate this way?
-
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!
-
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.
-
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.:)
-
Why don't you like arrays?
In c++ allocate your dynamic arrays with new.
char* a = new char[180];
delete[] a;