Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/58.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/arrays/12.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/8/xslt/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_Arrays_Struct - Fatal编程技术网

C 重置/清除指向结构的指针的全局数组

C 重置/清除指向结构的指针的全局数组,c,arrays,struct,C,Arrays,Struct,我是C新手,在重置或清除指向结构的全局指针数组时遇到了一些问题。 我需要接收一个文件(或更多)并在两个周期内读取它的行。 在阅读时,我需要创建一个符号表(基本上是标签),然后使用它创建更多的数据(将标签的位置导出到文件,并以十六进制值写入) 我遇到的问题是当我读两个文件时。第一个文件读取良好,所有值都正确,但当它尝试读取第二个文件时,我创建的表(数组)中的值仍然存在,即使我释放了它们 我在头文件中声明了这一点:(大小为30) 添加新符号的示例:(位于读取函数中) 添加功能,在不同的文件中:(更新

我是C新手,在重置或清除指向结构的全局指针数组时遇到了一些问题。 我需要接收一个文件(或更多)并在两个周期内读取它的行。 在阅读时,我需要创建一个符号表(基本上是标签),然后使用它创建更多的数据(将标签的位置导出到文件,并以十六进制值写入)

我遇到的问题是当我读两个文件时。第一个文件读取良好,所有值都正确,但当它尝试读取第二个文件时,我创建的表(数组)中的值仍然存在,即使我释放了它们

我在头文件中声明了这一点:(大小为30)

添加新符号的示例:(位于读取函数中)

添加功能,在不同的文件中:(更新

以及第二个文件的第一个循环后的错误输出:(更新

应该是:

action: yes | extern: no | address: 100 | label MAIN | data: no
action: no | extern: yes | address: 0 | label W | data: no
action: yes | extern: no | address: 108 | label LOOP | data: no
action: no | extern: no | address: 0 | label STR | data: yes
action: no | extern: no | address: 3 | label K | data: yes
有几个问题:

  • 我用过之后,有没有办法清理清单?然后对新值重复使用相同的列表?避免内存泄漏
  • 这种使用数组的想法只是浪费空间吗?我应该尝试使用链表吗?我担心我将不得不重新写我的整个项目,而时间不在我这边
  • 更新后

  • 当我到达数组的末尾时,如何重新分配更多内存以增加数组的大小
    抱歉,如果我误用了一个术语,我正在用另一种语言学习C。

    即使您正在释放结构,您的数据也不会立即被覆盖,因为不需要堆分配器;它只是说明您释放的空间是可用的。您应该更进一步,在释放数组的每个索引后,将它们设置为NULL。这实际上会将它们从阵列中移除

    现在,我在使用数组时要小心。您已经指定了一个大小,但没有在add函数中强制它

    如果使用malloc交换阵列以使用动态内存,并且在程序中稍后达到大小限制,则可以使用
    realloc
    分配更多空间(这基本上允许您在不干扰程序其余实现的情况下实现ArrayList)

    要最初为阵列分配空间,请尝试:

    symbol **symbol_Table = (symbol *) malloc(sizeof(symbol *) * SIZE);
    

    注意:只能从方法内部调用malloc。因此,您可以在文件顶部声明变量:

    symbol **symbol_Table;
    
    然后,在使用它之前,应该在
    main()
    方法中定义它:

    symbol_Table = (symbol **) malloc(sizeof(symbol **) * SIZE);
    

    注意malloc语句中的符号-您需要乘以 具有符号指针大小的数组,因为malloc接受要分配的字节数
    sizeof(symbol*)
    提供单个
    symbol
    指针的大小

    另外:为了使用realloc,
    symbol\u Table
    的初始分配应该是malloc/calloc/realloc返回的值,或者是NULL

    如果您只是想将重新分配弹出到If语句中,请尝试:

    static size_t current_size = SIZE;
    
    if(count == current_size) {
      current_size = current_size << 1;
      symbol_Table = (symbol *)realloc(symbol_table, sizeof(symbol *) * current_size);
    }
    
    if(count < current_size) {
      // ... the rest of the code continues here
    
    你的问题是(依我看)
    SIZE
    是一个常数。如果将其设置为变量,则可以在需要时调整符号表的大小

    最干净的方法是将指针数组和大小一起存储在某些信封结构中,如:


    结构符号{ 未签名操作:1; 无符号外部:1; 无符号数据_标志:1; int地址; 字符标签[大小]; }象征; 结构符号表{ 无符号大小; 未签名的使用; 结构符号**表;/*用作指针数组*/ }; /*全球:为了好玩*/ 结构symtab thetab={0,0,NULL}; 整型调整大小(结构符号表*sp,未签名的新大小) { 结构符号**新表; /*注意:realloc也负责复制*/ newtable=realloc(sp->table,新的大小*sp的大小->table[0]); 如果(!newtable){/*句柄错误*/ 返回-1; } sp->table=newtable; /*TODO:您也需要检查一下新的尺寸size=新尺寸; 返回0;/*无错误*/ } /*典型用法:*/ if(thetab.used>=thetab.size&调整对象大小(&thetab,12345)){ /*报告错误*/ } 表[thetab.used++]=新的_符号(…);
    释放指针将释放指针指向的内存;它对指针值本身没有任何影响。我建议维护一个计数器,指示数组中有多少前导元素包含有效值。这样做是为了通过索引检查条目的有效性,而不是假设非空指针值是有效的。或者,在释放每个指针后,将其显式设置为空。并且您应该设置(并控制)一个限制,以避免
    symbol\u Table
    上出现溢出。我仍然没有解决这个问题,因此,我不知道这些答案是否正确。如何使用realloc内存来增加数组的大小?我添加了一些代码来演示如何使用realloc。ooohh-malloc调用只能在方法中发生。您应该在当前位置声明变量,然后在main方法中使用malloc来定义它。我将更新我的答案。为整个数组分配内存后,如何在两个循环结束时释放它?像使用free(symbol_Table[i])运行循环这样的事情给了我一个错误:“free”参数1的类型不兼容@Anish Goyal在对代码进行了大量修改之后,我相信它现在工作正常了!我真是太感谢你了!所以再次感谢你!:)我还没试过,但我回家后就可以了!谢谢!:)
    action: yes | extern: no | address: 100 | label MAIN | data: yes
    action: no | extern: yes | address: 0 | label W | data: yes
    action: yes | extern: no | address: 108 | label LOOP | data: yes
    action: no | extern: no | address: 0 | label STR | data: yes
    action: no | extern: no | address: 3 | label K | data: yes
    
    action: yes | extern: no | address: 100 | label MAIN | data: no
    action: no | extern: yes | address: 0 | label W | data: no
    action: yes | extern: no | address: 108 | label LOOP | data: no
    action: no | extern: no | address: 0 | label STR | data: yes
    action: no | extern: no | address: 3 | label K | data: yes
    
    symbol **symbol_Table = (symbol *) malloc(sizeof(symbol *) * SIZE);
    
    symbol **symbol_Table;
    
    symbol_Table = (symbol **) malloc(sizeof(symbol **) * SIZE);
    
    static size_t current_size = SIZE;
    
    if(count == current_size) {
      current_size = current_size << 1;
      symbol_Table = (symbol *)realloc(symbol_table, sizeof(symbol *) * current_size);
    }
    
    if(count < current_size) {
      // ... the rest of the code continues here
    
    int i;
    for(i = 0; i < count ; i++){
      if(symbol_Table[i] != NULL) // Important - we cannot free NULL
      {
        free(symbol_Table[i]);
        symbol_Table[i] = NULL;
      }
    }
    count = 0; // Important - if we are deleting everything in the array, we
               // need to make sure we reset the count for our next delete
               // operation
    
    struct symbol{
            unsigned action :1;
            unsigned external :1;
            unsigned data_flag :1;
            int address;
            char label[SIZE];
            } symbol ;
    
    
    struct symtab{
            unsigned size;
            unsigned used;
            struct symbol **table; /* used as an array of pointers */
            };
    
            /* global: for fun */
    struct symtab thetab = {0,0,NULL};
    
    int resize_the_thing(struct symtab *sp, unsigned new_size)
    {
    struct symbol **newtable;
    
            /* Note: realloc takes care of copying, too ...*/
    newtable = realloc (sp->table, new_size * sizeof sp->table[0]);
    
    if (!newtable) { /* handle error */
            return -1;
            }
    
    sp->table = newtable;
            /* TODO: you need to check the case new_size < size here, too */
    sp->size = new_size;
    return 0; /* no_error */
    }
    
    /* typical usage: */
    
    if (thetab.used >= thetab.size && resize_the_thing( &thetab, 12345)) {
            /* report error */
            }
    
    thetab.table[thetab.used++] = new_symbol(...);