C auto-expanding array of pointers

QUESTION ANSWER TO THE END OF THE PAGE. FULLY WORKING CODE.

Hi, I would like to do in C what I asked in the title, however, I do not know how to do this. I did this in C ++ thanks to the templates, but à la C. Here is the fully functional C ++ code: List.h (simple database)

* I wonder if with void pointers I can emulate code. The problem is that I saw a link that void * should be avoided because it can cause more problems than it can solve.

Basically, it is a “smart array” that stores pointers to the variables themselves. If I know the size of each pointer and the size of each specified structure, should simple mallocs and reallocs do the right thing?

typedef struct
{
  void **list;

  // internal
  int last_item_index;
  size_t element_size; // size of each pointer
  int elements;        // number of currently allocated elements
  int total_size;      // >= #elements so that we don't have to always call malloc
  int tweak_request_size; // each time the list grows we add this # of elements

} List;
// a shot at an addCopy function
// it deepcopies the object you pass in
List_addCopy(List *db, void *ptr_to_new_element)
{
  ... // grow **list
  // alloc and copy new element
  db->list[db->last_item_index+1] = malloc(element_size); // WORKS?
  // HOW TO COPY THE ELEMENT TO HERE IF IT IS A STRUCTURE FOR INSTANCE???
  ...
}

or
// a shot at an assign function 
// (allocate the elements yourself then pass the pointer to the List)
List_assign(List *db, void *ptr_to_new_element)
{
  db->List = realloc(db->List, element_size*(elements+tweak_request_size));
  db->List[db->last_item_index+1] = ptr_to_new_element;
}

// Usage example

List db; // our database
struct funky *now = (funky*)malloc(sizeof(funky));

funky->soul = JamesBrown;

List_addCopy(db, funky);

if (list[0]->soul == JamesBrown)
  puts("We did It! :D");

, , - **.

List_add? , / ?

List_assign? .

: p

+4
2

void* - :

#include <stdio.h>
#include <stdlib.h>

#define List(T) \
    typedef struct { \
        T** items; \
        int count;  \
    } List_ ## T ;\
    \
    List_ ## T * List_ ## T ## _New() { \
        List_ ## T * list = (List_ ## T *) malloc(sizeof(List_ ## T)); \
        list->count = 0; \
        return list; \
    } \
    \
    void List_ ## T ## _Add(List_ ## T *list, T * data) { \
        printf("%d\n", ++list->count); \
    } \
    void List_ ## T ## _Del(List_ ## T *list, int index) { \
        printf("%d\n", --list->count); \
    }

/* define just one list per type */
List(int);
List(double);

int main()
{
    int a, b, c;
    double d, e;
    List_int *l1;
    List_double *l2;

    l1 = List_int_New();
    List_int_Add(l1, &a);
    List_int_Add(l1, &b);
    List_int_Add(l1, &c);
    List_int_Del(l1, 0);
    List_int_Del(l1, 0);
    List_int_Del(l1, 0);
    free(l1);

    l2 = List_double_New();
    List_double_Add(l2, &d);
    List_double_Add(l2, &e);
    List_double_Del(l2, 0);
    List_double_Del(l2, 0);
    free(l2);

    return 0;
}

=)

+1

, , void ** , xD

, ( , " "), , , @SourceForge, , void, ;) .. .

, , : List - &&

, , , .

+1

Source: https://habr.com/ru/post/1787850/


All Articles