2013-11-28 58 views
1

我正在研究一個程序,它應該搜索註冊表中的特定值,並將它們和它們的路徑存儲在一個數組中。所以我不知道程序會找到多少個鍵,因此我需要使用動態增長的數組。我現在使用此代碼,但我不確定它是否正確。什麼是爲結構動態分配內存的正確方法?

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++; 

有人可以告訴我,如果這可能是好的,或沒有。如果不是,我該如何正確做?

謝謝!

+0

realloc不能用於未分配的數據 –

+1

@AlterMann:'NULL'指針上的'realloc'相當於'malloc'。 – netcoder

+0

netcoder,你是對的,對不起 –

回答

1

你已經掌握了它的要點。然而,也有一些改進,你應該:

  1. Don't cast the return value of malloc, realloc, calloc, etc.

    RegArray[ ArrayCount ] = (struct data *)malloc(sizeof(struct data)); 
    

    ... ...變得

    RegArray[ ArrayCount ] = malloc(sizeof(struct data)); 
    
  2. 爲了防止內存泄漏,總是realloc到臨時變量,然後在檢查它是否成功後分配到預定位置:

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

    ... ...變得

    struct data **tmp = realloc(RegArray, (ArrayCount + 1) * sizeof(struct data *)); 
    if (tmp == NULL) { 
        /* handle error case */ 
    } 
    RegArray = tmp; 
    
  3. 經常檢查malloc返回值,realloccalloc等:

    RegArray[ ArrayCount ] = (struct data *)malloc(sizeof(struct data)); 
    

    ... ...變得

    RegArray[ ArrayCount ] = malloc(sizeof(struct data)); 
    if (RegArray[ ArrayCount ] == NULL) { 
        /* handle error case */ 
    } 
    
  4. 使用th e變量而不是使用sizeof時的類型。我平時也下降了沒用括號周圍的表達sizeof以提高可讀性:

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

    ... ...變得

    RegArray[ ArrayCount ] = malloc(sizeof **RegArray); 
    
1

列表方式:

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; 
} 
+0

非常感謝你的回答。這也很有幫助,但不幸的是,我只能接受一個答案。 – kampi

+0

@ HAL9000,你應該刪除malloc上的強制轉換,進一步的討論在這裏:http://stackoverflow.com/questions/605845/do-i-cast-the-result-of-malloc –

+0

@ HAL9000:你能否也請發佈代碼如何刪除元素?謝謝 – kampi

相關問題