Hi Gerome.
Hi,
Seems fine, but...
Imagine i want to use your personal alloc & calloc functions to allocate char* of a size > 64 kb, for example i need to use 4 char* of 4 MB each.
char *a, *b, *c, *d;
if i branch'em to your alloc function by telling it i wanted (1024*1024)*4 bytes, i only only have 64 Kb available for my pointer...
Or is there something i've missed ?
Thanks for your help.
You are quite right. The solution would be to resize the buffer, which can be done with a realloc without losing any memory. Just change the size of the "max" variable and realloc to that. Or allocate as much memory as you can get away with straight off the bat.
But actually, since you DO know the number of pointers in the example you cite, you can easily get away with a normal malloc-free arrangement.
It's when your array of pointers are likely to be variable sizes or when you don't even know how many dimensions your arrays will be dim-ed to that you can get into sticky situations with malloc branches.
In the example I was working on, belive it or not, there was a
dirent ***varName
variable required.
The final variable was a folder or file name in some arbitrary directory.
How many? Which to free first?
How many chances do we get to get it right? (All are pointers and all can be freed in any order. I.e., there's no type checking to help us here. )
...And how much time will the OS take to do it's memory management?
We can do ours pretty quickly though accomodating large allocations would require a tad bit of extra code.
Stick with malloc for what you are doing now, but consider a recycling buffer for those temp thingy's we all have need for so often, and for arrays of arrays of pointers to pointers to pointers it's even more convenient.
.