C dynamically growing array

前端 未结 7 2018
闹比i
闹比i 2020-11-22 07:57

I have a program that reads a \"raw\" list of in-game entities, and I intend to make an array holding an index number (int) of an indeterminate number of entities, for proce

7条回答
  •  一生所求
    2020-11-22 08:41

    Building on Matteo Furlans design, when he said "most dynamic array implementations work by starting off with an array of some (small) default size, then whenever you run out of space when adding a new element, double the size of the array". The difference in the "work in progress" below is that it doesn't double in size, it aims at using only what is required. I have also omitted safety checks for simplicity...Also building on brimboriums idea, I have tried to add a delete function to the code...

    The storage.h file looks like this...

    #ifndef STORAGE_H
    #define STORAGE_H
    
    #ifdef __cplusplus
    extern "C" {
    #endif
    
        typedef struct 
        {
            int *array;
            size_t size;
        } Array;
    
        void Array_Init(Array *array);
        void Array_Add(Array *array, int item);
        void Array_Delete(Array *array, int index);
        void Array_Free(Array *array);
    
    #ifdef __cplusplus
    }
    #endif
    
    #endif /* STORAGE_H */
    

    The storage.c file looks like this...

    #include 
    #include 
    #include "storage.h"
    
    /* Initialise an empty array */
    void Array_Init(Array *array) 
    {
        int *int_pointer;
    
        int_pointer = (int *)malloc(sizeof(int));
    
        if (int_pointer == NULL)
        {       
            printf("Unable to allocate memory, exiting.\n");
            free(int_pointer);
            exit(0);
        }
        else
        {
            array->array = int_pointer; 
            array->size = 0;
        }
    }
    
    /* Dynamically add to end of an array */
    void Array_Add(Array *array, int item) 
    {
        int *int_pointer;
    
        array->size += 1;
    
        int_pointer = (int *)realloc(array->array, array->size * sizeof(int));
    
        if (int_pointer == NULL)
        {       
            printf("Unable to reallocate memory, exiting.\n");
            free(int_pointer);
            exit(0);
        }
        else
        {
            array->array = int_pointer;
            array->array[array->size-1] = item;
        }
    }
    
    /* Delete from a dynamic array */
    void Array_Delete(Array *array, int index) 
    {
        int i;
        Array temp;
        int *int_pointer;
    
        Array_Init(&temp);
    
        for(i=index; isize; i++)
        {
            array->array[i] = array->array[i + 1];
        }
    
        array->size -= 1;
    
        for (i = 0; i < array->size; i++)
        {
            Array_Add(&temp, array->array[i]);
        }
    
        int_pointer = (int *)realloc(temp.array, temp.size * sizeof(int));
    
        if (int_pointer == NULL)
        {       
            printf("Unable to reallocate memory, exiting.\n");
            free(int_pointer);
            exit(0);
        }
        else
        {
            array->array = int_pointer; 
        } 
    }
    
    /* Free an array */
    void Array_Free(Array *array) 
    {
      free(array->array);
      array->array = NULL;
      array->size = 0;  
    }
    

    The main.c looks like this...

    #include 
    #include 
    #include "storage.h"
    
    int main(int argc, char** argv) 
    {
        Array pointers;
        int i;
    
        Array_Init(&pointers);
    
        for (i = 0; i < 60; i++)
        {
            Array_Add(&pointers, i);        
        }
    
        Array_Delete(&pointers, 3);
    
        Array_Delete(&pointers, 6);
    
        Array_Delete(&pointers, 30);
    
        for (i = 0; i < pointers.size; i++)
        {        
            printf("Value: %d Size:%d \n", pointers.array[i], pointers.size);
        }
    
        Array_Free(&pointers);
    
        return (EXIT_SUCCESS);
    }
    

    Look forward to the constructive criticism to follow...

提交回复
热议问题