[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Segmentation Fault !
From: |
Hans Aberg |
Subject: |
Re: Segmentation Fault ! |
Date: |
Sat, 24 Mar 2001 20:47:07 +0100 |
At 17:51 +0530 2001/03/24, Rutu Raghu Manchiganti [T] wrote:
>I am making a parser using lex and yacc..not flex and bison..so sorry
>for posting the message on this group.
...
>I get a segmentation fault when i try 2 allocate some memory..here is
>the sample code..
>[a-zA-Z-]+[0-9]* { yylval.name = (char*)malloc(strlen(yytext) + 1);
> strcpy(yylval.name, yytext);
> return IDENTIFIER; }
>
>This works fine for the first time and gives a segmentation fault the
>second time !!
Flex \0 terminates yytext, so it appears correct under Flex, except that
you have a memory leak, and could better write malloc(yyleng + 1).
So check what Yacc does, or switch to Flex and Bison
ftp://ftp.digital.com/pub/GNU/non-gnu/flex/flex-2.5.4a.tar.gz
ftp://alpha.gnu.org/gnu/cvs/
I compile under C++, and use
class my_type {
public:
std::string text;
...
};
#define YYSTYPE my_type
and in Flex:
yylval.text = std::string(yytext, yyleng);
This way I avoid memory leaks, and I do not have to worry about correct
allocation. (For more details, see the archives of the help-bison and
bug-bison list.)
Hans Aberg
- Segmentation Fault !, Rutu Raghu Manchiganti [T], 2001/03/24
- Re: Segmentation Fault !,
Hans Aberg <=