I guess you were pointing out the possible memory leak in that
proposed design of my_match(), right?
Yes.
There's no necessary to relate Memory Leak with The C Language, right?
Sort of. Part of the nature of C is that you do your own memory management.
How about the functions specified in REGEX(3). They have regfree(). Is
it possible to have memory leak when I use these functions?
If you are at a level of expertise where you have to ask this question, isn't
it pretty ridiculous for you to be making sweeping claims about the respective
merits of C and other languages?
For example, is there risk of memory leak in this kind of new design?
char *my_match(const char *haystack,
const char *pattern,
char *result,
size_t pos);
If it still has memory leak anyway, is it possible to improve it
further to avoid the memory leak? Can an expert like you implement it
without memory leak? Can you improve it with you expertise and
experience?
That design doesn't tell us enough to know how it would work. Let's ignore
the obvious problem, which is that you've not provided any way to specify
how large the result buffer is.
The minimal case is that you have to allocate storage for a result buffer,
then free that buffer later. You wouldn't have to in a scripting language
that hides memory management.
String manipulations can involve large numbers of allocations and frees.
In C, those are easy to get wrong. In perl/python/ruby/php/et al., those
are impossible to get wrong. It turns out that, if you're doing a lot of
string code, you will always get it written faster and more clearly in
a language designed for it.
Learn to use multiple tools effectively. You're basically arguing that
a really GOOD carpenter should only need a screwdriver, because you can sort
of use a screwdriver to shave things like a plane, and you can bash things
with the handle like a hammer, so really, the screwdriver is the best tool
and we should try to eliminate these other, inferior, tools which can't do
everything.
-s