[LCP]Regex Questions
Andrew Weaver
Andrew.Weaver at tecnomen.fi
Tue Nov 27 22:26:16 UTC 2001
OK. I try. Here is code that I use and works.
regex_t OprPatternBuff,
ProgPatternBuff,
InstPatternBuff,
IntfPatternBuff,
SvcIdPatternBuff,
PortPatternBuff,
IORPatternBuff;
size_t NumMatches = 0; // Unused...
regmatch_t SubstrMatches; // Unused...
then.... Result = regcomp(&OprPatternBuff, SearchId.Prog.Opr,
(int)REG_EXTENDED | REG_NOSUB);
if (Result)
return;
then...
if (regexec(&OprPatternBuff, SLEP->SLEUnion.SLDataEntry.OperatorName,
NumMatches, &SubstrMatches, (int)REG_NOTBOL |
REG_NOTEOL))
continue;
then...regfree(&OprPatternBuff);
regfree(&ProgPatternBuff);
regfree(&InstPatternBuff);
regfree(&IntfPatternBuff);
regfree(&SvcIdPatternBuff);
regfree(&PortPatternBuff);
regfree(&IORPatternBuff);
return;
...and so on. To try to answer some specifics...
You have to provide the preg structure (and its space of course). Within the
preg struct, regcomp will construct some pattern buffers and so on. It does
this by malloc/calloc onto some pointers that are fields within the struct.
regfree() doesn't free the preg struct, it frees the stuff that it
mallc/calloc itself.
> -----Original Message-----
> From: Chuck Martin [SMTP:nrocinu at myrealbox.com]
> Sent: Tuesday, November 27, 2001 10:04 AM
> To: Linux C Programming
> Subject: [LCP]Regex Questions
>
> 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
>
> _______________________________________________
> This is the Linux C Programming List
> : http://lists.linux.org.au/listinfo/linuxcprogramming List
More information about the linuxCprogramming
mailing list