ftp.delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/2002/04/11/14:31:23

X-Authentication-Warning: delorie.com: mailnull set sender to djgpp-bounces using -f
Date: Thu, 11 Apr 2002 21:26:09 +0300
From: "Eli Zaretskii" <eliz AT is DOT elta DOT co DOT il>
Sender: halo1 AT zahav DOT net DOT il
To: "Yong Kwang Goh" <gohyongkwang AT hotmail DOT com>
Message-Id: <1659-Thu11Apr2002212609+0300-eliz@is.elta.co.il>
X-Mailer: emacs 21.2.50 (via feedmail 8 I) and Blat ver 1.8.9
CC: djgpp AT delorie DOT com
In-reply-to: <a94dfn$a91$1@violet.singnet.com.sg> (gohyongkwang@hotmail.com)
Subject: Re: Allocate memory for pmatch[] prior to calling regexec()?
References: <a94dfn$a91$1 AT violet DOT singnet DOT com DOT sg>
Reply-To: djgpp AT delorie DOT com
Errors-To: nobody AT delorie DOT com
X-Mailing-List: djgpp AT delorie DOT com
X-Unsubscribes-To: listserv AT delorie DOT com

> From: "Goh, Yong Kwang" <gohyongkwang AT hotmail DOT com>
> Newsgroups: comp.os.msdos.djgpp
> Date: Thu, 11 Apr 2002 16:51:10 +0800
> 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?

You have to allocate it.

> 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

The number of subexpressions is returned by regcomp in the
preg->re_nsub member (see the docs of regcomp, which explains this).

> 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.

This argument is input to regexec, not output from it.  So it's
passed by value and is not changed by regexec.

> In another word, the size of pmatch[] is already fixed and memory
> has already been allocated for pmatch[] prior to calling regexec.

That's correct.

- Raw text -

  webmaster     delorie software   privacy  
  Copyright 2019   by DJ Delorie     Updated Jul 2019