manpagez: man pages & more
info flex
Home | html | info | man

File: flex.info,  Node: A Note About yytext And Memory,  Prev: Overriding The Default Memory Management,  Up: Memory Management

21.3 A Note About yytext And Memory
===================================

This section applies only to target languages with manual memory
management.

   When flex finds a match, 'yytext' points to the first character of
the match in the input buffer.  The string itself is part of the input
buffer, and is _NOT_ allocated separately.  The value of yytext will be
overwritten the next time yylex() is called.  In short, the value of
yytext is only valid from within the matched rule's action.

   Often, you want the value of yytext to persist for later processing,
i.e., by a parser with non-zero lookahead.  In order to preserve yytext,
you will have to copy it with strdup() or a similar function.  But this
introduces some headache because your parser is now responsible for
freeing the copy of yytext.  If you use a yacc or bison parser,
(commonly used with flex), you will discover that the error recovery
mechanisms can cause memory to be leaked.

   To prevent memory leaks from strdup'd yytext, you will have to track
the memory somehow.  Our experience has shown that a garbage collection
mechanism or a pooled memory mechanism will save you a lot of grief when
writing parsers.

© manpagez.com 2000-2026
Individual documents may contain additional copyright information.