Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ionic-framework/2.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_Windows_Malloc_Realloc - Fatal编程技术网

C 为结构动态分配内存的正确方法是什么?

C 为结构动态分配内存的正确方法是什么?,c,windows,malloc,realloc,C,Windows,Malloc,Realloc,我正在开发一个程序,它应该在注册表中搜索特定的值,并将它们和它们的路径存储在一个数组中。所以我不知道程序会找到多少个键,因此我需要使用一个动态增长的数组。我现在使用这个代码,但我不确定它是否正确 struct data { char * Path; char * Key; }; struct data **RegArray = NULL; int ArrayCount = 0; // .... // .... // search the registry here.... // valu

我正在开发一个程序,它应该在注册表中搜索特定的值,并将它们和它们的路径存储在一个数组中。所以我不知道程序会找到多少个键,因此我需要使用一个动态增长的数组。我现在使用这个代码,但我不确定它是否正确

struct data
{
char * Path;
char * Key;
};
struct data **RegArray = NULL;

int ArrayCount = 0;

// ....
// ....

// search the registry here....

// value has been found, so i should add it to the array here
RegArray = ( struct data **)realloc( RegArray, ( ArrayCount + 1 ) * sizeof( struct data *) );
RegArray[ ArrayCount ] = ( struct data *)malloc( sizeof( struct data ) );

RegArray[ ArrayCount ]->Path = _strdup( CurrentPath );
RegArray[ ArrayCount ]->Key = _strdup( CurrentKey );

ArrayCount++;
有人能告诉我这是否可以。如果没有,我应该如何正确地执行


谢谢

你已经掌握了要点。但是,您应该做一些改进:

  • :

    …变成

    RegArray[ ArrayCount ] = malloc( sizeof( struct data ) );
    
    struct data **tmp = realloc( RegArray, ( ArrayCount + 1 ) * sizeof( struct data *) );
    if (tmp == NULL) {
        /* handle error case */
    }
    RegArray = tmp;
    
    RegArray[ ArrayCount ] = malloc( sizeof( struct data ) );
    if (RegArray[ ArrayCount ] == NULL) {
        /* handle error case */
    }
    
    RegArray[ ArrayCount ] = malloc( sizeof **RegArray );
    
  • 为防止内存泄漏,在检查是否成功分配到预期位置之前,请始终
    realloc
    临时变量:

    RegArray = ( struct data **)realloc( RegArray, ( ArrayCount + 1 ) * sizeof( struct data *) );
    
    …变成

    RegArray[ ArrayCount ] = malloc( sizeof( struct data ) );
    
    struct data **tmp = realloc( RegArray, ( ArrayCount + 1 ) * sizeof( struct data *) );
    if (tmp == NULL) {
        /* handle error case */
    }
    RegArray = tmp;
    
    RegArray[ ArrayCount ] = malloc( sizeof( struct data ) );
    if (RegArray[ ArrayCount ] == NULL) {
        /* handle error case */
    }
    
    RegArray[ ArrayCount ] = malloc( sizeof **RegArray );
    
  • 始终检查
    malloc
    realloc
    calloc
    等的返回值:

    …变成

    RegArray[ ArrayCount ] = malloc( sizeof( struct data ) );
    
    struct data **tmp = realloc( RegArray, ( ArrayCount + 1 ) * sizeof( struct data *) );
    if (tmp == NULL) {
        /* handle error case */
    }
    RegArray = tmp;
    
    RegArray[ ArrayCount ] = malloc( sizeof( struct data ) );
    if (RegArray[ ArrayCount ] == NULL) {
        /* handle error case */
    }
    
    RegArray[ ArrayCount ] = malloc( sizeof **RegArray );
    
  • 使用
    sizeof
    时,请使用变量而不是类型。我通常还会在
    sizeof
    中的表达式周围添加无用的括号,以提高可读性:

    RegArray[ ArrayCount ] = malloc( sizeof( struct data ) );
    
    …变成

    RegArray[ ArrayCount ] = malloc( sizeof( struct data ) );
    
    struct data **tmp = realloc( RegArray, ( ArrayCount + 1 ) * sizeof( struct data *) );
    if (tmp == NULL) {
        /* handle error case */
    }
    RegArray = tmp;
    
    RegArray[ ArrayCount ] = malloc( sizeof( struct data ) );
    if (RegArray[ ArrayCount ] == NULL) {
        /* handle error case */
    }
    
    RegArray[ ArrayCount ] = malloc( sizeof **RegArray );
    
  • 列表方式:

    struct Data
    {
    char * Path;
    char * Key;
    Data * next;
    };
    
    void deallocate(Data *ptr){
        free(ptr->Path);
        free(ptr->Key);
        free(ptr);
    }
    
    Data *removeElement(Data *list, char *Key){
        Data *ptr = list;
        Data *prev = NULL;
        while(ptr != NULL){
            if(strcmp(Key,ptr->Key) == 0){
               if(prev != NULL){
                   prev->next = ptr->next;
                   deallocate(ptr);
               }
               else{
                   prev = ptr;
                   list = ptr->next;
                   deallocate(prev);
               }
            }
            else{
                ptr = ptr->next;
            }
        }
        return list;
    }
    
    Data * addElement(Data *list, char *path, char *key){
         if(list == NULL) {
            list = (Data *)malloc(sizeof(Data));
            return list;
         }
         Data *cursor = list;
         while(cursor != NULL){
             cursor = cursor->next;
         }
         cursor = (Data *)malloc(sizeof(Data));
         cursor->next = NULL;
         cursor->path = path;
         cursor->key = key;
         return list;
    }
    
    int main(){
        Data *list = NULL;
    
        // value has been found
        list = addElement(list,path,key);
    
    return 0;
    }
    

    realloc不能用于未分配的data@AlterMann:
    realloc
    指针上的
    NULL
    相当于
    malloc
    。netcoder,您说得对,非常感谢您的回答。这也很有帮助,但不幸的是,我只能接受一个答案。@HAL9000,您应该删除malloc上的强制转换,这里有进一步的讨论:@HAL9000:您可以发布代码如何删除元素吗?感谢you@kampi我添加了两个函数。我没有编译代码,如果你有问题尽管问。