jcl@bdrc.bd.com (John C. Lusth) (04/02/91)
I have a need to store some information in an array, but don't want to have an arbitrary limit on the size of the array. I came up with the following scheme to successively increase the size of the array as needed. The code has been excerpted and edited; I hope I haven't introduced errors. char **Strings; if (i >= StringCount) { /* str is not already in the pool */ if (SlotsLeft <= 0) { if (FirstTime) { Strings = (char **) malloc(BankSize * sizeof(char *)); FirstTime = 0; } else Strings = (char **) realloc(Strings, (i+BankSize) * sizeof(char *)); SlotsLeft = BankSize; } Strings[i] = str; /* here's where I trash somebody else */ ++StringCount; --SlotsLeft; } In the program I am working on, I have four arrays I expand this way. Unfortunately, at one point in saving a string, I trash the memory used by another expanding array. My questions are: 1) is there a better way to have arrays that can grow without bound? 2) if not, am I using realloc correctly? I suspect realloc is the culprit because if I set BankSize high enough, the problem goes away. BTW, in the actual code, I check the returns of malloc and realloc. 3) if I am using realloc correctly, what's the best way for finding out how I'm ending up with some shared memory locations? I am running SUN OS 4.1.1 using cc. john -- John C. Lusth, Becton Dickinson Research Center, RTP, NC, jcl@bdrc.bd.com