Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/59.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/gwt/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在C中使用字符串数组实现符号表_C_Symbol Table - Fatal编程技术网

在C中使用字符串数组实现符号表

在C中使用字符串数组实现符号表,c,symbol-table,C,Symbol Table,我试图使用一个结构数组来创建一个符号表。这是我到目前为止所拥有的,但是我在create函数中分配内存时遇到了问题,到目前为止我所拥有的是正确的吗 我想要这样的结果作为arr的最终结果 {{sym1;1},{sym2;2},{sym3;3} struct str_id { char* s; int id; } struct symbol_table { int count; struct str_id** arr; } struct symbol_table *symbol_t

我试图使用一个结构数组来创建一个符号表。这是我到目前为止所拥有的,但是我在create函数中分配内存时遇到了问题,到目前为止我所拥有的是正确的吗

我想要这样的结果作为arr的最终结果 {{sym1;1},{sym2;2},{sym3;3}

struct str_id {
  char* s;
  int id;
}

struct symbol_table {
  int count;
  struct str_id** arr;
}

struct symbol_table *symbol_table_create(void) {
  struct symbol_table *stt = malloc(sizeof(struct symbol_table));
  stt->count = 1;
  stt->arr =  malloc(sizeof(struct str_id*) * stt->count);
  return stt;

}
对标识符使用描述性名称,而不是像s和stru_id这样的神秘短名称。 i、 e.根据标识符的类型或与其含义相反的标识符命名或前缀。 在您的例子中,我假设str_id是struct_id或string_id的缩写,这是一个坏名字,因为很明显它是一个struct或包含一个string。 它一直流行到20世纪90年代,当时程序员开始使用功能更强大的编辑器和IDE来跟踪变量类型,而现在已经不需要了。 * 始终通过将calloc和malloc的返回值比较为NULL来检查堆分配是否成功或失败。如果某个_指针中止,则可以执行此操作。 不要使用assert some_指针,因为断言仅在调试版本中启用,请使用abort,因为它表示程序异常终止,而不是退出。 传递size_t参数,以便使用者可以指定符号表的大小。 内存中保存的对象数量应表示为大小,例如数组索引器! 您需要在每个结构定义的末尾放一个分号。 是否确实要一个指向结构的指针数组,而不仅仅是一个结构数组?在这种情况下,可以使用内联结构并对数组使用单个分配,而不是单独分配每个成员。 因为要执行自定义分配,所以还必须定义析构函数。
结构符号_表{int count=1;…}非法。您无法在C中的类型声明中初始化结构字段。是的,对不起,我已经解决了这个问题。其余的可以吗?如果我在create函数中将它设置为1,那么它是如何未定义的呢?我明白你的意思了,我忘记了做stt->count。感谢使用stt->entries=calloc count,sizeofstruct symbol\u table\u entry;进行分配;。建议stt->entries=calloc count,stt的大小->条目[0];为了避免输入错误。@chux,谢谢你的改进,我已经免费移除了防护装置。至于sizeof,我不认为在这种情况下使用sizeof stt->entries[0]而不是sizeofstruct symbol\u table\u条目会带来很大的优势。Dai,sizeofstruct symbol\u table\u条目是最好的选择。sizeofstruct symbol\u table\u entry*应该是正确的。sizeof stt->entries[0]更有可能被正确编码,它避免了错误类型的原始编码错误,因此具有易于查看和维护的优点。
struct symbol_table_entry {
  char* symbolText;
  int   id;
};

struct symbol_table {
  size_t count;
  struct symbol_table_entry** entries;
};

struct symbol_table* create_symbol_table( size_t count ) {
    struct symbol_table* stt = malloc( sizeof(struct symbol_table) );
    if( !stt )
    {
        abort();
    }
    stt->count = count;
    stt->entries = calloc( count, sizeof(struct symbol_table_entry) );
    if( !stt->entries ) {
        free( stt );
        abort();
    }
    // Note that calloc will zero-initialize all entries of the array (which prevents debuggers showing garbage string contents) so we don't need to do it ourselves.
    return stt;
}

void destroy_symbol_table( struct symbol_table* stt, bool free_strings ) {
    if( stt->entries ) {
        if( free_strings ) {
            for( size_t i = 0; i < stt->count; i++ ) {
                free( stt->entries[i]->symbolText );
            }
        }
        free( stt->entries );
    }
    free( stt );
}