X-Authentication-Warning: delorie.com: mailnull set sender to djgpp-bounces using -f From: "Goh, Yong Kwang" Newsgroups: comp.os.msdos.djgpp Subject: Allocate memory for pmatch[] prior to calling regexec()? Date: Thu, 11 Apr 2002 16:51:10 +0800 Organization: Singapore Telecommunications Ltd Lines: 37 Message-ID: NNTP-Posting-Host: 58charlie005.singnet.com.sg X-Priority: 3 X-MSMail-Priority: Normal X-Newsreader: Microsoft Outlook Express 5.50.4133.2400 X-MimeOLE: Produced By Microsoft MimeOLE V5.50.4133.2400 To: djgpp AT delorie DOT com DJ-Gateway: from newsgroup comp.os.msdos.djgpp Reply-To: djgpp AT delorie DOT com The prototype for regexec() looks like this: int regexec(const regex_t *preg, const char *string, size_t nmatch, regmatch_t pmatch[], int eflags); So prior to calling regexec(), do we have to allocate memory for the pmatch[] array of regmatch_t structures or it'll be dynamically allocated by regexec() itself as it finds sub-expressions matches, conforming to the compiled regex, in the string? If we have to allocate memory for pmatch[], how do we know how many sub-expressions there are in the regex, assuming the regex is only known when the program is run and is determined by user's input, in order to ensure sufficient number of regmatch_t structures to hold the sub-expression matches? Also, one thing that I cannot understand is that that size_t nmatch is supposed to hold the number of matches (sub-expressions) it could find in the string. But the fact is that it's passed by value (and not as pointer) into regexec() and it couldn't possibly be updated by regexec() as it finds matches, hence nmatch has to be of a known value before regexec starts. In another word, the size of pmatch[] is already fixed and memory has already been allocated for pmatch[] prior to calling regexec. Could anyone care to enlighten me on the use of regexec()? Thanks. -- Yong-Kwang Goh Singapore gohyongkwang AT hotmail DOT com --- Outgoing mail is certified Virus Free. Checked by AVG anti-virus system (http://www.grisoft.com). Version: 6.0.344 / Virus Database: 191 - Release Date: 4/2/02