本文介绍了在lex / yacc中释放()或不释放()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




我在学校项目中工作,我们使用lex / yacc编写编译器

用于虚构(类似Java)语言。我已经处理了有关yacc和lex文件的所有详细信息,但我仍然有一个关于字符串动态内存分配的问题。当lex文件遇到变量

名称时,我希望它通过yylval将它传递给yacc,然后

检索它以添加到语法树。为此,我在lex文件(第三部分)中编写了

以下代码,它将在yylval中保存

变量:


void copy_name(char ** dst,char * yy){

int len;


//免费(* dst);


//为新字符串分配内存

len = strlen(yy);

* dst =(char *)malloc( len * sizeof(char)+ 1);

//复制字符串

strcpy(* dst,yy);

}


它基本上是strcpy()的包装器,它还通过malloc()分配一些内存




在lex文件中,我有这样的东西

{LETTER}({LETTER} | {DIGIT} |" _")* {copy_name(&(yylval.Name),yytext );

返回(NAME);}


现在我的问题是我是否应该在copy_name中保持免费电话或
$ b $不是。我认为我需要这样做,否则每次找到新变量时都会分配新的内存,并且我将面对内存

泄漏。然而,当我取消注释该行时,我开始得到段错误,因为我无法理解




任何对lex有更多专业知识的人都可以/ yacc帮我解决了这个

问题?


谢谢,

Berk Birand


-

通过

Hi,

I am working on a school project where we use lex/yacc to write a compiler
for a fictional (Java-like) language. I have handled all the details about
the yacc and lex files, but I still have a question regarding the dynamic
memory allocation for strings. When the lex file encounters a variable
name, I want it to pass this to yacc through yylval, and then
retrieve it to add to a syntax tree. For this purpose, I wrote the
following code in the lex file (third section), that will save the
variable in yylval:

void copy_name(char** dst, char* yy) {
int len;

//free(*dst);

// allocate memory for the new string
len = strlen(yy);
*dst = (char*) malloc(len * sizeof(char) + 1);
// copy the string
strcpy(*dst, yy);
}

It''s basically a wrapper around strcpy(), which also allocates some memory
through malloc().

In the lex file, I have something like this
{LETTER}({LETTER}|{DIGIT}|"_")* {copy_name(&(yylval.Name) , yytext);
return(NAME);}

Now my question is whether I should keep the call to free in copy_name or
not. I would think that I need to do that, otherwise new memory would be
allocated each time a new variable is found, and I''d be facing memory
leaks. Yet when I uncomment that line, I start to get segfaults, for
reasons that I can''t understand.

Can anybody with more expertise with lex/yacc help me out with this
problem?

Thank you,
Berk Birand

--
Posted via a free Usenet account from http://www.teranews.com

推荐答案



为什么不* dst = malloc(len + 1); ?


sizeof(char)的定义是1。

Why not *dst = malloc(len + 1); ?

sizeof(char) is 1 by definition.



yylval在哪里.Name来自形式,它不是由malloc分配的,所有

投注都是关闭的。您必须使用malloc为您的

字符串分配所有内存,或者设计另一种方案来复制它们。


-

Ian Collins。

Where does yylval.Name come form, it it isn''t allocated by malloc, all
bets are off. You either have to allocate all of the memory for your
string with malloc, or devise another scheme for copying them.

--
Ian Collins.




为什么不* dst = malloc(len + 1); ?


sizeof(char)的定义是1。


Why not *dst = malloc(len + 1); ?

sizeof(char) is 1 by definition.



是的,我想我能做到。我认为这样会更明确,

但是如果它被定义为1那么它只是额外的复杂化。

Yeah I guess I can do that. I figured it would be more explicit this way,
but if it''s defined as 1 then it''s just extra complication.



yylval.Name来自何处,它不是由malloc分配的,所有

投注都是关闭的。您必须使用malloc为您的

字符串分配所有内存,或者设计另一种方案来复制它们。

Where does yylval.Name come form, it it isn''t allocated by malloc, all
bets are off. You either have to allocate all of the memory for your
string with malloc, or devise another scheme for copying them.



这就是问题所在。 yylval是yacc的内部结构变量。在我的

yacc文件中,我设置它的类型如下:


%union {

int Value;

char运算符[3];

char *名称;

NodeType * nPtr;

};


现在Name是一个字符指针。 Lex并没有搞乱它的分配,而只是声明yylval是一个结构。

这个copy_name函数就是做这个分配。所以我想我们可以

假设我编写的代码将是分配它的代码。

使用免费是否有意义?


感谢您的回答,

bb


-

通过免费的Usenet帐户发布来自

That''s the problem. yylval is an internal struct variable to yacc. In my
yacc file, I set it''s type as follows:

%union {
int Value;
char Operator[3];
char* Name;
NodeType *nPtr;
};

Now Name is a character pointer. Lex doesn''t mess with the allocation of
it, but just declares yylval to be a struct.The point of
this copy_name function is to do that allocation. So I suppose we can
assume that the code that I wrote will be the one that will allocate it.
Would it the make sense to use free?

Thanks for you answer,
bb

--
Posted via a free Usenet account from http://www.teranews.com





这就是问题所在。 yylval是yacc的内部结构变量。在我的

yacc文件中,我设置它的类型如下:


%union {

int Value;

char运算符[3];

char *名称;

NodeType * nPtr;

};


现在Name是一个字符指针。 Lex并没有搞乱它的分配,而只是声明yylval是一个结构。

这个copy_name函数就是做这个分配。所以我想我们可以

假设我编写的代码将是分配它的代码。

使用免费是否有意义?



That''s the problem. yylval is an internal struct variable to yacc. In my
yacc file, I set it''s type as follows:

%union {
int Value;
char Operator[3];
char* Name;
NodeType *nPtr;
};

Now Name is a character pointer. Lex doesn''t mess with the allocation of
it, but just declares yylval to be a struct.The point of
this copy_name function is to do that allocation. So I suppose we can
assume that the code that I wrote will be the one that will allocate it.
Would it the make sense to use free?



除非已经分配了某些东西,否则不会。如果添加免费原因

一次崩溃,赔率是指针未分配并且包含随机值

扼杀免费。因此,如果作业一次由您完成,而且再也不会再为
,则免费。如果它被重新分配(由你),那么你有一个

的问题,因为无法判断该值是否有效。


-

Ian Collins。

Not unless something has been assigned to it. If adding the free causes
a crash, odds are the pointer is unassigned and contains a random value
that chokes free. So if the assignment is made once, by you and never
again, drop the free. If it gets reassigned (by you) then you have a
problem as there is no way of telling if the value is valid or not.

--
Ian Collins.


这篇关于在lex / yacc中释放()或不释放()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-22 19:12