[LCP]Regex Questions

Chuck Martin nrocinu at myrealbox.com
Tue Nov 27 19:09:29 UTC 2001


Could someone who is more familiar with the regex functions than I am
please answer a few questions for me?  In the regex man page, it says:

SYNOPSIS
       #include <regex.h>

       int regcomp(regex_t *preg, const char *regex, int cflags);
       int regexec(const   regex_t  *preg,  const  char  *string,
                   size_t  nmatch,   regmatch_t   pmatch[],   int
                   eflags);
       size_t regerror(int  errcode,  const  regex_t  *preg, char
                       *errbuf, size_t errbuf_size);
       void regfree(regex_t *preg);


Then farther down, it says this about the regfree function:

POSIX PATTERN BUFFER FREEING
       Supplying regfree with a precompiled pattern buffer,  preg
       will  free  the  memory allocated to the pattern buffer by
       the compiling process, regcomp.


That makes it sound like preg in the above synopsis is allocated by
regcomp.  Unfortunately, making that assumption causes segmentation
faults.  If I allocate the space myself, there are no segmentation
faults.  If I'm supposed to allocate space for preg with malloc, then
I have three questions:

    1. How do I know how much space to allocate?
    2. Since space allocated with malloc is supposed to be freed with
       free, what is the purpose of regfree?
    3. Why does the description of regfree sound like regcomp allocates
       that space?

If regcomp is supposed to allocate the space for preg, then my questions
are:

    1. Why am I getting segmentation faults when I fail to allocate space
       for preg?
    2. How do I eliminate the segmentation faults without allocating space
       for preg?

Any help would be greatly appreciated.  Thanks.

Chuck




More information about the linuxCprogramming mailing list