Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Array of struct and sizeof

I want to write, in C (no particolar flavour, lets say c11) a generic, global const array of struct, like the following pseudocode:

void * generic[] ={&(struct a va),&(struct b vb)};

Now I want to create a function that, given the id of the position of desired struct (I'm planning to use a const for every id, hardcoded), will copy it.

Because that copy() also is generic (accepts a void * as destination) it would need to know the strict size, witch may be specified by caller (a lot of error prone, he already need to provide the target structure and the right id) or i will also maintain a parallel array of correct sizeof for every structure.

Is there a way to automate at compile time the initialization of sizeof array? Or even implement some sort of trick to let the user specify just pass the pointer to struct without its id, without having to write a specialized get_struct_x()?

No dynamic allocation (alloc(), local variable are fine) allowed, all struct and generic array content is known at compile time.

Sorry for my bad explanation, feel free to improve/correct it.

Edit for clarification: i need to deep copy a know struct type from an array where there are stored many struct type, but the same type never repeat itself. And I need to do it from a generic get function witch will be called from different thread, so before and after the copy will lock a mutex, and I want to keep all the locking and casting code in one point, to minimize debug and create more effective test.

like image 573
Lesto Avatar asked Sep 29 '22 19:09

Lesto


People also ask

Can you use sizeof on an array?

In C++, we use sizeof() operator to find the size of desired data type, variables, and constants. It is a compile-time execution operator. We can find the size of an array using the sizeof() operator as shown: // Finds size of arr[] and stores in 'size' int size = sizeof(arr)/sizeof(arr[0]);

How do you find the length of an array structure?

foo=sizeof(para)/sizeof(para[0]);

What is the sizeof struct?

Size of the struct should be sum of all the data member, which is: Size of int n1+ size of int* n2 +size of char c1+ size of char* c2. Now considering the 64-bit system, Size of int is 4 Bytes. Size of character is 1 Byte. Size of any pointer type is 8 Bytes.

Can a struct hold an array?

A structure may contain elements of different data types – int, char, float, double, etc. It may also contain an array as its member.


1 Answers

Without having to modify the underlying data structures, this is the only method I can see where this will be fairly manageable. Each entry in your array is not a void*, but rather a struct that contains an void* (to the element) and a size_t (sizeof data for that element):

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

typedef struct Entry
{
    size_t len;
    void const *elem;
} Entry;

#define ELEM_ENTRY(x)   { sizeof(x), &(x) }

struct A
{
    int a1;
    long a2;
} a;

struct B
{
    float b1;
    char b2[6];
} b;

struct C
{
    unsigned int c1;
    double c2[5];
} c;

Entry generic[] =
{
    ELEM_ENTRY(a),
    ELEM_ENTRY(b),
    ELEM_ENTRY(c)
};
size_t generic_n = sizeof(generic)/sizeof(*generic);


int main()
{
    for (size_t i=0; i<generic_n; ++i)
        printf("[%zu] = %zu\n", i, generic[i].len);
}

Output

[0] = 8
[1] = 12
[2] = 44

I think that's what you're trying to do. if not, I'll drop this answer. Note that this will likely not work how you want it to if the element is a pointer type (but it will work how you want it to if it is a native array type). So be careful.

like image 72
WhozCraig Avatar answered Oct 03 '22 02:10

WhozCraig