简体   繁体   English

野牛-符号表-Malloc免费匹配

[英]Bison - symbol table - matching free for malloc

I am going through mfcalc example in the Bison manual and I had a question about the symbol table. 我正在阅读 Bison手册中的mfcalc示例,并且对符号表有疑问。

Specifically in the routine putsym() we have calls to malloc but I don't see the corresponding call to free . 具体来说,在例程putsym()我们有对malloc调用,但没有看到对free的相应调用。 Do we need to deallocate symbol table ( sym_table in the following code) manually or does the tool take care of this automatically? 我们需要手动取消分配符号表(以下代码中的sym_table )还是该工具自动进行处理?

symrec *
putsym (char const *sym_name, int sym_type)
{
  symrec *ptr = (symrec *) malloc (sizeof (symrec));
  ptr->name = (char *) malloc (strlen (sym_name) + 1);
  strcpy (ptr->name,sym_name);
  ptr->type = sym_type;
  ptr->value.var = 0; /* Set value to 0 even if fctn.  */
  ptr->next = (struct symrec *)sym_table;
  sym_table = ptr;
  return ptr;
}

"The tool" knows nothing about what your actions do. “工具”对您的操作一无所知。

I quoted "the tool" because in reality, there are at least two code generation tools involved in most parsing projects: a parser generators (bison, in this case) and a scanner generator ((f)lex, perhaps). 我引用“工具”是因为实际上,大多数解析项目中至少涉及两个代码生成工具:解析器生成器(在这种情况下为野牛)和扫描器生成器(也许是(f)lex)。 The mfcalc example uses a hand-built lexer to avoid depending on lex although it would probably have been simpler to have used (f)lex. mfcalc示例使用手工生成的词法分析器来避免依赖lex尽管使用(f)lex可能会更简单。 In any event, the only calls to the symbol table library are in the scanner and have absolutely nothing to do with the bison-generated code. 无论如何,对符号表库的唯一调用都在扫描程序中,并且与野牛生成的代码完全无关。

Of course, there are other tools at play. 当然,还有其他工具在起作用。 For example, the entire project is built with a C compiler and runs inside some kind of hosted environment (to use the words of the C standard); 例如,整个项目是使用C编译器构建的,并且在某种托管环境中运行(使用C标准的语言); in other words, an operating system and runtime support library which includes implementations of malloc and free (although, as you point out, free is nowhere called by the example code). 换句话说,一个包含mallocfree实现的操作系统和运行时支持库(尽管您指出,示例代码在任何地方都没有提到free )。

I mention these last because they are relevant to your question. 我最后提到这些是因为它们与您的问题有关。 When a process terminates, all process resources are released, including its memory image. 当进程终止时,将释放所有进程资源,包括其内存映像。 (This is not required by the C standard but almost all hosted environments work that way.) So you don't really need to free() memory allocated if it is going to be in use up to program termination. (这不是C标准所必需的,但几乎所有托管环境都可以这种方式工作。)因此,如果要在程序终止之前使用它,则实际上并不需要free()分配的内存。

Like global variables, unreleased memory allocations were pretty common at one time. 像全局变量一样,一次未释放的内存分配很常见。 These days, such things are considered poor practice (at best) and most programmers will avoid them, but it wasn't always the case. 如今,此类事情被认为是(最好的)实践不佳,大多数程序员会避免使用它们,但并非总是如此。 There was a time when many programmers considered it wasteful to track resources only in order to release them just before program termination, or to jump through the hoops necessary to ensure that pre-termination cleanup was guaranteed to execute. 曾经有一段时间,许多程序员认为跟踪资源仅是为了在程序终止之前释放资源,或者跳过确保执行终止前清理所必需的循环是浪费的。 (Even today, many programmers will just insert a call to exit(1) when an unrecoverable error occurs, rather than going to the bother of tracking down and manually freeing every allocated memory block. Particularly in non-production code.) (即使到了今天,许多程序员也只会在发生不可恢复的错误时才插入对exit(1)的调用,而不必费心查找和手动释放每个分配的内存块。特别是在非生产代码中。)

Whether you approve of this coding style or not, the examples in the bison manual (and many other code examples of all kinds) date back to that innocent time. 无论您是否赞成这种编码方式,野牛手册中的示例(以及许多其他各种代码示例)都可以追溯到那段纯真的时间。

So, it's true that the symbol table entries in this example are never freed. 因此,在此示例中,绝对不会释放符号表条目。 Your production code should probably do better, but it also should probably use a more efficient data structure and avoid depending on a (single) global context. 您的生产代码可能应该做得更好,但也应该使用更有效的数据结构,并避免依赖于(单个)全局上下文。 But none of that has anything to do with the bison features that mfcalc is attempting to illustrate. 但这与mfcalc试图说明的野牛功能没有任何关系。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM