This is more of a best practices question.
 
I'm working with environment variables.  In UNIX environment variables are made available directly via the
 
extern char **environ
 
construct.  Which is an array of pointers to strings, each string containing the key value pairs of environment variables inherited from the parent process. 
 
What I'm doing is taking those environment variables and modifying some and adding a few more.  This means I need to create a new char * array.  Since I'm adding some new key value pairs I need to grow the array, the documentation I have is pretty explicit about you not been able to realloc the existing array of char*s.  So I create a new one that has enough space to hold pointers to all of the existing strings of key value pairs, plus the number of additional pointers as required.  I then simply assign this new array to the environ variable.
 
My problem is (and I may be a little anal) is that this is a memory leak.  I'm losing the space that is occupied by the original char * array.  Which on a 32 bit machine is bytes of ram per entry.  With a common number of entries been somewhere in the neighbourhood of 70.  So I lose 280 bytes.  Not a big deal.  I worry about these sorts of things because I'm looking to generalize my functions for dealing with key value pairs of variables into a library, which means that with multiple uses the memory leak losses could pile up.  I can't conceive of a time when somebody would use these functions enough for this to be a problem, however just because I can't doesn't mean somebody won't (maybe they are writing a new shell or CGI processor).
 
My problem is you can't free the original environ char * array, because it wasn't allocated by malloc, the same reason why I can't realloc it in place.  I can code around this and be able to free all subsequent arrays I create but damnit, the solution lacks elegance, and I hate that.
 
Is there anything I can do about this?
_______________________________________________
clug-talk mailing list
[EMAIL PROTECTED]
http://clug.ca/mailman/listinfo/clug-talk_clug.ca

Reply via email to