Related articles |
---|
Passing string literals in function calls in an interpreter jimmyp@hal.csd.auth.gr (2002-12-26) |
Re: Passing string literals in function calls in an interpreter jimbo@radiks.net (2002-12-30) |
Re: Passing string literals in function calls in an interpreter roger@corman.net (2002-12-31) |
From: | jimbo@radiks.net (Jim Lawless) |
Newsgroups: | comp.compilers |
Date: | 30 Dec 2002 23:57:40 -0500 |
Organization: | EarthLink Inc. -- http://www.EarthLink.net |
References: | 02-12-115 |
Keywords: | yacc, practice |
Posted-Date: | 30 Dec 2002 23:57:39 EST |
On 26 Dec 2002 23:37:53 -0500, jimmyp@hal.csd.auth.gr (Dimitris)
wrote:
> I'm using bison for the parser so freeing malloced buffers can be a
> problem so if anyone has any ideas on where to find temporary space
> that is freed automagically (like space on the stack frame) I'd be
> very interested.
I use the following code:
#define SCRA_SZ (4096)
char *scratch;
int scratchndx;
....
scratch=(char *)malloc(SCRA_SZ);
scratchndx=0;
....
char *maketmp(char *s)
{
int i;
char *p;
i=strlen(s)+1;
if( (scratch+scratchndx+i) >(scratch+SCRA_SZ-1) ) {
scratchndx=0;
}
strcpy(scratch+scratchndx,s);
p=scratch+scratchndx;
scratchndx+=i;
return p;
}
It's essentially a ring-buffer. I reset the buffer index to zero if I
run out of room and hope that I'm not destroying any active strings.
The buffer just keeps filling until we approach the end, then it
starts over.
While I don't use this for literals, I use it for temporary copies of
strings, such as those that result from a concatenation operation or
something.
Jim Lawless * jimbo@radiks.net * http://www.radiks.net/~jimbo
http://www.mailsend-online.com * Command-line e-mail tools,
tiny scheduler, DUN hangup, batch script language
[Ugh. I'd rather something deterministic, like chaining all the allocated
space together and releasing it all at once when you know you're not
using it any more. -John]
Return to the
comp.compilers page.
Search the
comp.compilers archives again.