Pure ANSI-C: create a shared array

Is it possible to replicate a shared array in pure ANSI-C?

I have this structure that contains an array (for floating at the moment) and some variables such as size and capacity for mutation in the array.

typedef struct _CustomArray
{
    float* array; //the array in which the objects will be stored
    int size; //the current size of the array
    int capacity; //the max capacity of the array
} CustomArray; 

I use this structure, so I can create an array in pure C, where I can add / remove elements, dynamically expand the size of the array if necessary, etc. everything that the "standard" array does, except that it is made only in C, and now I want to make it so that when you initialize this structure, you can set the data type of the elements that should be held, and at the moment it is capable of store only float data types, but I want to make it so that it can store any data type / other structures. But I do not know if this is possible.

At this point, the function to create this array is:

CustomArray* CustomArray_Create(int initCapacity, /*type elementType*/)
{
    CustomArray* customArray_ptr; //create pointer to point at the structure
    float* internalArray = (float*)malloc(sizeof(float) * initCapacity); //create the internal array that holds the items
    if(internalArray != NULL)
    {
        CustomArray customArray = { internalArray, 0, initCapacity }; //make the struct with the data
        customArray_ptr = &customArray; //get the adress of the structure and assign it to the pointer
        return customArray_ptr; //return the pointer
    }
    return NULL;
}

Is it possible to specify a data type as a parameter so that I can store memory for this data type and dynamically display it as a given data type in an array?

Thanks in advance,

Marnix van Rijswijk

+5
5

... (CustomArray) , . , .

, , ... , - :

#include <stdlib.h> 
#define DefArray(type) \
typedef struct T_##type##Array {\
    type *array; \
    int size, capacity; \
} type##Array; \
static type##Array *type##ArrayCreate(int capacity)\
{\
    type##Array *s = malloc(sizeof(type##Array));\
    if (!s) return NULL;\
    s->array = malloc(sizeof(type) * capacity);\
    if (!s->array) { free(s); return NULL; }\
    s->size=0; s->capacity = capacity;\
    return s;\
}

#include "customarray.h"
DefArray(float);
DefArray(double);

void foo()
{
    floatArray *fa = floatArrayCreate(100);
    ...
}

, . , ( , , ++, , ). .h .c .

+8

, ++.

, C, , (sizeof (type)).

, , , , , - . bsearch().

+2

X-macros.

(, ) , .

// defining generic parameters
#define PREFIX tv
#define ITEM token
#define NAME token_vector
#include "vector.h"

...
token_vector tv = tv_new(100);
*(tv.front) = some_token;
tv_push_back(&tv, other_token);
+2

C , .

, . , : .

, ( , ), , .. .. Coughcrazycough C .

, : https://github.com/christianfriedl/CGenerics/blob/master/src/cgArray.h

0

, " " , . ( , " * * alloc" ) , " " - , , , char char[] .

. , ( ), ( , ).

, - :

# define DECL_VECTOR(NAME,TYPE,SIZE) PUN_STRUCT(NAME,TYPE,SIZE)  INIT_STRUCT(NAME,TYPE,SIZE) 

# define PUN_SIZE sizeof(void*)+sizeof(int)*2


# define PUN_STRUCT(NAME,TYPE,SIZE)                      \
   struct {                                              \
      TYPE (*p)[(SIZE)];                                 \
      int size;                                          \
      int capacity;                                      \
   } *NAME = malloc(PUN_SIZE);                        


# define INIT_STRUCT(NAME,TYPE,SIZE)  do {               \
   if (!NAME) {                                          \
        perror("malloc fail");                           \
        abort();                                         \
   }else {                                               \
        NAME->size = (SIZE);                             \
        NAME->capacity = 0;                              \
        NAME->p = malloc(sizeof(*NAME->p));              \
        if (!NAME->p) {                                  \
            perror("malloc fail");                       \
            abort();                                     \
        }                                                \
        NAME->p = (TYPE(*)[(SIZE)])(NAME->p);            \
   }                                                     \
   }while(false)


int main(int argc, char *argv[]) 
 {

   DECL_VECTOR(vec1,int,8);


    printf("size of handle struct:  %zu,\
  size of vector array:   %zu,\
  size of vector element: %zu\n\
address of handle struct: %p,\
address of vector array:  %p\n",  sizeof(*vec1),       \
                                  sizeof(*vec1->p),    \
                                  sizeof(*vec1->p[0]), \
                                  vec1,                \
                                  vec1->p);

   return 0;
 }
0
source

All Articles