B
Bill Reid
Now I can, because I just figured out my error, but now I'm
more confused than usual, because I just don't get why my
code was working at all but I couldn't free the list...here's
the story, maybe somebody can explain this behavior...
In my never-ending quest to mess up my code in an attempt to
speed it up by 0.00005%, I recently tried a different way to sort
some fairly big lists of strings (up to 100,000 strings or even more).
I wanted to sort a buffer full of these null-terminated strings
by alpha order of "substrings" at certain locations in the strings.
So I generated a list of pointers to the strings in the buffer and
a function to sort the substrings and reorder the list using
qsort().
Everything worked great, it was measureably a quicker way to
sort through these strings than I was using before, but there's
just one tiny little problem: I got an exception when I tried to free
the list of string pointers!
I'm declaring my list of string pointers as "char **", allocating
the list based on the count of newlines running through a text
file buffer finding each newline and replacing it with a NUL
terminator while I add a pointer to the start of the string to
each incremented position in the list. Then I sort the list of
pointers based on substrings and can then perform various
search and other operations on the reordered list. Again,
all of this stuff worked FINE, but when I went to free the
list, BLAMMO!!!
The list pointer was fine before, during, and after all the
operations, but the problem was the fact that I forgot to
multiply the number of strings by "sizeof(char *)" when I
allocated the list originally. So I guess I was not getting a
list the size of "pointers to char" but the size of "char", right?
So my question would be: how the hell did my code work
fine with that type of error? Isn't a "pointer to char" a much
bigger size than "char", so how did the list successfully
hold and increment and sort through the pointers? And
then why the hell would it only fail to FREE the list?
ANSWER ME, DAMMIT!!!
more confused than usual, because I just don't get why my
code was working at all but I couldn't free the list...here's
the story, maybe somebody can explain this behavior...
In my never-ending quest to mess up my code in an attempt to
speed it up by 0.00005%, I recently tried a different way to sort
some fairly big lists of strings (up to 100,000 strings or even more).
I wanted to sort a buffer full of these null-terminated strings
by alpha order of "substrings" at certain locations in the strings.
So I generated a list of pointers to the strings in the buffer and
a function to sort the substrings and reorder the list using
qsort().
Everything worked great, it was measureably a quicker way to
sort through these strings than I was using before, but there's
just one tiny little problem: I got an exception when I tried to free
the list of string pointers!
I'm declaring my list of string pointers as "char **", allocating
the list based on the count of newlines running through a text
file buffer finding each newline and replacing it with a NUL
terminator while I add a pointer to the start of the string to
each incremented position in the list. Then I sort the list of
pointers based on substrings and can then perform various
search and other operations on the reordered list. Again,
all of this stuff worked FINE, but when I went to free the
list, BLAMMO!!!
The list pointer was fine before, during, and after all the
operations, but the problem was the fact that I forgot to
multiply the number of strings by "sizeof(char *)" when I
allocated the list originally. So I guess I was not getting a
list the size of "pointers to char" but the size of "char", right?
So my question would be: how the hell did my code work
fine with that type of error? Isn't a "pointer to char" a much
bigger size than "char", so how did the list successfully
hold and increment and sort through the pointers? And
then why the hell would it only fail to FREE the list?
ANSWER ME, DAMMIT!!!