1

I'm working on a program, which should search the registry for specific values, and store them, and their path in an array. So i don't know how many keys the program will find, and therefore i need to use a dynamically growing array. I use this code right now, but i'm not sure if it is correct.

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

Could someone tell me please if this could is ok, or not. If not, how should i do it correctly?

Thanks!

kampi
  • 2,362
  • 12
  • 52
  • 91

2 Answers2

1

You have got the gist of it. However, there are a few improvements you should make:

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

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

    ...becomes...

    RegArray[ ArrayCount ] = malloc( sizeof( struct data ) );
    
  2. To prevent memory leaks, always realloc to a temporary variable before assigning to the intended location after checking if it was successful:

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

    ...becomes...

    struct data **tmp = realloc( RegArray, ( ArrayCount + 1 ) * sizeof( struct data *) );
    if (tmp == NULL) {
        /* handle error case */
    }
    RegArray = tmp;
    
  3. Always check the return value of malloc, realloc, calloc, etc.:

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

    ...becomes...

    RegArray[ ArrayCount ] = malloc( sizeof( struct data ) );
    if (RegArray[ ArrayCount ] == NULL) {
        /* handle error case */
    }
    
  4. Use the variable rather than the type when using sizeof. I also usually drop the useless parenthesis around the expression in sizeof to improve readability:

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

    ...becomes...

    RegArray[ ArrayCount ] = malloc( sizeof **RegArray );
    
Community
  • 1
  • 1
netcoder
  • 66,435
  • 19
  • 125
  • 142
1

The list way:

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;
}
HAL9000
  • 3,562
  • 3
  • 25
  • 47
  • Thank you very much for your answer. This is also very helpful, but unfortunately, i can accept only one answer. – kampi Dec 01 '13 at 01:35
  • @HAL9000, You should remove the casts on malloc, further discussion is here: http://stackoverflow.com/questions/605845/do-i-cast-the-result-of-malloc – Bruce Dean Dec 02 '13 at 03:36
  • @HAL9000: Could you please also post the code how to remove an element? Thank you – kampi Dec 11 '13 at 20:38
  • @kampi I added two functions. I didn't compile the code, if you have problems feel free to ask. – HAL9000 Dec 11 '13 at 21:57